2022-03-04 19:08:55 +01:00
|
|
|
import os
|
|
|
|
import shutil
|
2024-04-21 20:44:08 +02:00
|
|
|
|
2024-04-24 23:05:36 +02:00
|
|
|
from flask import flash
|
|
|
|
from flask_login import current_user
|
2024-04-28 13:15:23 +02:00
|
|
|
from sqlalchemy.exc import (
|
|
|
|
DatabaseError,
|
|
|
|
DataError,
|
|
|
|
IntegrityError,
|
|
|
|
InterfaceError,
|
|
|
|
InvalidRequestError,
|
|
|
|
)
|
2024-04-28 13:04:07 +02:00
|
|
|
|
|
|
|
from app import db
|
|
|
|
from distribusikan.distribusiselector import SelectCurrentDistribusi
|
2022-03-04 19:08:55 +01:00
|
|
|
from forms.uploadform import UploadForm
|
2024-04-28 15:34:29 +02:00
|
|
|
from models.distribusi_model import Distribusis
|
|
|
|
from models.user_model import User
|
2024-04-21 20:44:08 +02:00
|
|
|
from statuspengguna.helper import UserHelper
|
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,
|
2024-04-21 22:09:44 +02:00
|
|
|
category=uploadform.category.data,
|
|
|
|
year=uploadform.year.data,
|
2022-03-04 19:08:55 +01:00
|
|
|
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")
|
2022-03-23 21:57:28 +01:00
|
|
|
return uploadform
|
|
|
|
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
|
|
|
|
db.session.rollback()
|
|
|
|
uploadform.sitename.errors.append("Something went wrong!")
|
|
|
|
flash("Something went wrong!", "danger")
|
|
|
|
return uploadform
|
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:
|
2022-03-28 20:16:34 +02:00
|
|
|
current_distribusi = UserHelper.current_distribusi()
|
2022-03-04 19:08:55 +01:00
|
|
|
distribusi = Distribusis.query.filter_by(
|
|
|
|
distribusiname=current_distribusi
|
|
|
|
).first()
|
2024-04-21 22:09:44 +02:00
|
|
|
distribusi.category = uploadform.category.data
|
|
|
|
distribusi.year = uploadform.year.data
|
2022-03-04 19:08:55 +01:00
|
|
|
distribusi.tags = uploadform.tags.data
|
2022-03-19 19:48:56 +01:00
|
|
|
distribusi.visible = False
|
2022-03-04 19:08:55 +01:00
|
|
|
db.session.commit()
|
2022-03-19 14:24:12 +01:00
|
|
|
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)
|
2022-04-04 21:48:52 +02:00
|
|
|
if os.path.exists(newuserfolder):
|
|
|
|
shutil.rmtree(newuserfolder)
|
2022-03-04 19:08:55 +01:00
|
|
|
os.mkdir(newuserfolder)
|
|
|
|
|
|
|
|
copyzipfile = os.path.join(uploadfolder, zipfilename)
|
|
|
|
shutil.copy(copyzipfile, newuserfolder)
|
|
|
|
os.remove(os.path.join(uploadfolder, zipfilename))
|
|
|
|
|
|
|
|
return uploadform
|