-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathusers.py
212 lines (182 loc) · 7.7 KB
/
users.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import os
from allauth.socialaccount.models import SocialAccount
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group, User
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.db.models import Count, Q
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.http import HttpRequest
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from loguru import logger
from wagtail.admin.panels import FieldPanel
from wagtail.contrib.routable_page.models import RoutablePageMixin, route
from bfportal.settings.base import LOGIN_URL
from .experience import ExperiencePage
from .helper import pagination_wrapper
from .pages import CustomBasePage
from .settings import GenericSystemSettings
def social_user(discord_id: int) -> User | bool:
"""Returns a User object for a discord id"""
try:
usr = get_user_model().objects.get(
id=SocialAccount.objects.get(uid=discord_id).user_id
)
return usr
except ObjectDoesNotExist:
return False
class Profile(models.Model):
"""Class that tracks extra data about user"""
user = models.OneToOneField(User, on_delete=models.CASCADE)
liked = models.ManyToManyField("core.ExperiencePage", blank=True)
is_mock_user = models.BooleanField(
default=False,
null=False,
help_text="If set to true, this user was created by the mock command, and is a fake user",
)
hide_username = models.BooleanField(
default=False,
null=False,
help_text="If set to true hides the username on the website",
)
autocomplete_search_field = "user__username"
panels = [FieldPanel("hide_username")]
def __str__(self):
return self.user.username
def autocomplete_label(self):
"""Called by Wagtail auto complete to get label for an account"""
if not self.user.is_superuser:
discord_data = self.user.socialaccount_set.extra().first().extra_data
if len(discord_data.get("username", "")):
return f"{discord_data['username']}#{discord_data['discriminator']} : {discord_data['id']}"
else:
return str(self.user)
else:
return str(self.user)
def add_liked_page(self, experience_page: "ExperiencePage"):
"""Adds a ExperiencePage to `self.liked`, and adds self to `ExperiencePage.liked_by`
Does not call the `save` function.
"""
self.liked.add(experience_page)
experience_page.liked_by.add(self)
def remove_liked_page(self, experience_page: "ExperiencePage"):
"""Removes a `ExperiencePage` from `self.liked`, and removes self from page's `liked_by`
Does not call the `save` function.
"""
self.liked.remove(experience_page)
experience_page.liked_by.remove(self)
@staticmethod
@receiver(post_save, sender=User)
def create_user_profile(sender, instance: User, created, **kwargs):
"""Called when a new user is created"""
if created:
if (group := Group.objects.filter(name="self edit")).exists():
instance.groups.add(group[0])
Profile.objects.create(user=instance)
@staticmethod
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
"""Called when a user data is updated"""
if (
profile := getattr(instance, "profile", None)
) is None: # is case a user with no profile tries to log in.
profile = Profile.objects.create(user=instance)
profile.save()
class ProfilePage(RoutablePageMixin, CustomBasePage):
"""Class representing the details of a user profile page"""
max_count = 1
parent_page_types = ["core.HomePage"]
subpage_types = ["core.BlogPage"]
def serve(self, request, view=None, args=None, kwargs=None):
"""Checks if a user is authenticated before serving the profile page"""
system_settings = GenericSystemSettings.load(request_or_site=request)
if system_settings.need_login_to_view_profile:
return (
super().serve(request, view, args, kwargs)
if request.user.is_authenticated
else redirect(LOGIN_URL)
)
return super().serve(request, view, args, kwargs)
def get_context(self, request, *args, **kwargs): # noqa: D102
list_experiences = kwargs.pop("list_experiences", None)
user_acc = kwargs.pop("user", None)
context = super().get_context(request, *args, **kwargs)
all_posts = (
ExperiencePage.objects.live()
.public()
.filter(Q(owner=user_acc) | Q(creators=user_acc))
.order_by("-first_published_at")
)
if not user_acc:
user_acc = request.user
if list_experiences:
context["posts"] = pagination_wrapper(
request,
all_posts,
)
logger.debug(f"filtred {len(all_posts)} for {user_acc}")
context["user"] = request.user
context["requested_user"] = user_acc
context["latest_post"] = all_posts.first()
context["total_num_posts"] = len(all_posts)
context["earned_likes"] = sum(all_posts.aggregate(Count("liked_by")).values())
context["owners"] = os.getenv("OWNERS", "").split(",")
return context
@route(r"^$")
def root_profile_page(self, request): # noqa: D102
return redirect("/")
@route(r"^(\d{18,})/$", name="discord_id")
def profile_page_view(self, request: HttpRequest, discord_id):
"""Serves a profile page for a user ID"""
user = social_user(discord_id)
if user:
logger.debug(f"fetch profile for {user}")
return TemplateResponse(
request,
self.get_template(request),
self.get_context(request, list_experiences=True, user=user),
)
else:
return TemplateResponse(request, "404.html", status=404)
@route(r"(\d{18,})/experiences/$", name="discord_id")
def user_experiences(self, request, discord_id):
"""Servers a list of experiences by a user"""
user = social_user(discord_id=discord_id)
if user:
logger.debug(f"fetch experiences for {user}")
return TemplateResponse(
request,
"core/experiences_page.html",
self.get_context(request=request, list_experiences=True, user=user),
)
else:
return TemplateResponse(request, "404.html", status=404)
@route(r"(\d{18,})/liked/$", name="discord_id")
def user_liked_experiences(self, request, discord_id):
"""Servers a list of experiences by a user"""
user = social_user(discord_id=discord_id)
if user:
return TemplateResponse(
request,
"core/experiences_page.html",
{"posts": user.profile.liked.all()},
)
else:
return TemplateResponse(request, "404.html", status=404)
@route(r"^(\w+)/$", name="username")
def named_profile_page_view(self, request: HttpRequest, username):
"""Handles the requests for users that have a named profile"""
if username == "admin":
return TemplateResponse(
request,
self.get_template(request),
self.get_context(
request,
list_experiences=True,
user=User.objects.filter(is_superuser=True).first(),
),
)
else:
return TemplateResponse(request, "404.html", status=404)