forked from crunk/distribusi-verse
100 lines
3.6 KiB
Python
100 lines
3.6 KiB
Python
import os
|
|
import shutil
|
|
|
|
from flask import flash
|
|
from flask_login import current_user
|
|
from sqlalchemy.exc import (
|
|
DatabaseError,
|
|
DataError,
|
|
IntegrityError,
|
|
InterfaceError,
|
|
InvalidRequestError,
|
|
)
|
|
|
|
from app import db
|
|
from distribusikan.distribusi_selector import select_current_distribusi
|
|
from distribusikan.forms.uploadform import UploadForm
|
|
from models.distribusi_model import Distribusis
|
|
from models.user_model import User
|
|
from statuspengguna.helper import UserHelper
|
|
|
|
|
|
def upload_new_distribusi(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,
|
|
category=uploadform.category.data,
|
|
description=uploadform.description.data,
|
|
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
|
|
select_current_distribusi(newdistribusi.distribusiname)
|
|
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 upload_updates_files(uploadfolder):
|
|
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
|
|
distribusi.description = (uploadform.description.data,)
|
|
distribusi.year = uploadform.year.data
|
|
distribusi.tags = uploadform.tags.data
|
|
distribusi.visible = False
|
|
db.session.commit()
|
|
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
|
|
db.session.rollback()
|
|
uploadform.sitename.errors.append(
|
|
"Something went wrong with the database!"
|
|
)
|
|
|
|
zipfilename = "{}.zip".format(distribusi.distribusiname)
|
|
zipfile = uploadform.zipfile.data
|
|
zipfile.save(os.path.join(uploadfolder, zipfilename))
|
|
|
|
newuserfolder = os.path.join("stash", distribusi.distribusiname)
|
|
if os.path.exists(newuserfolder):
|
|
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
|