Skip to content

Commit

Permalink
+ upload page WIP v4
Browse files Browse the repository at this point in the history
  • Loading branch information
lunden23 committed Feb 3, 2022
1 parent 3cd624c commit 3392640
Show file tree
Hide file tree
Showing 13 changed files with 2,575 additions and 79 deletions.
9 changes: 7 additions & 2 deletions .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import os
basedir = os.path.abspath(os.path.dirname(__file__))


class Config(object):
SECRET_KEY = "cloaked"
SQLALCHEMY_DATABASE_URI = "sqlite:///site.db"
SECRET_KEY = os.environ.get("SECRET_KEY") or "cloaked"
SQLALCHEMY_DATABASE_URI = os.environ.get("DATABASE_URL") or "sqlite:///" + os.path.join(basedir, "site.db")
SQLALCHEMY_TRACK_MODIFICATIONS = False
MAX_CONTENT_LENGTH = 50 * 1024 * 1024
# MAX_DOWNLOAD_SIZE = 50 * 1024 * 1024
# MAX_UPLOAD_SIZE = 40 * 1024 * 1024
7 changes: 4 additions & 3 deletions skanhama/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
from config import Config

app = Flask(__name__)
app.config["SECRET_KEY"] = "cloaked"
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///site.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
login_manager = LoginManager(app)
login_manager.login_view = "login"
login_manager.login_message_category = "flash flash-fail"
Expand Down
12 changes: 9 additions & 3 deletions skanhama/forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed, FileSize
from flask_login import current_user
from wtforms import StringField, PasswordField, SubmitField, BooleanField, TextAreaField, SelectField
from wtforms import StringField, PasswordField, SubmitField, BooleanField, TextAreaField, SelectField, MultipleFileField
from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError
from skanhama.models import User

Expand All @@ -21,7 +21,7 @@ def validate_username(self, username):
def validate_email(self, email):
user = User.query.filter_by(email=email.data).first()
if user:
raise ValidationError("Unable to create account. Please ensure you have submitted correct information.")
raise ValidationError("An account with that email address already exists.")


class LoginForm(FlaskForm):
Expand Down Expand Up @@ -56,13 +56,19 @@ def validate_email(self, email):


class UploadPackage(FlaskForm):
# TODO: add base game as input
name = StringField("Package Name", validators=[DataRequired()])
version = StringField("Current Version", validators=[DataRequired(), Length(min=1, max=30)])
author = StringField("Author(s) or Team", validators=[DataRequired()])
summary = TextAreaField("Brief Description", validators=[DataRequired(), Length(min=1, max=230)])
description = TextAreaField("Description", validators=[DataRequired(), Length(min=1, max=5000)])
requirements = TextAreaField("Requirements", validators=[DataRequired()])
category = SelectField("Primary Category", choices=["Combat", "Sex"], validators=[DataRequired()])
game = SelectField("Game Version", choices=[(1, "Skyrim"), (2, "Skyrim Special Edition"),
(3, "Skyrim Anniversary Edition")],
validators=[DataRequired()], default=2)
nsfw = BooleanField("NSFW Content")
package = FileField("Select Package", validators=[FileAllowed(["zip"]), FileSize(max_size=20*1024*1024)])
package = FileField("Select Package", validators=[FileAllowed(["zip"]), FileSize(max_size=50*1024*1024)])
banner = FileField("Select Banner Image", validators=[FileAllowed(["jpg", "gif", "png"]), FileSize(max_size=4*1024*1024)])
gallery = MultipleFileField("Add Gallery Images", validators=[FileAllowed(["jpg", "gif", "png"]), FileSize(max_size=10*1024*1024)])
upload = SubmitField("Upload")
21 changes: 13 additions & 8 deletions skanhama/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,38 @@ def load_user(user_id):

class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(23), unique=True, nullable=False)
username = db.Column(db.String(64), unique=True, index=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password = db.Column(db.String(60), nullable=False)
password_hash = db.Column(db.String(128), nullable=False)
registered_on = db.Column(db.DateTime, nullable=False)
confirmed = db.Column(db.Boolean, nullable=False, default=False)
confirmed_on = db.Column(db.DateTime, nullable=True)
admin = db.Column(db.Boolean, nullable=False, default=False)
uploads = db.relationship("Package", backref="upload_author", lazy="dynamic")
comments = db.relationship("Comment", backref="comment_author", lazy=True)
comments = db.relationship("Comment", backref="comment_author", lazy="dynamic")

def __repr__(self):
return f"User ('{self.id}', '{self.username}', '{self.email}', '{self.confirmed_on}, 'Admin={self.admin}')"


class Package(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(120), unique=True, nullable=False)
name = db.Column(db.String(120), unique=True, index=True, nullable=False)
version = db.Column(db.String(30), nullable=False)
author = db.Column(db.String(100), nullable=False)
author = db.Column(db.String(100), index=True, nullable=False)
nsfw = db.Column(db.Boolean, nullable=False)
summary = db.Column(db.String(230), nullable=False)
description = db.Column(db.Text, nullable=False)
requirements = db.Column(db.Text, nullable=False)
package_dir = db.Column(db.String(400), nullable=False, unique=True)
date_uploaded = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
downloads_total = db.Column(db.BigInteger, nullable=False)
downloads_total = db.Column(db.BigInteger, index=True, nullable=False)
downloads_current_version = db.Column(db.BigInteger, nullable=False)
likes = db.Column(db.BigInteger, nullable=False)
likes = db.Column(db.BigInteger, index=True, nullable=False)
views_total = db.Column(db.BigInteger, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
category = db.relationship("Categories", backref="category_id", lazy=True)
comments = db.relationship("Comment", backref="package_comments", lazy=True)
games = db.relationship("Games", backref="games_id", lazy=True)

def __repr__(self):
return f"Package ('{self.date_uploaded}', {self.id}', '{self.name}', '{self.uploaderl}', '{self.synposis}')"
Expand All @@ -64,6 +64,11 @@ def __repr__(self):
return f"Package ('{self.id}', {self.name}')"


class Games(db.Model):
id = db.Column(db.Integer, db.ForeignKey("package.id"), primary_key=True)
name = db.Column(db.String(100), unique=True, nullable=False)


class Comment(db.Model):
id = db.Column(db.Integer, primary_key=True)
comment = db.Column(db.Text, nullable=False)
Expand Down
20 changes: 12 additions & 8 deletions skanhama/routes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import secrets
import os.path
import sqlite3
import zipfile
Expand Down Expand Up @@ -29,8 +28,8 @@ def register():
form = RegistrationForm()
if form.validate_on_submit():
hashed_password = generate_password_hash(form.password.data, "sha256")
user = User(username=form.username.data, email=form.email.data, password=hashed_password,
registered_on=datetime.now(), confirmed=False, admin=False)
user = User(username=form.username.data, email=form.email.data, password_hash=hashed_password,
registered_on=datetime.now(), confirmed=False)
db.session.add(user)
db.session.commit()
flash(f"An email has been sent to the registered email for user {form.username.data}. Please "
Expand All @@ -46,7 +45,7 @@ def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first()
if user and check_password_hash(user.password, form.password.data):
if user and check_password_hash(user.password_hash, form.password.data):
login_user(user, remember=form.remember.data)
next_page = request.args.get("next")
return redirect(next_page) if next_page else redirect(url_for("home"))
Expand Down Expand Up @@ -121,9 +120,10 @@ def browse():


# Processes the uploaded package
#
# TODO: potentially stream the uploaded file first before saving to disk? See
# https://blog.miguelgrinberg.com/post/handling-file-uploads-with-flask
def process_package(form):
processor_version = 0.1
processor_version = "0.10"
f_name, f_ext = os.path.splitext(form.package.data.filename)
root_dir = Path(app.root_path, "static/packages", current_user.username)
extracted_dir = Path(root_dir, str(form.name.data + "_" + form.version.data))
Expand Down Expand Up @@ -165,6 +165,8 @@ def process_package(form):
shutil.rmtree(extracted_dir)
return file_dict

# def secure_validate_hkx(hkx_file):


@app.route("/upload", methods=["GET", "POST"])
@login_required
Expand All @@ -177,8 +179,11 @@ def upload():
if file_dict:
print(f"package_data: {file_dict['_package_data'][1]}")
pack = Package(name=form.name.data,
version=form.version.data,
author=current_user.username,
version=form.version.data,
category=form.category.data,
game=form.category.data,
nsfw=form.nsfw.data,
summary=form.summary.data,
description=form.description.data,
requirements=form.requirements.data,
Expand All @@ -188,7 +193,6 @@ def upload():
downloads_current_version=0,
views_total=0,
likes=0,
nsfw=form.nsfw.data,
user_id=current_user.id)
db.session.add(pack)
db.session.commit()
Expand Down
Binary file modified skanhama/site.db
Binary file not shown.
Loading

0 comments on commit 3392640

Please sign in to comment.