friendship
This commit is contained in:
parent
a4d272bdfe
commit
52b09cd692
47
Main/migrations/0007_friendship.py
Normal file
47
Main/migrations/0007_friendship.py
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
# Generated by Django 3.2.4 on 2021-12-19 14:52
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||||
|
("Main", "0006_solution_test"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name="Friendship",
|
||||||
|
fields=[
|
||||||
|
(
|
||||||
|
"id",
|
||||||
|
models.BigAutoField(
|
||||||
|
auto_created=True,
|
||||||
|
primary_key=True,
|
||||||
|
serialize=False,
|
||||||
|
verbose_name="ID",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
("verified", models.BooleanField(default=False)),
|
||||||
|
(
|
||||||
|
"from_user",
|
||||||
|
models.ForeignKey(
|
||||||
|
on_delete=django.db.models.deletion.CASCADE,
|
||||||
|
related_name="from_friendship",
|
||||||
|
to=settings.AUTH_USER_MODEL,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"to_user",
|
||||||
|
models.ForeignKey(
|
||||||
|
on_delete=django.db.models.deletion.CASCADE,
|
||||||
|
related_name="to_friendship",
|
||||||
|
to=settings.AUTH_USER_MODEL,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
@ -8,3 +8,4 @@ from Main.models.solution import Solution
|
|||||||
from Main.models.extrafile import ExtraFile
|
from Main.models.extrafile import ExtraFile
|
||||||
from Main.models.progress import Progress
|
from Main.models.progress import Progress
|
||||||
from Main.models.solution_file import SolutionFile
|
from Main.models.solution_file import SolutionFile
|
||||||
|
from Main.models.friendship import Friendship
|
||||||
|
8
Main/models/friendship.py
Normal file
8
Main/models/friendship.py
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
from django.contrib.auth.models import User
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
|
class Friendship(models.Model):
|
||||||
|
from_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="from_friendship")
|
||||||
|
to_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="to_friendship")
|
||||||
|
verified = models.BooleanField(default=False)
|
@ -1,5 +1,8 @@
|
|||||||
|
from functools import cached_property
|
||||||
|
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.db.models import Q
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
from Main.models.set import Set
|
from Main.models.set import Set
|
||||||
@ -28,6 +31,13 @@ class UserInfo(models.Model):
|
|||||||
def has_favourite_language(self):
|
def has_favourite_language(self):
|
||||||
return self.favourite_language_id is not None
|
return self.favourite_language_id is not None
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def friends(self):
|
||||||
|
return User.objects.filter(
|
||||||
|
Q(to_friendship__to_user=self, to_friendship__verified=True)
|
||||||
|
| Q(from_friendship__from_user=self, from_friendship__verified=True)
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def favourite_language(self):
|
def favourite_language(self):
|
||||||
if not self.has_favourite_language:
|
if not self.has_favourite_language:
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
from django.db.models import Q
|
||||||
|
|
||||||
|
from Main.management.commands.bot import bot
|
||||||
|
from Main.models import Friendship
|
||||||
from SprintLib.BaseView import BaseView
|
from SprintLib.BaseView import BaseView
|
||||||
from SprintLib.utils import delete_file, write_bytes
|
from SprintLib.utils import delete_file, write_bytes
|
||||||
|
|
||||||
@ -9,7 +12,7 @@ class AccountView(BaseView):
|
|||||||
required_login = True
|
required_login = True
|
||||||
endpoint = "account"
|
endpoint = "account"
|
||||||
|
|
||||||
def get(self):
|
def pre_handle(self):
|
||||||
if "username" in self.request.GET.keys():
|
if "username" in self.request.GET.keys():
|
||||||
self.context["account"] = User.objects.get(
|
self.context["account"] = User.objects.get(
|
||||||
username=self.request.GET["username"]
|
username=self.request.GET["username"]
|
||||||
@ -17,7 +20,45 @@ class AccountView(BaseView):
|
|||||||
else:
|
else:
|
||||||
self.context["account"] = self.request.user
|
self.context["account"] = self.request.user
|
||||||
self.context["owner"] = self.context["account"] == self.request.user
|
self.context["owner"] = self.context["account"] == self.request.user
|
||||||
|
|
||||||
|
def get(self):
|
||||||
self.context["error_message"] = self.request.GET.get("error_message", "")
|
self.context["error_message"] = self.request.GET.get("error_message", "")
|
||||||
|
friendship = Friendship.objects.filter(
|
||||||
|
Q(from_user=self.request.user, to_user=self.context["account"])
|
||||||
|
| Q(to_user=self.request.user, from_user=self.context["account"])
|
||||||
|
).first()
|
||||||
|
if friendship is None:
|
||||||
|
self.context["friendship_status"] = 0
|
||||||
|
elif friendship.verified:
|
||||||
|
self.context["friendship_status"] = 1
|
||||||
|
elif friendship.from_user == self.request.user:
|
||||||
|
self.context["friendship_status"] = 2
|
||||||
|
else:
|
||||||
|
self.context["friendship_status"] = 3
|
||||||
|
|
||||||
|
def post_friendship(self):
|
||||||
|
if "to_do" in self.request.POST:
|
||||||
|
friendship = Friendship.objects.filter(
|
||||||
|
Q(from_user=self.request.user, to_user=self.context["account"])
|
||||||
|
| Q(to_user=self.request.user, from_user=self.context["account"])
|
||||||
|
).first()
|
||||||
|
if friendship is None:
|
||||||
|
Friendship.objects.create(from_user=self.request.user, to_user=self.context["account"])
|
||||||
|
bot.send_message(self.context["account"].userinfo.telegram_chat_id, f"Пользователь {self.request.user.username} хочет добавить тебя в друзья")
|
||||||
|
elif friendship.verified or friendship.from_user == self.request.user:
|
||||||
|
friendship.delete()
|
||||||
|
else:
|
||||||
|
if self.request.POST["todo"] == "yes":
|
||||||
|
friendship.verified = True
|
||||||
|
friendship.save()
|
||||||
|
bot.send_message(self.context["account"].userinfo.telegram_chat_id,
|
||||||
|
f"Пользователь {self.request.user.username} добавил тебя в друзья")
|
||||||
|
else:
|
||||||
|
friendship.delete()
|
||||||
|
bot.send_message(self.context["account"].userinfo.telegram_chat_id,
|
||||||
|
f"Пользователь {self.request.user.username} отклонил твою заявку")
|
||||||
|
return "/account?username=" + self.request.GET["username"]
|
||||||
|
|
||||||
|
|
||||||
def post_upload_photo(self):
|
def post_upload_photo(self):
|
||||||
if self.request.user.userinfo.has_profile_pic:
|
if self.request.user.userinfo.has_profile_pic:
|
||||||
|
@ -24,6 +24,21 @@
|
|||||||
<h3>
|
<h3>
|
||||||
{{ account.userinfo.surname }} {{ account.userinfo.name }}
|
{{ account.userinfo.surname }} {{ account.userinfo.name }}
|
||||||
<span style="margin-left: 15px;" class="badge badge-{% if account.userinfo.activity_status == online_status %}success{% else %}secondary{% endif %}">{{ account.userinfo.activity_status }}</span>
|
<span style="margin-left: 15px;" class="badge badge-{% if account.userinfo.activity_status == online_status %}success{% else %}secondary{% endif %}">{{ account.userinfo.activity_status }}</span>
|
||||||
|
<form method="POST">
|
||||||
|
{% csrf_token %}
|
||||||
|
<input type="hidden" name="action" value="friendship">
|
||||||
|
{% if not owner %}
|
||||||
|
{% if friendship_status == 0 %}
|
||||||
|
<button type="submit" class="btn btn-primary" name="to_do" value="add">Добавить в друзья</button>
|
||||||
|
{% else %}{% if friendship_status == 1 %}
|
||||||
|
<button class="btn btn-success"><i class="fa fa-check"></i> Друзья</button> <button class="btn btn-danger" type="submit" name="to_do" value="delete"><i class="fa fa-times"></i> Удалить</button>
|
||||||
|
{% else %}{% if friendship_status == 2 %}
|
||||||
|
<button class="btn btn-info">Приглашение отправлено</button> <button class="btn btn-danger" type="submit" name="to_do" value="delete">Отменить</button>
|
||||||
|
{% else %}
|
||||||
|
<button class="btn btn-info" type="submit" name="to_do" value="yes">Ответить на приглашение</button> <button class="btn btn-danger" type="submit" name="to_do" value="no">Отклонить</button>
|
||||||
|
{% endif %}{% endif %}{% endif %}
|
||||||
|
{% endif %}
|
||||||
|
</form>
|
||||||
{% if user.is_superuser and owner %}
|
{% if user.is_superuser and owner %}
|
||||||
<a style="margin-left: 15px;" href="/admin/" class="badge badge-secondary">Админ</a>
|
<a style="margin-left: 15px;" href="/admin/" class="badge badge-secondary">Админ</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
|
|
||||||
{% block main %}
|
{% block main %}
|
||||||
<h2>Решения</h2>
|
<h2>Решения</h2>
|
||||||
<h4>Фильтр</h4>
|
<h4>Фильтр (пока недоступная опция)</h4>
|
||||||
<div>
|
<div>
|
||||||
<select name="set_id" style="width: 33%">
|
<select name="set_id" style="width: 33%">
|
||||||
<option value="0">Все сеты</option>
|
<option value="0">Все сеты</option>
|
||||||
|
Loading…
Reference in New Issue
Block a user