distribusi-verse/verse/upload.py

94 lines
3.3 KiB
Python
Raw Normal View History

2022-03-04 19:08:55 +01:00
import os
import shutil
from flask import flash
2022-03-18 17:32:49 +01:00
from flask_login import current_user
2022-03-04 19:08:55 +01:00
from sqlalchemy.exc import (
IntegrityError,
InvalidRequestError,
DataError,
InterfaceError,
DatabaseError,
2022-03-04 19:08:55 +01:00
)
2022-03-18 17:32:49 +01:00
from app import db
2022-03-04 19:08:55 +01:00
from usermodel import User
from distribusimodel import Distribusis
2022-03-18 17:32:49 +01:00
2022-03-04 19:08:55 +01:00
from statuspengguna.helper import CurrentDistribusi
2022-03-19 16:19:51 +01:00
from distribusiselector import SelectCurrentDistribusi
2022-03-04 19:08:55 +01:00
from forms.uploadform import UploadForm
2022-03-18 17:32:49 +01:00
2022-03-04 19:08:55 +01:00
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, DataError, InterfaceError, DatabaseError):
2022-03-04 19:08:55 +01:00
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")
2022-03-18 17:32:49 +01:00
SelectCurrentDistribusi(newdistribusi.distribusiname)
2022-03-04 19:08:55 +01:00
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 (InvalidRequestError, DataError, InterfaceError, DatabaseError):
2022-03-04 19:08:55 +01:00
db.session.rollback()
uploadform.sitename.errors.append("Something went wrong!")
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