Added view for archived posts

Added option in the user profile to view their archived posts. Added _archive.html for displaying archived posts.
This commit is contained in:
Jarrett Jackson 2023-03-12 20:42:51 -04:00
parent caf54c8b75
commit 09ab33aeed
6 changed files with 89 additions and 11 deletions

View File

@ -108,10 +108,31 @@ def edit_profile():
return render_template('edit_profile.html', title=_('Edit Profile'),
form=form)
@bp.route('/archive/<post_id>/<post_b>/<post_user>/<post_time>')
@bp.route('/archived/<username>')
@login_required
def archive(post_id, post_b, post_user, post_time):
current_user.archive(post_id, post_b, post_user, post_time)
def view_archive(username):
no_posts = False
user = User.query.filter_by(username=username).first_or_404()
page = request.args.get('page', 1, type=int)
posts = user.archived.order_by(Archive.timestamp.desc()).paginate(
page=page, per_page=current_app.config['POSTS_PER_PAGE'],
error_out=False)
if not current_user.has_archived_posts():
no_posts = True
flash(_('You have no archived posts!'))
next_url = url_for('main.view_archive', username=user.username,
page=posts.next_num) if posts.has_next else None
prev_url = url_for('main.view_archive', username=user.username,
page=posts.prev_num) if posts.has_prev else None
form = EmptyForm()
return render_template('archived_posts.html', user=user, archived=posts.items, none_archived=no_posts,
next_url=next_url, prev_url=prev_url, form=form)
@bp.route('/archive/<post_id>/<post_b>/<user_id>/<post_user>/<post_time>')
@login_required
def archive(post_id, post_b, user_id, post_user, post_time):
current_user.archive(post_id, post_b, user_id, post_user, post_time)
db.session.commit()
flash(_('You have archived %(username)s post!', username=post_user))
return redirect(url_for('main.explore'))
@ -124,6 +145,14 @@ def archive_remove(post_user, post_id):
flash(_('You have removed %(username)s post from your archive!', username=post_user))
return redirect(url_for('main.explore'))
@bp.route('/archived/<post_user>/<post_id>')
@login_required
def archive_remove_user(post_user, post_id):
current_user.archive_remove(post_id)
db.session.commit()
flash(_('You have removed %(username)s post from your archive!', username=post_user))
return redirect(url_for('main.view_archive', username=current_user.username))
@bp.route('/follow/<username>', methods=['POST'])
@login_required

View File

@ -12,6 +12,7 @@ import redis
import rq
from app import db, login
from app.search import add_to_index, remove_from_index, query_index
from datetime import datetime
class SearchableMixin(object):
@ -97,7 +98,8 @@ class User(UserMixin, PaginatedAPIMixin, db.Model):
password_hash = db.Column(db.String(128))
posts = db.relationship('Post', backref='author', lazy='dynamic')
# link archive class to user
archived = db.relationship('Archive', backref='archivee', lazy='dynamic')
archived = db.relationship('Archive', foreign_keys='Archive.archived_by', backref='archivee', lazy='dynamic')
archived_other_user = db.relationship('Archive', foreign_keys='Archive.archived_owner', backref='own', 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)
@ -133,8 +135,9 @@ class User(UserMixin, PaginatedAPIMixin, db.Model):
digest, size)
# Create new achive entry
def archive(self, post_id, post_body, post_user, post_time):
a = Archive(id=post_id, body=post_body, author=post_user, archived_by=self.id)
def archive(self, post_id, post_body, user_id, post_user, post_time):
time_obj = datetime.strptime(post_time, '%Y-%m-%d %H:%M:%S.%f')
a = Archive(id=post_id, body=post_body, author=post_user, archived_by=self.id, archived_owner=user_id, timestamp=time_obj)
db.session.add(a)
print("Archived!")
@ -143,7 +146,10 @@ class User(UserMixin, PaginatedAPIMixin, db.Model):
print("Removed Archive!")
def has_archived_post(self, post_id):
return Archive.query.filter_by(id=post_id).count() > 0
return Archive.query.filter_by(id=post_id, archived_by=self.id).count() > 0
def has_archived_posts(self):
return Archive.query.filter_by(archived_by=self.id).count() > 0
def follow(self, user):
if not self.is_following(user):
@ -269,10 +275,12 @@ class Post(SearchableMixin, db.Model):
# Archive class for archived posts
class Archive(db.Model):
id = db.Column(db.Integer, primary_key=True)
archive_id = db.Column(db.Integer, primary_key=True)
id = db.Column(db.Integer)
body = db.Column(db.String(140))
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
author = db.Column(db.String(50))
archived_owner = db.Column(db.Integer, db.ForeignKey('user.id'))
archived_by = db.Column(db.Integer, db.ForeignKey('user.id'))
language = db.Column(db.String(5))

View File

@ -0,0 +1,27 @@
<table class="table table-hover">
<tr>
<td width="70px">
<a href="{{ url_for('main.user', username=archive.author) }}">
<img src="{{ archive.own.avatar(70) }}" />
</a>
</td>
<td>
{% set user_link %}
<span class="user_popup">
<a href="{{ url_for('main.user', username=archive.author) }}">
{{ archive.author }}
</a>
</span>
{% endset %}
{{ _('%(username)s said %(when)s',
username=user_link, when=moment(archive.timestamp).fromNow()) }}
<br>
<span id="archive{{ archive.id }}">{{ archive.body }}</span>
<br>
{% if archive.author != current_user.username %}
<a href="{{ url_for('main.archive_remove_user', post_user=archive.author, post_id=archive.id) }}">Remove from Archive</a>
{% endif %}
</td>
</tr>
</table>

View File

@ -31,7 +31,7 @@
{% endif %}
{% if post.author.username != current_user.username %}
{% if not current_user.has_archived_post(post.id) %}
<a href="{{ url_for('main.archive', post_id=post.id, post_b=post.body, post_user=post.author.username, post_time=post.timestamp) }}">Archive</a>
<a href="{{ url_for('main.archive', post_id=post.id, user_id=post.author.id ,post_b=post.body, post_user=post.author.username, post_time=post.timestamp) }}">Archive</a>
{% else %}
<a href="{{ url_for('main.archive_remove', post_user=post.author.username, post_id=post.id) }}">Remove from Archive</a>
{% endif %}

View File

@ -0,0 +1,14 @@
{% extends "base.html" %}
{% block app_content %}
{% if none_archived == false %}
<h1>{{ _('Your Archived Posts') }}</h1>
{% endif %}
<div class="row">
<div class="col-md-4">
{% for archive in archived %}
{% include '_archive.html' %}
{% endfor %}
</div>
</div>
{% endblock %}

View File

@ -13,8 +13,9 @@
<p>{{ _('%(count)d followers', count=user.followers.count()) }}, {{ _('%(count)d following', count=user.followed.count()) }}</p>
{% if user == current_user %}
<p><a href="{{ url_for('main.edit_profile') }}">{{ _('Edit your profile') }}</a></p>
<p><a href="{{ url_for('main.view_archive' , username=user.username) }}">{{ _('View Archived Posts') }}</a></p>
{% if not current_user.get_task_in_progress('export_posts') %}
<p><a href="{{ url_for('main.export_posts') }}">{{ _('Export your posts') }}</a></p>
<p><a href="{{ url_for('main.export_posts')}}">{{ _('Export your posts') }}</a></p>
{% endif %}
{% elif not current_user.is_following(user) %}
<p>
@ -34,7 +35,6 @@
{% if user != current_user %}
<p><a href="{{ url_for('main.send_message', recipient=user.username) }}">{{ _('Send private message') }}</a></p>
{% endif %}
</td>
</tr>
</table>