Users can now archive posts

Added option to posts to archive a post.
This commit is contained in:
Jarrett Jackson 2023-03-11 18:17:10 -05:00
parent 3472187155
commit d3bfb13dbf
5 changed files with 45 additions and 6 deletions

View File

@ -7,9 +7,10 @@ from langdetect import detect, LangDetectException
from app import db from app import db
from app.main.forms import EditProfileForm, EmptyForm, PostForm, SearchForm, \ from app.main.forms import EditProfileForm, EmptyForm, PostForm, SearchForm, \
MessageForm 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.translate import translate
from app.main import bp from app.main import bp
from sqlalchemy import select
@bp.before_app_request @bp.before_app_request
@ -19,6 +20,7 @@ def before_request():
db.session.commit() db.session.commit()
g.search_form = SearchForm() g.search_form = SearchForm()
g.locale = str(get_locale()) g.locale = str(get_locale())
db.create_all()
@bp.route('/', methods=['GET', 'POST']) @bp.route('/', methods=['GET', 'POST'])
@ -82,7 +84,6 @@ def user(username):
return render_template('user.html', user=user, posts=posts.items, return render_template('user.html', user=user, posts=posts.items,
next_url=next_url, prev_url=prev_url, form=form) next_url=next_url, prev_url=prev_url, form=form)
@bp.route('/user/<username>/popup') @bp.route('/user/<username>/popup')
@login_required @login_required
def user_popup(username): def user_popup(username):
@ -107,6 +108,15 @@ def edit_profile():
return render_template('edit_profile.html', title=_('Edit Profile'), return render_template('edit_profile.html', title=_('Edit Profile'),
form=form) form=form)
#
@bp.route('/archive/<post_b>/<post_user>/<post_time>')
@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/<username>', methods=['POST']) @bp.route('/follow/<username>', methods=['POST'])
@login_required @login_required

View File

@ -89,12 +89,15 @@ followers = db.Table(
) )
class User(UserMixin, PaginatedAPIMixin, db.Model): class User(UserMixin, PaginatedAPIMixin, db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True) username = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True) email = db.Column(db.String(120), index=True, unique=True)
password_hash = db.Column(db.String(128)) password_hash = db.Column(db.String(128))
posts = db.relationship('Post', backref='author', lazy='dynamic') 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)) about_me = db.Column(db.String(140))
last_seen = db.Column(db.DateTime, default=datetime.utcnow) last_seen = db.Column(db.DateTime, default=datetime.utcnow)
token = db.Column(db.String(32), index=True, unique=True) 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() digest = md5(self.email.lower().encode('utf-8')).hexdigest()
return 'https://www.gravatar.com/avatar/{}?d=identicon&s={}'.format( return 'https://www.gravatar.com/avatar/{}?d=identicon&s={}'.format(
digest, size) 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): def follow(self, user):
if not self.is_following(user): if not self.is_following(user):
self.followed.append(user) self.followed.append(user)
@ -251,6 +260,18 @@ class Post(SearchableMixin, db.Model):
def __repr__(self): def __repr__(self):
return '<Post {}>'.format(self.body) return '<Post {}>'.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 '<Archive {}>'.format(self.body)
class Message(db.Model): class Message(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)

View File

@ -1,4 +1,5 @@
<table class="table table-hover">
<table class="table table-hover">
<tr> <tr>
<td width="70px"> <td width="70px">
<a href="{{ url_for('main.user', username=post.author.username) }}"> <a href="{{ url_for('main.user', username=post.author.username) }}">
@ -17,6 +18,7 @@
username=user_link, when=moment(post.timestamp).fromNow()) }} username=user_link, when=moment(post.timestamp).fromNow()) }}
<br> <br>
<span id="post{{ post.id }}">{{ post.body }}</span> <span id="post{{ post.id }}">{{ post.body }}</span>
{% if post.language and post.language != g.locale %} {% if post.language and post.language != g.locale %}
<br><br> <br><br>
<span id="translation{{ post.id }}"> <span id="translation{{ post.id }}">
@ -27,6 +29,12 @@
'{{ g.locale }}');">{{ _('Translate') }}</a> '{{ g.locale }}');">{{ _('Translate') }}</a>
</span> </span>
{% endif %} {% endif %}
{% if post.author.username != current_user.username %}
<a href="{{ url_for('main.archive', post_b=post.body, post_user=post.author.username, post_time=post.timestamp) }}">Archive</a>
{% endif %}
</td> </td>
</tr> </tr>
</table> </table>

View File

@ -25,7 +25,7 @@
</p> </p>
{% else %} {% else %}
<p> <p>
<form action="{{ url_for('main.unfollow', username=user.username) }}" method="post"> <form action="{{ url_for('main.unfollow', username=user.username) }}">
{{ form.hidden_tag() }} {{ form.hidden_tag() }}
{{ form.submit(value=_('Unfollow'), class_='btn btn-default') }} {{ form.submit(value=_('Unfollow'), class_='btn btn-default') }}
</form> </form>

View File

@ -18,7 +18,7 @@ Flask-Migrate==3.0.1
Flask-Moment==1.0.1 Flask-Moment==1.0.1
Flask-SQLAlchemy==2.5.1 Flask-SQLAlchemy==2.5.1
Flask-WTF==0.15.1 Flask-WTF==0.15.1
greenlet==1.1.0 greenlet==2.0.1
httpie==2.4.0 httpie==2.4.0
idna==2.10 idna==2.10
itsdangerous==2.0.1 itsdangerous==2.0.1