From d3bfb13dbfb0ae9077a2e24cfce42d1e1637c69b Mon Sep 17 00:00:00 2001 From: Jarrett Jackson Date: Sat, 11 Mar 2023 18:17:10 -0500 Subject: [PATCH] Users can now archive posts Added option to posts to archive a post. --- app/main/routes.py | 14 ++++++++++++-- app/models.py | 23 ++++++++++++++++++++++- app/templates/_post.html | 10 +++++++++- app/templates/user.html | 2 +- requirements.txt | 2 +- 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/app/main/routes.py b/app/main/routes.py index 2b650bc..9c990ef 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -7,9 +7,10 @@ from langdetect import detect, LangDetectException from app import db from app.main.forms import EditProfileForm, EmptyForm, PostForm, SearchForm, \ MessageForm -from app.models import User, Post, Message, Notification +from app.models import User, Post, Message, Notification, Archive from app.translate import translate from app.main import bp +from sqlalchemy import select @bp.before_app_request @@ -19,6 +20,7 @@ def before_request(): db.session.commit() g.search_form = SearchForm() g.locale = str(get_locale()) + db.create_all() @bp.route('/', methods=['GET', 'POST']) @@ -82,7 +84,6 @@ def user(username): return render_template('user.html', user=user, posts=posts.items, next_url=next_url, prev_url=prev_url, form=form) - @bp.route('/user//popup') @login_required def user_popup(username): @@ -107,6 +108,15 @@ def edit_profile(): return render_template('edit_profile.html', title=_('Edit Profile'), form=form) +# +@bp.route('/archive///') +@login_required +def archive(post_b, post_user, post_time): + current_user.archive(post_b, post_user, post_time) + db.session.commit() + flash(_('You have archived %(username)s post!', username=post_user)) + return redirect(url_for('main.index')) + @bp.route('/follow/', methods=['POST']) @login_required diff --git a/app/models.py b/app/models.py index 5bdab20..33b545f 100644 --- a/app/models.py +++ b/app/models.py @@ -89,12 +89,15 @@ followers = db.Table( ) + class User(UserMixin, PaginatedAPIMixin, 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) password_hash = db.Column(db.String(128)) posts = db.relationship('Post', backref='author', lazy='dynamic') + # link archive class to user + archive = db.relationship('Archive', backref='archivee', lazy='dynamic') about_me = db.Column(db.String(140)) last_seen = db.Column(db.DateTime, default=datetime.utcnow) token = db.Column(db.String(32), index=True, unique=True) @@ -128,7 +131,13 @@ class User(UserMixin, PaginatedAPIMixin, db.Model): digest = md5(self.email.lower().encode('utf-8')).hexdigest() return 'https://www.gravatar.com/avatar/{}?d=identicon&s={}'.format( digest, size) - + + # Create new achive entry + def archive(self, post_body, post_user, post_time): + a = Archive(body=post_body, author=post_user, archived_by=self.id) + db.session.add(a) + print("Archived!") + def follow(self, user): if not self.is_following(user): self.followed.append(user) @@ -251,6 +260,18 @@ class Post(SearchableMixin, db.Model): def __repr__(self): return ''.format(self.body) +# Archive class for archived posts +class Archive(db.Model): + id = db.Column(db.Integer, primary_key=True) + body = db.Column(db.String(140)) + timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) + author = db.Column(db.String(50)) + archived_by = db.Column(db.Integer, db.ForeignKey('user.id')) + language = db.Column(db.String(5)) + + def __repr__(self): + return ''.format(self.body) + class Message(db.Model): id = db.Column(db.Integer, primary_key=True) diff --git a/app/templates/_post.html b/app/templates/_post.html index 631bcb7..616fae6 100644 --- a/app/templates/_post.html +++ b/app/templates/_post.html @@ -1,4 +1,5 @@ - + +
@@ -17,6 +18,7 @@ username=user_link, when=moment(post.timestamp).fromNow()) }}
{{ post.body }} + {% if post.language and post.language != g.locale %}

@@ -27,6 +29,12 @@ '{{ g.locale }}');">{{ _('Translate') }}
{% endif %} + {% if post.author.username != current_user.username %} + Archive + {% endif %}
+ + + diff --git a/app/templates/user.html b/app/templates/user.html index 1d85872..397eb1e 100644 --- a/app/templates/user.html +++ b/app/templates/user.html @@ -25,7 +25,7 @@

{% else %}

-

+ {{ form.hidden_tag() }} {{ form.submit(value=_('Unfollow'), class_='btn btn-default') }}
diff --git a/requirements.txt b/requirements.txt index d27db7e..bed1e03 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,7 +18,7 @@ Flask-Migrate==3.0.1 Flask-Moment==1.0.1 Flask-SQLAlchemy==2.5.1 Flask-WTF==0.15.1 -greenlet==1.1.0 +greenlet==2.0.1 httpie==2.4.0 idna==2.10 itsdangerous==2.0.1