forked from crunk/distribusi-verse
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
3.4 KiB
91 lines
3.4 KiB
3 years ago
|
import os
|
||
|
import shutil
|
||
7 months ago
|
|
||
7 months ago
|
from flask import flash
|
||
|
from flask_login import current_user
|
||
7 months ago
|
from sqlalchemy.exc import (DatabaseError, DataError, IntegrityError,
|
||
|
InterfaceError, InvalidRequestError)
|
||
|
|
||
|
from app import db
|
||
|
from distribusikan.distribusiselector import SelectCurrentDistribusi
|
||
3 years ago
|
from forms.uploadform import UploadForm
|
||
7 months ago
|
from models.distribusimodel import Distribusis
|
||
|
from models.usermodel import User
|
||
|
from statuspengguna.helper import UserHelper
|
||
3 years ago
|
|
||
3 years ago
|
|
||
|
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,
|
||
7 months ago
|
category=uploadform.category.data,
|
||
|
year=uploadform.year.data,
|
||
3 years ago
|
tags=uploadform.tags.data,
|
||
|
)
|
||
|
user.currentdistribusi = uploadform.sitename.data
|
||
|
db.session.add(newdistribusi)
|
||
|
db.session.commit()
|
||
|
except IntegrityError:
|
||
|
db.session.rollback()
|
||
|
uploadform.sitename.errors.append(
|
||
|
"distribusi name already exists!"
|
||
|
)
|
||
|
flash("distribusi name already exists!", "warning")
|
||
3 years ago
|
return uploadform
|
||
|
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
|
||
|
db.session.rollback()
|
||
|
uploadform.sitename.errors.append("Something went wrong!")
|
||
|
flash("Something went wrong!", "danger")
|
||
|
return uploadform
|
||
3 years ago
|
SelectCurrentDistribusi(newdistribusi.distribusiname)
|
||
3 years ago
|
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:
|
||
3 years ago
|
current_distribusi = UserHelper.current_distribusi()
|
||
3 years ago
|
distribusi = Distribusis.query.filter_by(
|
||
|
distribusiname=current_distribusi
|
||
|
).first()
|
||
7 months ago
|
distribusi.category = uploadform.category.data
|
||
|
distribusi.year = uploadform.year.data
|
||
3 years ago
|
distribusi.tags = uploadform.tags.data
|
||
3 years ago
|
distribusi.visible = False
|
||
3 years ago
|
db.session.commit()
|
||
3 years ago
|
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
|
||
3 years ago
|
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)
|
||
3 years ago
|
if os.path.exists(newuserfolder):
|
||
|
shutil.rmtree(newuserfolder)
|
||
3 years ago
|
os.mkdir(newuserfolder)
|
||
|
|
||
|
copyzipfile = os.path.join(uploadfolder, zipfilename)
|
||
|
shutil.copy(copyzipfile, newuserfolder)
|
||
|
os.remove(os.path.join(uploadfolder, zipfilename))
|
||
|
|
||
|
return uploadform
|