Сделаны лайки

This commit is contained in:
mrmur 2022-04-14 16:57:18 +05:00
parent 39c4475fba
commit 7bd4121354
9 changed files with 55 additions and 87 deletions

View File

@ -1 +1 @@
from . import users, diary_post, questions, answer_quest, like, dislike
from . import users, diary_post, questions, answer_quest, like

View File

@ -1,16 +0,0 @@
import sqlalchemy
from flask_login import UserMixin
from .db_session import SqlAlchemyBase
class Dislike(SqlAlchemyBase, UserMixin):
__tablename__ = 'dislike'
id = sqlalchemy.Column(sqlalchemy.Integer,
primary_key=True, autoincrement=True)
user = sqlalchemy.Column(sqlalchemy.Integer,
sqlalchemy.ForeignKey("users.id"), nullable=True, default=None)
post = sqlalchemy.Column(sqlalchemy.Integer,
sqlalchemy.ForeignKey("posts.id"), nullable=True, default=None)
date = sqlalchemy.Column(sqlalchemy.DateTime, nullable=True, default=None)

Binary file not shown.

49
main.py
View File

@ -11,7 +11,6 @@ from data import db_session
from data.answer_quest import Answer
from data.diary_post import DiaryPost
from data.like import Like
from data.dislike import Dislike
from data.questions import Quest
from data.users import User
from forms.add_question import AddQuest
@ -72,28 +71,22 @@ def main_page():
return render_template('base.html', title='moona')
@app.route('/new_dislike/<int:user_id>/<int:post_id>/<string:ret_href>')
def new_dislike(user_id, post_id, ret_href):
session = db_session.create_session()
dislike = Dislike()
dislike.user = user_id
dislike.post = post_id
dislike.date = datetime.datetime.now()
session.add(dislike)
session.commit()
return redirect(f"/{ret_href}")
@app.route('/new_like/<int:user_id>/<int:post_id>/<string:ret_href>')
def new_like(user_id, post_id, ret_href):
session = db_session.create_session()
like = Like()
like.user = user_id
like.post = post_id
like.date = datetime.datetime.now()
session.add(like)
session.commit()
return redirect(f"/{ret_href}")
find = session.query(Like).filter(Like.post == post_id, Like.user == user_id).first()
if find:
session.delete(find)
session.commit()
return redirect(f"/{ret_href}")
else:
like = Like()
like.user = user_id
like.post = post_id
like.date = datetime.datetime.now()
session.add(like)
session.commit()
return redirect(f"/{ret_href}")
@app.route('/publications', methods=['GET', 'POST'])
@ -103,7 +96,7 @@ def publications():
emotion_fresh = []
for i in fresh_posts:
emotion = {id: i.id, 'pos_emot': [], 'nig_emot': [], 'link': [],
'author': session.query(User).filter(User.id == i.author).first()}
'author': session.query(User).filter(User.id == i.author).first(), 'like': None, 'is_like': 0}
if i.pos_emot:
emotion['pos_emot'] = i.pos_emot.split()
else:
@ -116,6 +109,11 @@ def publications():
emotion['link'] = i.link.split()
else:
emotion['link'] = None
like = session.query(Like).filter(Like.post == i.id).all()
if like:
emotion['like'] = len(like)
if session.query(Like).filter(Like.post == i.id, Like.user == current_user.id).first():
emotion['is_like'] = 1
emotion_fresh.append(emotion)
return render_template('publications.html', fresh_post=fresh_posts, emotion_fresh=emotion_fresh, title='moona')
@ -287,7 +285,7 @@ def diary():
pub_post = pub_post[::-1]
emotion_pub = []
for i in pub_post:
emotion = {id: i.id, 'pos_emot': [], 'nig_emot': [], 'link': [], 'like': None, 'dislike': None}
emotion = {id: i.id, 'pos_emot': [], 'nig_emot': [], 'link': [], 'like': None, 'is_like': 0}
if i.pos_emot:
emotion['pos_emot'] = i.pos_emot.split()
else:
@ -302,10 +300,9 @@ def diary():
emotion['link'] = None
like = db_sess.query(Like).filter(Like.post == i.id).all()
if like:
emotion['like'] = like
dislike = db_sess.query(Like).filter(Like.post == i.id).all()
if like:
emotion['dislike'] = dislike
emotion['like'] = len(like)
if db_sess.query(Like).filter(Like.post == i.id, Like.user == current_user.id).first():
emotion['is_like'] = 1
emotion_pub.append(emotion)
lis_emotion = []
for i in posts:

View File

@ -30,8 +30,7 @@ summary {
-webkit-border-radius: 22px;
-moz-border-radius: 22px;
border-radius: 22px;
border:2px;
solid #ffffff;
border:2px solid #ffffff;
background-color:#1daff0;
top: 98px;
}
@ -130,21 +129,4 @@ table {
width: 50%;
margin-left: auto;
margin-right: auto;
}
.like_block, .dislike_block {
border-radius: 22px;
width: 40px;
height: 40px;
}
.div_like {
background-color: #95e9ff;
width: 50px;
height: 50px;
border-radius: 20px;
}
.div_dislike {
background-color: #df3c43;
width: 50px;
height: 50px;
border-radius: 20px;
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 101 KiB

After

Width:  |  Height:  |  Size: 584 KiB

BIN
static/img/like_add.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 411 KiB

View File

@ -78,27 +78,19 @@
приватная
</div>
{% endif %}
<form>
<table class="like">
<th class="like_block">
<div class="div_like">
<a type="button"
href="/new_like/{{current_user.id}}/{{item.id}}/diary">
<img src="../static/img/like.png" width="50" height="50"
style="margin-left: auto;margin-right: auto;">
</a></div>
</th>
<th class="dislike_block">
<div class="div_dislike">
<a type="button" class="dislike"
href="/new_dislike/{{current_user.id}}/{{item.id}}/diary">
<img src="../static/img/dislike.png" width="50" height="50"
style="margin-left: auto;margin-right: auto;">
</a>
</div>
</th>
</table>
</form>
<div class="like">
<a type="button"
href="/new_like/{{current_user.id}}/{{item.id}}/diary">
{% if emotion_pub[loop.index0]['is_like'] %}
<img src="../static/img/like_add.png" width="117" height="100">
{% else %}
<img src="../static/img/like.png" width="94" height="80">
{% endif %}
</a>
{% if emotion_pub[loop.index0]['like'] != None %}
<p style="color:#ffffff">{{emotion_pub[loop.index0]['like']}}</p>
{% endif %}
</div>
<div class="my_author"><img src="../{{ current_user.photo }}" width="40" height="40"
style="border-radius: 22px">
<strong style="color: #ffffff">{{ current_user.name }}</strong>
@ -198,9 +190,8 @@
приватная
</div>
{% endif %}
<div class="my_author" style="style=position:absolute; width:148px; height:44px; left:255px; -webkit-border-radius:
22px;-moz-border-radius: 22px;border-radius: 22px; border:2px solid #FFFFFF; background-color:#1daff0; top:
98px;"><img src="../{{ current_user.photo }}" width="40" height="40" style="border-radius: 22px">
<div class="my_author"><img src="../{{ current_user.photo }}" width="40" height="40"
style="border-radius: 22px">
<strong style="color: #ffffff">{{ current_user.name }}</strong>
</div>
<strong style="color:#ffffff">{{item.date}}</strong>

View File

@ -56,6 +56,19 @@
</div>
<p></p>
{% endif %}
<div class="like">
<a type="button"
href="/new_like/{{current_user.id}}/{{item.id}}/publications">
{% if emotion_fresh[loop.index0]['is_like'] %}
<img src="../static/img/like_add.png" width="117" height="100">
{% else %}
<img src="../static/img/like.png" width="94" height="80">
{% endif %}
</a>
{% if emotion_fresh[loop.index0]['like'] != None %}
<p style="color:#ffffff">{{emotion_fresh[loop.index0]['like']}}</p>
{% endif %}
</div>
{% if emotion_fresh[loop.index0]['link'] != None %}
<details>
<summary class="emot_block">
@ -73,7 +86,8 @@
{% endif %}
<div class="author" style="style=position:absolute; width:148px; height:44px; left:255px; -webkit-border-radius:
22px;-moz-border-radius: 22px;border-radius: 22px; border:2px solid #FFFFFF; background-color:#1daff0; top:
98px;"><img src="../{{ emotion_fresh[loop.index0]['author'].photo }}" width="40" height="40" style="border-radius: 22px">
98px;"><img src="../{{ emotion_fresh[loop.index0]['author'].photo }}" width="40" height="40"
style="border-radius: 22px">
<strong style="color: #ffffff">{{ emotion_fresh[loop.index0]['author'].name }}</strong>
</div>
<strong style="color:#ffffff">{{item.date}}</strong>