The U and D part of CRUD
This commit is contained in:
parent
ed9e9af4ac
commit
fc4b1a5c75
@ -1,20 +1,11 @@
|
|||||||
from flask import (
|
import os
|
||||||
render_template,
|
import shutil
|
||||||
redirect,
|
from flask import flash
|
||||||
request,
|
|
||||||
flash,
|
|
||||||
url_for,
|
|
||||||
abort,
|
|
||||||
)
|
|
||||||
|
|
||||||
from usermodel import User
|
|
||||||
from flask_login import current_user
|
from flask_login import current_user
|
||||||
|
|
||||||
from forms.loginform import LoginForm
|
from usermodel import User
|
||||||
from forms.uploadform import UploadForm
|
from distribusimodel import Distribusis
|
||||||
from forms.distribusiform import DistribusiForm
|
|
||||||
from forms.themeform import ThemeForm
|
|
||||||
from forms.editorform import EditorForm
|
|
||||||
from forms.selectorform import SelectorForm
|
from forms.selectorform import SelectorForm
|
||||||
|
|
||||||
from statuspengguna.helper import HasDistribusi
|
from statuspengguna.helper import HasDistribusi
|
||||||
@ -25,30 +16,50 @@ from app import db
|
|||||||
|
|
||||||
def SelectNewDistribusi():
|
def SelectNewDistribusi():
|
||||||
print("make a new distribusi")
|
print("make a new distribusi")
|
||||||
|
selectorform = SelectorForm()
|
||||||
user = User.query.filter_by(email=current_user.email).first()
|
user = User.query.filter_by(email=current_user.email).first()
|
||||||
try:
|
try:
|
||||||
user.currentdistribusi = "new"
|
user.currentdistribusi = "new"
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
except:
|
except:
|
||||||
db.session.rollback()
|
db.session.rollback()
|
||||||
selectorform.new.errors.append("Unknown error occured!")
|
selectorform.distribusis.errors.append("Unknown error occured!")
|
||||||
flash("An error occured !", "danger")
|
flash("An error occured !", "danger")
|
||||||
|
return selectorform
|
||||||
|
|
||||||
|
|
||||||
def SelectUpdateDistribusi(distribusiname):
|
def SelectUpdateDistribusi(distribusiname):
|
||||||
print(f"Update this distribusi {distribusiname}")
|
print(f"Update this distribusi {distribusiname}")
|
||||||
|
selectorform = SelectorForm()
|
||||||
user = User.query.filter_by(email=current_user.email).first()
|
user = User.query.filter_by(email=current_user.email).first()
|
||||||
try:
|
try:
|
||||||
user.currentdistribusi = distribusiname
|
user.currentdistribusi = distribusiname
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
except:
|
except:
|
||||||
db.session.rollback()
|
db.session.rollback()
|
||||||
selectorform.new.errors.append("Unknown error occured!")
|
selectorform.distribusis.errors.append("Unknown error occured!")
|
||||||
flash("An error occured !", "danger")
|
flash("An error occured !", "danger")
|
||||||
|
return selectorform
|
||||||
|
|
||||||
|
|
||||||
def DeleteDistribusi(distribusiname):
|
def DeleteDistribusi(distribusiname):
|
||||||
print(f"delete this distribusi {distribusiname}")
|
print(f"delete this distribusi {distribusiname}")
|
||||||
|
selectorform = SelectorForm()
|
||||||
|
try:
|
||||||
|
user = User.query.filter_by(email=current_user.email).first()
|
||||||
|
distribusi = Distribusis.query.filter_by(
|
||||||
|
distribusiname=distribusiname
|
||||||
|
).first()
|
||||||
|
if distribusi.userid is user.id:
|
||||||
|
db.session.delete(distribusi)
|
||||||
|
db.session.commit()
|
||||||
|
userfolder = os.path.join("stash", distribusi.distribusiname)
|
||||||
|
shutil.rmtree(userfolder)
|
||||||
|
except:
|
||||||
|
db.session.rollback()
|
||||||
|
selectorform.distribusis.errors.append("Unknown error occured!")
|
||||||
|
flash("An error occured !", "danger")
|
||||||
|
return selectorform
|
||||||
|
|
||||||
|
|
||||||
def SelectorVisible():
|
def SelectorVisible():
|
||||||
|
@ -8,15 +8,10 @@ from datetime import timedelta
|
|||||||
from flask import (
|
from flask import (
|
||||||
render_template,
|
render_template,
|
||||||
redirect,
|
redirect,
|
||||||
flash,
|
|
||||||
url_for,
|
url_for,
|
||||||
session,
|
session,
|
||||||
send_from_directory,
|
send_from_directory,
|
||||||
)
|
)
|
||||||
from sqlalchemy.exc import (
|
|
||||||
IntegrityError,
|
|
||||||
InvalidRequestError,
|
|
||||||
)
|
|
||||||
from flask_login import (
|
from flask_login import (
|
||||||
logout_user,
|
logout_user,
|
||||||
login_required,
|
login_required,
|
||||||
@ -42,9 +37,14 @@ from selector import SelectUpdateDistribusi
|
|||||||
from selector import DeleteDistribusi
|
from selector import DeleteDistribusi
|
||||||
from selector import SelectorVisible
|
from selector import SelectorVisible
|
||||||
|
|
||||||
|
# Upload
|
||||||
|
from upload import UploadNewDistribusi
|
||||||
|
from upload import UploadUpdatedFiles
|
||||||
|
|
||||||
from statuspengguna.helper import AreFilesUploaded
|
# UserPengguna
|
||||||
|
from statuspengguna.helper import IsZipUploaded
|
||||||
from statuspengguna.helper import CurrentDistribusi
|
from statuspengguna.helper import CurrentDistribusi
|
||||||
|
from statuspengguna.helper import ResetUserState
|
||||||
from statuspengguna.loginuser import LoginUser
|
from statuspengguna.loginuser import LoginUser
|
||||||
from statuspengguna.registeruser import RegisterUser
|
from statuspengguna.registeruser import RegisterUser
|
||||||
|
|
||||||
@ -63,6 +63,7 @@ def session_handler():
|
|||||||
|
|
||||||
@APP.route("/")
|
@APP.route("/")
|
||||||
def index():
|
def index():
|
||||||
|
ResetUserState()
|
||||||
distribusis = Distribusis.query.filter(
|
distribusis = Distribusis.query.filter(
|
||||||
Distribusis.distribusiname.isnot(None)
|
Distribusis.distribusiname.isnot(None)
|
||||||
).all()
|
).all()
|
||||||
@ -82,10 +83,9 @@ def distribusi():
|
|||||||
selectorform = SelectorForm()
|
selectorform = SelectorForm()
|
||||||
selectorform.distribusis.choices = distribusisfields()
|
selectorform.distribusis.choices = distribusisfields()
|
||||||
|
|
||||||
files_uploaded = AreFilesUploaded()
|
|
||||||
selectorvisible = SelectorVisible()
|
selectorvisible = SelectorVisible()
|
||||||
current_distribusi = CurrentDistribusi()
|
current_distribusi = CurrentDistribusi()
|
||||||
|
files_uploaded = IsZipUploaded(current_distribusi)
|
||||||
user = User.query.filter_by(email=current_user.email).first()
|
user = User.query.filter_by(email=current_user.email).first()
|
||||||
distribusi = Distribusis.query.filter_by(
|
distribusi = Distribusis.query.filter_by(
|
||||||
distribusiname=current_distribusi
|
distribusiname=current_distribusi
|
||||||
@ -130,53 +130,20 @@ def distribusi():
|
|||||||
@APP.route("/upload", methods=["POST"])
|
@APP.route("/upload", methods=["POST"])
|
||||||
@login_required
|
@login_required
|
||||||
def upload():
|
def upload():
|
||||||
uploadform = UploadForm()
|
|
||||||
distribusiform = DistribusiForm()
|
distribusiform = DistribusiForm()
|
||||||
themeform = ThemeForm()
|
themeform = ThemeForm()
|
||||||
selectorform = SelectorForm()
|
selectorform = SelectorForm()
|
||||||
selectorform.distribusis.choices = distribusisfields()
|
selectorform.distribusis.choices = distribusisfields()
|
||||||
|
|
||||||
files_uploaded = AreFilesUploaded()
|
|
||||||
selectorvisible = SelectorVisible()
|
selectorvisible = SelectorVisible()
|
||||||
|
|
||||||
if uploadform.validate_on_submit():
|
current_distribusi = CurrentDistribusi()
|
||||||
user = User.query.filter_by(email=current_user.email).first()
|
if current_distribusi == "new":
|
||||||
|
uploadform = UploadNewDistribusi(APP.config["UPLOAD_FOLDER"])
|
||||||
|
else:
|
||||||
|
uploadform = UploadUpdatedFiles(APP.config["UPLOAD_FOLDER"])
|
||||||
|
files_uploaded = IsZipUploaded(uploadform.sitename.data)
|
||||||
|
|
||||||
try:
|
|
||||||
newdistribusi = Distribusis(
|
|
||||||
distribusiname=uploadform.sitename.data,
|
|
||||||
userid=user.id,
|
|
||||||
term=uploadform.term.data,
|
|
||||||
course=uploadform.course.data,
|
|
||||||
year=uploadform.academicyear.data,
|
|
||||||
tags=uploadform.tags.data,
|
|
||||||
)
|
|
||||||
user.currentdistribusi = uploadform.sitename.data
|
|
||||||
db.session.add(newdistribusi)
|
|
||||||
db.session.commit()
|
|
||||||
except InvalidRequestError:
|
|
||||||
db.session.rollback()
|
|
||||||
uploadform.sitename.errors.append("Something went wrong!")
|
|
||||||
flash("Something went wrong!", "danger")
|
|
||||||
except IntegrityError:
|
|
||||||
db.session.rollback()
|
|
||||||
uploadform.sitename.errors.append(
|
|
||||||
"distribusi name already exists!"
|
|
||||||
)
|
|
||||||
flash("distribusi name already exists!", "warning")
|
|
||||||
|
|
||||||
zipfilename = "{}.zip".format(newdistribusi.distribusiname)
|
|
||||||
zipfile = uploadform.zipfile.data
|
|
||||||
zipfile.save(os.path.join(APP.config["UPLOAD_FOLDER"], zipfilename))
|
|
||||||
|
|
||||||
newuserfolder = os.path.join("stash", newdistribusi.distribusiname)
|
|
||||||
if not os.path.exists(newuserfolder):
|
|
||||||
os.mkdir(newuserfolder)
|
|
||||||
|
|
||||||
copyzipfile = os.path.join(APP.config["UPLOAD_FOLDER"], zipfilename)
|
|
||||||
shutil.copy(copyzipfile, newuserfolder)
|
|
||||||
os.remove(os.path.join(APP.config["UPLOAD_FOLDER"], zipfilename))
|
|
||||||
files_uploaded = AreFilesUploaded()
|
|
||||||
template = render_template(
|
template = render_template(
|
||||||
"distribusi.html",
|
"distribusi.html",
|
||||||
uploadform=uploadform,
|
uploadform=uploadform,
|
||||||
@ -198,9 +165,9 @@ def theme():
|
|||||||
selectorform = SelectorForm()
|
selectorform = SelectorForm()
|
||||||
selectorform.distribusis.choices = distribusisfields()
|
selectorform.distribusis.choices = distribusisfields()
|
||||||
|
|
||||||
files_uploaded = AreFilesUploaded()
|
|
||||||
selectorvisible = SelectorVisible()
|
selectorvisible = SelectorVisible()
|
||||||
current_distribusi = CurrentDistribusi()
|
current_distribusi = CurrentDistribusi()
|
||||||
|
files_uploaded = IsZipUploaded(current_distribusi)
|
||||||
|
|
||||||
if themeform.validate_on_submit():
|
if themeform.validate_on_submit():
|
||||||
newuserfolder = os.path.join("stash", current_distribusi)
|
newuserfolder = os.path.join("stash", current_distribusi)
|
||||||
@ -226,9 +193,8 @@ def theme():
|
|||||||
@login_required
|
@login_required
|
||||||
def editor():
|
def editor():
|
||||||
editorform = EditorForm()
|
editorform = EditorForm()
|
||||||
files_uploaded = AreFilesUploaded()
|
|
||||||
current_distribusi = CurrentDistribusi()
|
current_distribusi = CurrentDistribusi()
|
||||||
|
files_uploaded = IsZipUploaded(current_distribusi)
|
||||||
if editorform.validate_on_submit():
|
if editorform.validate_on_submit():
|
||||||
userfolder = os.path.join("stash", current_distribusi)
|
userfolder = os.path.join("stash", current_distribusi)
|
||||||
cssfilename = "{}.css".format(editorform.cssname.data)
|
cssfilename = "{}.css".format(editorform.cssname.data)
|
||||||
@ -253,17 +219,31 @@ def selector():
|
|||||||
themeform = ThemeForm()
|
themeform = ThemeForm()
|
||||||
selectorform = SelectorForm()
|
selectorform = SelectorForm()
|
||||||
selectorform.distribusis.choices = distribusisfields()
|
selectorform.distribusis.choices = distribusisfields()
|
||||||
|
current_distribusi = CurrentDistribusi()
|
||||||
files_uploaded = AreFilesUploaded()
|
|
||||||
selectorvisible = SelectorVisible()
|
|
||||||
|
|
||||||
if selectorform.validate_on_submit():
|
if selectorform.validate_on_submit():
|
||||||
if selectorform.new.data:
|
if selectorform.new.data:
|
||||||
SelectNewDistribusi()
|
selectorform = SelectNewDistribusi()
|
||||||
if selectorform.delete.data:
|
if selectorform.delete.data:
|
||||||
DeleteDistribusi(selectorform.distribusis.data)
|
selectorform = DeleteDistribusi(selectorform.distribusis.data)
|
||||||
|
selectorform.distribusis.choices = distribusisfields()
|
||||||
if selectorform.update.data:
|
if selectorform.update.data:
|
||||||
SelectUpdateDistribusi(selectorform.distribusis.data)
|
selectorform = SelectUpdateDistribusi(
|
||||||
|
selectorform.distribusis.data
|
||||||
|
)
|
||||||
|
current_distribusi = CurrentDistribusi()
|
||||||
|
distribusi = Distribusis.query.filter_by(
|
||||||
|
distribusiname=current_distribusi
|
||||||
|
).first()
|
||||||
|
uploadform.sitename.data = distribusi.distribusiname
|
||||||
|
uploadform.sitename.render_kw = {"readonly": True}
|
||||||
|
uploadform.term.data = distribusi.term
|
||||||
|
uploadform.course.data = distribusi.course
|
||||||
|
uploadform.academicyear.data = distribusi.year
|
||||||
|
uploadform.tags.data = distribusi.tags
|
||||||
|
|
||||||
|
files_uploaded = IsZipUploaded(current_distribusi)
|
||||||
|
selectorvisible = SelectorVisible()
|
||||||
|
|
||||||
template = render_template(
|
template = render_template(
|
||||||
"distribusi.html",
|
"distribusi.html",
|
||||||
uploadform=uploadform,
|
uploadform=uploadform,
|
||||||
|
@ -1,19 +1,20 @@
|
|||||||
import os
|
import os
|
||||||
from flask_login import current_user
|
from flask_login import current_user
|
||||||
|
from flask import flash
|
||||||
from usermodel import User
|
from usermodel import User
|
||||||
from distribusimodel import Distribusis
|
from distribusimodel import Distribusis
|
||||||
|
from app import db
|
||||||
|
|
||||||
|
|
||||||
def AreFilesUploaded():
|
def IsZipUploaded(distribusiname):
|
||||||
user = User.query.filter_by(email=current_user.email).first()
|
userfolder = os.path.join("stash", distribusiname)
|
||||||
distribusi = Distribusis.query.filter_by(userid=user.id).first()
|
|
||||||
if distribusi is None:
|
|
||||||
print("distribusi folder is empty")
|
|
||||||
return False
|
|
||||||
userfolder = os.path.join("stash", distribusi.distribusiname)
|
|
||||||
if os.path.exists(userfolder):
|
if os.path.exists(userfolder):
|
||||||
print("folder found, files are uploaded")
|
zipfilename = "{}.zip".format(distribusiname)
|
||||||
return True
|
if os.path.exists(os.path.join(userfolder, zipfilename)):
|
||||||
|
print("folder with zipfile found, file uploaded")
|
||||||
|
return True
|
||||||
|
print("distribusi folder is empty")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def HasDistribusi():
|
def HasDistribusi():
|
||||||
@ -32,7 +33,7 @@ def CurrentDistribusi():
|
|||||||
user = User.query.filter_by(email=current_user.email).first()
|
user = User.query.filter_by(email=current_user.email).first()
|
||||||
if user.currentdistribusi is None:
|
if user.currentdistribusi is None:
|
||||||
return "None"
|
return "None"
|
||||||
if user.currentdistribusi is "new":
|
if user.currentdistribusi == "new":
|
||||||
return "new"
|
return "new"
|
||||||
return user.currentdistribusi
|
return user.currentdistribusi
|
||||||
|
|
||||||
@ -43,6 +44,19 @@ def DistribusiSelected():
|
|||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def ResetUserState():
|
||||||
|
"""reset user state upon visiting index, so that distribusi workflow can
|
||||||
|
be done correctly"""
|
||||||
|
try:
|
||||||
|
user = User.query.filter_by(email=current_user.email).first()
|
||||||
|
user.currentdistribusi = None
|
||||||
|
db.session.commit()
|
||||||
|
except:
|
||||||
|
db.session.rollback()
|
||||||
|
flash("An error occured !", "danger")
|
||||||
|
|
||||||
|
|
||||||
# def IsThemeSelected
|
# def IsThemeSelected
|
||||||
# def IsCustomThemePresent
|
# def IsCustomThemePresent
|
||||||
# def IsDistribusiLive
|
# def IsDistribusiLive
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<div id="distribusi" class="workflow">
|
<div id="distribusi" class="workflow">
|
||||||
<h3>Welcome back to your Distribusi</h3>
|
<h2>Welcome back to your Distribusi</h2>
|
||||||
<p>You have already uploaded a distribusi website, do you want to make a new one, update or delete?</p>
|
<p>You have already uploaded a distribusi website, do you want to make a new one, update or delete?</p>
|
||||||
<form method="POST" enctype="multipart/form-data" action="{{ url_for('selector') }}">
|
<form method="POST" enctype="multipart/form-data" action="{{ url_for('selector') }}">
|
||||||
{{ selectorform.csrf_token }}
|
{{ selectorform.csrf_token }}
|
||||||
@ -13,13 +13,15 @@
|
|||||||
</div>
|
</div>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
<fieldset class="button required multiselect">
|
<fieldset class="button required multiselect">
|
||||||
{{ selectorform.new }}
|
|
||||||
</fieldset>
|
|
||||||
<fieldset class="button required multiselect">
|
|
||||||
{{ selectorform.update }}
|
{{ selectorform.update }}
|
||||||
</fieldset>
|
</fieldset>
|
||||||
<fieldset class="button required multiselect warning">
|
<fieldset class="button required multiselect warning">
|
||||||
{{ selectorform.delete }}
|
{{ selectorform.delete }}
|
||||||
</fieldset>
|
</fieldset>
|
||||||
|
|
||||||
|
<p>Alternatively you can make a new site</p>
|
||||||
|
<fieldset class="button required multiselect">
|
||||||
|
{{ selectorform.new }}
|
||||||
|
</fieldset>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
88
verse/upload.py
Normal file
88
verse/upload.py
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
from flask import flash
|
||||||
|
from sqlalchemy.exc import (
|
||||||
|
IntegrityError,
|
||||||
|
InvalidRequestError,
|
||||||
|
)
|
||||||
|
from usermodel import User
|
||||||
|
from distribusimodel import Distribusis
|
||||||
|
from flask_login import current_user
|
||||||
|
from statuspengguna.helper import CurrentDistribusi
|
||||||
|
from forms.uploadform import UploadForm
|
||||||
|
from app import db
|
||||||
|
|
||||||
|
|
||||||
|
def UploadNewDistribusi(uploadfolder):
|
||||||
|
uploadform = UploadForm()
|
||||||
|
if uploadform.validate_on_submit():
|
||||||
|
user = User.query.filter_by(email=current_user.email).first()
|
||||||
|
try:
|
||||||
|
newdistribusi = Distribusis(
|
||||||
|
distribusiname=uploadform.sitename.data,
|
||||||
|
userid=user.id,
|
||||||
|
term=uploadform.term.data,
|
||||||
|
course=uploadform.course.data,
|
||||||
|
year=uploadform.academicyear.data,
|
||||||
|
tags=uploadform.tags.data,
|
||||||
|
)
|
||||||
|
user.currentdistribusi = uploadform.sitename.data
|
||||||
|
db.session.add(newdistribusi)
|
||||||
|
db.session.commit()
|
||||||
|
except InvalidRequestError:
|
||||||
|
db.session.rollback()
|
||||||
|
uploadform.sitename.errors.append("Something went wrong!")
|
||||||
|
flash("Something went wrong!", "danger")
|
||||||
|
except IntegrityError:
|
||||||
|
db.session.rollback()
|
||||||
|
uploadform.sitename.errors.append(
|
||||||
|
"distribusi name already exists!"
|
||||||
|
)
|
||||||
|
flash("distribusi name already exists!", "warning")
|
||||||
|
|
||||||
|
zipfilename = "{}.zip".format(newdistribusi.distribusiname)
|
||||||
|
zipfile = uploadform.zipfile.data
|
||||||
|
zipfile.save(os.path.join(uploadfolder, zipfilename))
|
||||||
|
|
||||||
|
newuserfolder = os.path.join("stash", newdistribusi.distribusiname)
|
||||||
|
if not os.path.exists(newuserfolder):
|
||||||
|
os.mkdir(newuserfolder)
|
||||||
|
|
||||||
|
copyzipfile = os.path.join(uploadfolder, zipfilename)
|
||||||
|
shutil.copy(copyzipfile, newuserfolder)
|
||||||
|
os.remove(os.path.join(uploadfolder, zipfilename))
|
||||||
|
|
||||||
|
return uploadform
|
||||||
|
|
||||||
|
|
||||||
|
def UploadUpdatedFiles(uploadfolder):
|
||||||
|
uploadform = UploadForm()
|
||||||
|
if uploadform.validate_on_submit():
|
||||||
|
try:
|
||||||
|
current_distribusi = CurrentDistribusi()
|
||||||
|
distribusi = Distribusis.query.filter_by(
|
||||||
|
distribusiname=current_distribusi
|
||||||
|
).first()
|
||||||
|
distribusi.term = uploadform.term.data
|
||||||
|
distribusi.course = uploadform.course.data
|
||||||
|
distribusi.year = uploadform.academicyear.data
|
||||||
|
distribusi.tags = uploadform.tags.data
|
||||||
|
db.session.commit()
|
||||||
|
except:
|
||||||
|
db.session.rollback()
|
||||||
|
uploadform.sitename.errors.append("Something went wrong!")
|
||||||
|
flash("Something went wrong!", "danger")
|
||||||
|
|
||||||
|
zipfilename = "{}.zip".format(distribusi.distribusiname)
|
||||||
|
zipfile = uploadform.zipfile.data
|
||||||
|
zipfile.save(os.path.join(uploadfolder, zipfilename))
|
||||||
|
|
||||||
|
newuserfolder = os.path.join("stash", distribusi.distribusiname)
|
||||||
|
shutil.rmtree(newuserfolder)
|
||||||
|
os.mkdir(newuserfolder)
|
||||||
|
|
||||||
|
copyzipfile = os.path.join(uploadfolder, zipfilename)
|
||||||
|
shutil.copy(copyzipfile, newuserfolder)
|
||||||
|
os.remove(os.path.join(uploadfolder, zipfilename))
|
||||||
|
|
||||||
|
return uploadform
|
Loading…
Reference in New Issue
Block a user