From 5022485ff6af7ccb6f665f2c4bd2ab9b32d44044 Mon Sep 17 00:00:00 2001
From: Xevion
Date: Sat, 6 Jul 2019 04:57:26 -0500
Subject: [PATCH] api
---
app/dashboard.py | 9 ++++-
app/forms.py | 10 +++--
app/models.py | 39 ++++++++++++++++++-
app/routes.py | 7 +++-
app/static/robots.txt | 2 +
app/templates/base.html | 2 +-
app/templates/dashboard/dashboard_base.html | 9 +++--
app/templates/dashboard/profile_settings.html | 38 +++++++++++++++---
8 files changed, 98 insertions(+), 18 deletions(-)
create mode 100644 app/static/robots.txt
diff --git a/app/dashboard.py b/app/dashboard.py
index 2484dbe..1f7fcf7 100644
--- a/app/dashboard.py
+++ b/app/dashboard.py
@@ -21,8 +21,13 @@ def profile_settings():
def profile_settings_submit():
form = ProfileSettingsForm()
if form.validate_on_submit():
- return jsonify(data={'message' : 'hello {}'.format(form.show_email.data)})
- return '$'
+ data = {
+ 'show_email' : form.show_email.data or None,
+ 'profile_picture_file' : request.files
+ }
+ return jsonify(data=data)
+ return '{}'
+
@app.route('/dashboard/constants')
@login_required
@require_role(roles=['Admin'])
diff --git a/app/forms.py b/app/forms.py
index 567af3c..7c33ba3 100644
--- a/app/forms.py
+++ b/app/forms.py
@@ -1,6 +1,6 @@
from flask_wtf import FlaskForm
-from wtforms import StringField, PasswordField, BooleanField, SubmitField, RadioField
-from wtforms.validators import ValidationError, DataRequired, EqualTo, Email
+from wtforms import StringField, PasswordField, BooleanField, SubmitField, RadioField, FileField
+from wtforms.validators import ValidationError, DataRequired, EqualTo, Email, URL
from app.models import User
class LoginForm(FlaskForm):
@@ -28,6 +28,10 @@ class RegistrationForm(FlaskForm):
class ProfileSettingsForm(FlaskForm):
show_email = RadioField('Show Email', default='registered', choices=[('public', 'Public'), ('registered', 'Registered Users Only'), ('hidden', 'Hidden')])
- profile_picture_file = FileField('Upload Profile Picture')
+
submit = SubmitField('Save Profile Settings')
+class ProfilePictureForm(FlaskForm):
+ profile_picture_file = FileField('Upload Profile Picture')
+ profile_picture_url = StringField('Use URL for Profile Picture', validators=[URL()])
+ submit = SubmitField('Submit Profile Picture')
\ No newline at end of file
diff --git a/app/models.py b/app/models.py
index ec8f775..d5882bf 100644
--- a/app/models.py
+++ b/app/models.py
@@ -9,11 +9,15 @@ class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
+ register_timestamp = db.Column(db.DateTime, default=datetime.utcnow)
password_hash = db.Column(db.String(64))
posts = db.relationship('Post', backref='author', lazy='dynamic')
search_history = db.relationship('Search', backref='user', lazy='dynamic')
uroles = db.Column(db.String(80), default='')
-
+ about_me = db.Column(db.String(320))
+ last_seen = db.Column(db.DateTime, default=datetime.utcnow)
+ show_email = db.Column
+
def set_password(self, password):
self.password_hash = generate_password_hash(password)
@@ -22,10 +26,36 @@ class User(UserMixin, db.Model):
raise "{} has no password_hash set!".format(self.__repr__())
return check_password_hash(self.password_hash, password)
+ # Retains order while making sure that there are no duplicate role values and they are capitalized
+ def post_role_processing(self):
+ user_roles = self.uroles.split(' ')
+ user_roles = list(dict.fromkeys(user_roles))
+ self.uroles = ' '.join([role.title() for role in user_roles])
+
+ def delete_role(self, role):
+ return self.delete_roles([role])
+
+ # Will return True if successful, else False if a role didn't exist
+ def delete_roles(self, roles, ignore=True):
+ user_roles = self.uroles.split(' ')
+ success = True
+ for role in roles:
+ try:
+ user_roles.remove(role)
+ except ValueError as e:
+ if not ignore:
+ raise e
+ success = False
+ return success
+
+
def get_roles(self):
return self.uroles.split(' ')
- def add_roles(self, roles):
+ def add_role(self, role):
+ self.add_roles([role])
+
+ def add_roles(self, roles, postprocess=True):
user_roles = self.uroles.split(' ')
if type(roles) == str:
user_roles.append(roles)
@@ -33,6 +63,11 @@ class User(UserMixin, db.Model):
user_roles.extend(roles)
user_roles = ' '.join(user_roles)
self.uroles = user_roles
+ if postprocess:
+ self.post_role_processing()
+
+ def has_role(self, role):
+ return self.has_roles([role])
# Input: ['Insane', ['Fortunate', 'Blessed']]
# Meaning: Must have 'Insane' role, as well as 'Fortunate' or 'Blessed' roles.
diff --git a/app/routes.py b/app/routes.py
index 05be6f2..b91f793 100644
--- a/app/routes.py
+++ b/app/routes.py
@@ -15,7 +15,8 @@ import json
fake = faker.Faker()
-def strgen(length): return ''.join(random.choices(list(string.ascii_letters), k=length))
+def strgen(length):
+ return ''.join(random.choices(list(string.ascii_letters), k=length))
@app.errorhandler(401)
def unauthorized(e):
@@ -26,6 +27,10 @@ def unauthorized(e):
def profile():
return render_template('profile.html')
+@app.route('/api/')
+def api():
+ return 'fuckoff bots'
+
@app.route('/userinfo/')
def user_info():
prepare = {
diff --git a/app/static/robots.txt b/app/static/robots.txt
new file mode 100644
index 0000000..77470cb
--- /dev/null
+++ b/app/static/robots.txt
@@ -0,0 +1,2 @@
+User-agent: *
+Disallow: /
\ No newline at end of file
diff --git a/app/templates/base.html b/app/templates/base.html
index a88d0d0..22c22df 100644
--- a/app/templates/base.html
+++ b/app/templates/base.html
@@ -127,7 +127,7 @@ Color = Bulma Color Type of the Message Box
Login
-
+
Register
{% endif %}
diff --git a/app/templates/dashboard/dashboard_base.html b/app/templates/dashboard/dashboard_base.html
index addc00e..0af048e 100644
--- a/app/templates/dashboard/dashboard_base.html
+++ b/app/templates/dashboard/dashboard_base.html
@@ -8,10 +8,12 @@
+
+
+
{% block dashboard_body %}
{% endblock dashboard_body %}
diff --git a/app/templates/dashboard/profile_settings.html b/app/templates/dashboard/profile_settings.html
index 91006a2..4398520 100644
--- a/app/templates/dashboard/profile_settings.html
+++ b/app/templates/dashboard/profile_settings.html
@@ -4,7 +4,7 @@
{{ super() }}
+
{% endblock head %}
{% block dashboard_body %}