The U and D part of CRUD
This commit is contained in:
parent
ed9e9af4ac
commit
fc4b1a5c75
@ -1,20 +1,11 @@
|
||||
from flask import (
|
||||
render_template,
|
||||
redirect,
|
||||
request,
|
||||
flash,
|
||||
url_for,
|
||||
abort,
|
||||
)
|
||||
|
||||
from usermodel import User
|
||||
import os
|
||||
import shutil
|
||||
from flask import flash
|
||||
from flask_login import current_user
|
||||
|
||||
from forms.loginform import LoginForm
|
||||
from forms.uploadform import UploadForm
|
||||
from forms.distribusiform import DistribusiForm
|
||||
from forms.themeform import ThemeForm
|
||||
from forms.editorform import EditorForm
|
||||
from usermodel import User
|
||||
from distribusimodel import Distribusis
|
||||
|
||||
from forms.selectorform import SelectorForm
|
||||
|
||||
from statuspengguna.helper import HasDistribusi
|
||||
@ -25,30 +16,50 @@ from app import db
|
||||
|
||||
def SelectNewDistribusi():
|
||||
print("make a new distribusi")
|
||||
selectorform = SelectorForm()
|
||||
user = User.query.filter_by(email=current_user.email).first()
|
||||
try:
|
||||
user.currentdistribusi = "new"
|
||||
db.session.commit()
|
||||
except:
|
||||
db.session.rollback()
|
||||
selectorform.new.errors.append("Unknown error occured!")
|
||||
selectorform.distribusis.errors.append("Unknown error occured!")
|
||||
flash("An error occured !", "danger")
|
||||
return selectorform
|
||||
|
||||
|
||||
def SelectUpdateDistribusi(distribusiname):
|
||||
print(f"Update this distribusi {distribusiname}")
|
||||
selectorform = SelectorForm()
|
||||
user = User.query.filter_by(email=current_user.email).first()
|
||||
try:
|
||||
user.currentdistribusi = distribusiname
|
||||
db.session.commit()
|
||||
except:
|
||||
db.session.rollback()
|
||||
selectorform.new.errors.append("Unknown error occured!")
|
||||
selectorform.distribusis.errors.append("Unknown error occured!")
|
||||
flash("An error occured !", "danger")
|
||||
return selectorform
|
||||
|
||||
|
||||
def DeleteDistribusi(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():
|
||||
|
@ -8,15 +8,10 @@ from datetime import timedelta
|
||||
from flask import (
|
||||
render_template,
|
||||
redirect,
|
||||
flash,
|
||||
url_for,
|
||||
session,
|
||||
send_from_directory,
|
||||
)
|
||||
from sqlalchemy.exc import (
|
||||
IntegrityError,
|
||||
InvalidRequestError,
|
||||
)
|
||||
from flask_login import (
|
||||
logout_user,
|
||||
login_required,
|
||||
@ -42,9 +37,14 @@ from selector import SelectUpdateDistribusi
|
||||
from selector import DeleteDistribusi
|
||||
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 ResetUserState
|
||||
from statuspengguna.loginuser import LoginUser
|
||||
from statuspengguna.registeruser import RegisterUser
|
||||
|
||||
@ -63,6 +63,7 @@ def session_handler():
|
||||
|
||||
@APP.route("/")
|
||||
def index():
|
||||
ResetUserState()
|
||||
distribusis = Distribusis.query.filter(
|
||||
Distribusis.distribusiname.isnot(None)
|
||||
).all()
|
||||
@ -82,10 +83,9 @@ def distribusi():
|
||||
selectorform = SelectorForm()
|
||||
selectorform.distribusis.choices = distribusisfields()
|
||||
|
||||
files_uploaded = AreFilesUploaded()
|
||||
selectorvisible = SelectorVisible()
|
||||
current_distribusi = CurrentDistribusi()
|
||||
|
||||
files_uploaded = IsZipUploaded(current_distribusi)
|
||||
user = User.query.filter_by(email=current_user.email).first()
|
||||
distribusi = Distribusis.query.filter_by(
|
||||
distribusiname=current_distribusi
|
||||
@ -130,53 +130,20 @@ def distribusi():
|
||||
@APP.route("/upload", methods=["POST"])
|
||||
@login_required
|
||||
def upload():
|
||||
uploadform = UploadForm()
|
||||
distribusiform = DistribusiForm()
|
||||
themeform = ThemeForm()
|
||||
selectorform = SelectorForm()
|
||||
selectorform.distribusis.choices = distribusisfields()
|
||||
|
||||
files_uploaded = AreFilesUploaded()
|
||||
selectorvisible = SelectorVisible()
|
||||
|
||||
if uploadform.validate_on_submit():
|
||||
user = User.query.filter_by(email=current_user.email).first()
|
||||
current_distribusi = CurrentDistribusi()
|
||||
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(
|
||||
"distribusi.html",
|
||||
uploadform=uploadform,
|
||||
@ -198,9 +165,9 @@ def theme():
|
||||
selectorform = SelectorForm()
|
||||
selectorform.distribusis.choices = distribusisfields()
|
||||
|
||||
files_uploaded = AreFilesUploaded()
|
||||
selectorvisible = SelectorVisible()
|
||||
current_distribusi = CurrentDistribusi()
|
||||
files_uploaded = IsZipUploaded(current_distribusi)
|
||||
|
||||
if themeform.validate_on_submit():
|
||||
newuserfolder = os.path.join("stash", current_distribusi)
|
||||
@ -226,9 +193,8 @@ def theme():
|
||||
@login_required
|
||||
def editor():
|
||||
editorform = EditorForm()
|
||||
files_uploaded = AreFilesUploaded()
|
||||
current_distribusi = CurrentDistribusi()
|
||||
|
||||
files_uploaded = IsZipUploaded(current_distribusi)
|
||||
if editorform.validate_on_submit():
|
||||
userfolder = os.path.join("stash", current_distribusi)
|
||||
cssfilename = "{}.css".format(editorform.cssname.data)
|
||||
@ -253,17 +219,31 @@ def selector():
|
||||
themeform = ThemeForm()
|
||||
selectorform = SelectorForm()
|
||||
selectorform.distribusis.choices = distribusisfields()
|
||||
|
||||
files_uploaded = AreFilesUploaded()
|
||||
selectorvisible = SelectorVisible()
|
||||
|
||||
current_distribusi = CurrentDistribusi()
|
||||
if selectorform.validate_on_submit():
|
||||
if selectorform.new.data:
|
||||
SelectNewDistribusi()
|
||||
selectorform = SelectNewDistribusi()
|
||||
if selectorform.delete.data:
|
||||
DeleteDistribusi(selectorform.distribusis.data)
|
||||
selectorform = DeleteDistribusi(selectorform.distribusis.data)
|
||||
selectorform.distribusis.choices = distribusisfields()
|
||||
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(
|
||||
"distribusi.html",
|
||||
uploadform=uploadform,
|
||||
|
@ -1,19 +1,20 @@
|
||||
import os
|
||||
from flask_login import current_user
|
||||
from flask import flash
|
||||
from usermodel import User
|
||||
from distribusimodel import Distribusis
|
||||
from app import db
|
||||
|
||||
|
||||
def AreFilesUploaded():
|
||||
user = User.query.filter_by(email=current_user.email).first()
|
||||
distribusi = Distribusis.query.filter_by(userid=user.id).first()
|
||||
if distribusi is None:
|
||||
def IsZipUploaded(distribusiname):
|
||||
userfolder = os.path.join("stash", distribusiname)
|
||||
if os.path.exists(userfolder):
|
||||
zipfilename = "{}.zip".format(distribusiname)
|
||||
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
|
||||
userfolder = os.path.join("stash", distribusi.distribusiname)
|
||||
if os.path.exists(userfolder):
|
||||
print("folder found, files are uploaded")
|
||||
return True
|
||||
|
||||
|
||||
def HasDistribusi():
|
||||
@ -32,7 +33,7 @@ def CurrentDistribusi():
|
||||
user = User.query.filter_by(email=current_user.email).first()
|
||||
if user.currentdistribusi is None:
|
||||
return "None"
|
||||
if user.currentdistribusi is "new":
|
||||
if user.currentdistribusi == "new":
|
||||
return "new"
|
||||
return user.currentdistribusi
|
||||
|
||||
@ -43,6 +44,19 @@ def DistribusiSelected():
|
||||
return False
|
||||
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 IsCustomThemePresent
|
||||
# def IsDistribusiLive
|
||||
|
@ -1,5 +1,5 @@
|
||||
<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>
|
||||
<form method="POST" enctype="multipart/form-data" action="{{ url_for('selector') }}">
|
||||
{{ selectorform.csrf_token }}
|
||||
@ -13,13 +13,15 @@
|
||||
</div>
|
||||
</fieldset>
|
||||
<fieldset class="button required multiselect">
|
||||
{{ selectorform.new }}
|
||||
</fieldset>
|
||||
<fieldset class="button required multiselect">
|
||||
{{ selectorform.update }}
|
||||
</fieldset>
|
||||
<fieldset class="button required multiselect warning">
|
||||
{{ selectorform.delete }}
|
||||
</fieldset>
|
||||
|
||||
<p>Alternatively you can make a new site</p>
|
||||
<fieldset class="button required multiselect">
|
||||
{{ selectorform.new }}
|
||||
</fieldset>
|
||||
</form>
|
||||
</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