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 (
|
2024-05-28 23:02:17 +02:00
|
|
|
DatabaseError,
|
|
|
|
DataError,
|
|
|
|
IntegrityError,
|
|
|
|
InterfaceError,
|
|
|
|
InvalidRequestError,
|
2024-04-28 13:15:23 +02:00
|
|
|
)
|
2024-04-28 13:04:07 +02:00
|
|
|
|
|
|
|
from app import db
|
2024-06-17 22:36:23 +02:00
|
|
|
from distribusikan.distribusi_selector import select_current_distribusi
|
2024-06-02 22:14:13 +02:00
|
|
|
from distribusikan.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
|
|
|
|
2024-06-17 22:36:23 +02:00
|
|
|
def upload_new_distribusi(uploadfolder):
|
2024-05-28 23:02:17 +02:00
|
|
|
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,
|
|
|
|
category=uploadform.category.data,
|
2024-06-17 22:36:23 +02:00
|
|
|
description=uploadform.description.data,
|
2024-05-28 23:02:17 +02:00
|
|
|
year=uploadform.year.data,
|
|
|
|
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")
|
|
|
|
return uploadform
|
|
|
|
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
|
|
|
|
db.session.rollback()
|
|
|
|
uploadform.sitename.errors.append("Something went wrong!")
|
|
|
|
flash("Something went wrong!", "danger")
|
|
|
|
return uploadform
|
2024-06-17 22:36:23 +02:00
|
|
|
select_current_distribusi(newdistribusi.distribusiname)
|
2024-05-28 23:02:17 +02:00
|
|
|
zipfilename = "{}.zip".format(newdistribusi.distribusiname)
|
|
|
|
zipfile = uploadform.zipfile.data
|
|
|
|
zipfile.save(os.path.join(uploadfolder, zipfilename))
|
2022-03-04 19:08:55 +01:00
|
|
|
|
2024-05-28 23:02:17 +02:00
|
|
|
newuserfolder = os.path.join("stash", newdistribusi.distribusiname)
|
|
|
|
if not os.path.exists(newuserfolder):
|
|
|
|
os.mkdir(newuserfolder)
|
2022-03-04 19:08:55 +01:00
|
|
|
|
2024-05-28 23:02:17 +02:00
|
|
|
copyzipfile = os.path.join(uploadfolder, zipfilename)
|
|
|
|
shutil.copy(copyzipfile, newuserfolder)
|
|
|
|
os.remove(os.path.join(uploadfolder, zipfilename))
|
2022-03-04 19:08:55 +01:00
|
|
|
|
2024-05-28 23:02:17 +02:00
|
|
|
return uploadform
|
2022-03-04 19:08:55 +01:00
|
|
|
|
|
|
|
|
2024-06-17 22:36:23 +02:00
|
|
|
def upload_updates_files(uploadfolder):
|
2024-05-28 23:02:17 +02:00
|
|
|
uploadform = UploadForm()
|
|
|
|
if uploadform.validate_on_submit():
|
|
|
|
try:
|
|
|
|
current_distribusi = UserHelper.current_distribusi()
|
|
|
|
distribusi = Distribusis.query.filter_by(
|
|
|
|
distribusiname=current_distribusi
|
|
|
|
).first()
|
|
|
|
distribusi.category = uploadform.category.data
|
2024-06-17 22:36:23 +02:00
|
|
|
distribusi.description = (uploadform.description.data,)
|
2024-05-28 23:02:17 +02:00
|
|
|
distribusi.year = uploadform.year.data
|
|
|
|
distribusi.tags = uploadform.tags.data
|
|
|
|
distribusi.visible = False
|
|
|
|
db.session.commit()
|
|
|
|
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
|
|
|
|
db.session.rollback()
|
2024-11-25 21:21:31 +01:00
|
|
|
uploadform.sitename.errors.append("Something went wrong with the database!")
|
2022-03-04 19:08:55 +01:00
|
|
|
|
2024-05-28 23:02:17 +02:00
|
|
|
zipfilename = "{}.zip".format(distribusi.distribusiname)
|
|
|
|
zipfile = uploadform.zipfile.data
|
|
|
|
zipfile.save(os.path.join(uploadfolder, zipfilename))
|
2022-03-04 19:08:55 +01:00
|
|
|
|
2024-05-28 23:02:17 +02:00
|
|
|
newuserfolder = os.path.join("stash", distribusi.distribusiname)
|
|
|
|
if os.path.exists(newuserfolder):
|
|
|
|
shutil.rmtree(newuserfolder)
|
|
|
|
os.mkdir(newuserfolder)
|
2022-03-04 19:08:55 +01:00
|
|
|
|
2024-05-28 23:02:17 +02:00
|
|
|
copyzipfile = os.path.join(uploadfolder, zipfilename)
|
|
|
|
shutil.copy(copyzipfile, newuserfolder)
|
|
|
|
os.remove(os.path.join(uploadfolder, zipfilename))
|
2022-03-04 19:08:55 +01:00
|
|
|
|
2024-05-28 23:02:17 +02:00
|
|
|
return uploadform
|