-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexperience.py
316 lines (284 loc) · 10.2 KB
/
experience.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
from core.models.categories import ExperiencesCategory
from core.models.helper import pagination_wrapper
from core.models.pages import CustomBasePage
from django import forms
from django.db import models
from django.http import HttpRequest, HttpResponse
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from embed_video.fields import EmbedVideoField
from loguru import logger
from markdownx.models import MarkdownxField
from modelcluster.contrib.taggit import ClusterTaggableManager
from modelcluster.fields import ParentalKey, ParentalManyToManyField
from taggit.models import TaggedItemBase
from wagtail.admin.panels import FieldPanel, MultiFieldPanel
from wagtail.contrib.routable_page.models import RoutablePageMixin, route
from wagtail.models import Page
from wagtailautocomplete.edit_handlers import AutocompletePanel
from bfportal.settings.base import LOGIN_URL
from ..helper import user_to_api_response
class ExperiencePageTag(TaggedItemBase):
"""Class to link a tag to an Experience page"""
content_object = ParentalKey(
"ExperiencePage",
related_name="tagged_items",
on_delete=models.CASCADE,
)
class ExperiencePage(RoutablePageMixin, CustomBasePage):
"""Class defining properties of an experience page
This is equivalent to a post in a blog
"""
featured = models.BooleanField(
default=False,
help_text="Is this experience a featured experience",
verbose_name="Set Featured",
)
trending = models.BooleanField(
default=False,
null=False,
help_text="Is this experience trending?",
verbose_name="Set Trending",
)
description = MarkdownxField(
default="",
help_text="Description of Your experience",
verbose_name="Description",
)
code = models.CharField(
blank=True,
max_length=6,
default="",
help_text="Six letter alpha-numeric code of you experience",
verbose_name="Experience Code",
)
exp_url = models.URLField(
blank=True,
default="",
help_text="Url of your experience",
verbose_name="Experience Url",
)
tags = ClusterTaggableManager(
blank=True,
help_text="Some tags",
through=ExperiencePageTag,
verbose_name="Tags",
)
vid_url = EmbedVideoField(
blank=True,
default="",
help_text="Link to vid showcasing your experience",
verbose_name="Video Url",
)
cover_img_url = models.URLField(
blank=True,
default="",
help_text="Link for your cover Image",
verbose_name="Cover Image Url",
max_length=1000,
)
no_players = models.PositiveIntegerField(
blank=True,
default=0,
help_text="Max Number of Human Players in your experience",
verbose_name="Number of Human Players",
)
no_bots = models.PositiveIntegerField(
blank=True,
default=0,
help_text="Max Number of Bots in your experience",
verbose_name="Number Of Bots",
)
category = models.ForeignKey(
ExperiencesCategory,
blank=False,
null=True,
on_delete=models.SET_NULL,
help_text="Choose Main Category",
related_name="+",
)
sub_categories = ParentalManyToManyField(
"core.SubCategory", blank=True, help_text="Choose Sub Category Category"
)
bugged = models.BooleanField(
default=False,
null=False,
help_text="Is the experience bugged",
verbose_name="Bugged ?",
)
bugged_report = ParentalManyToManyField(
"auth.User",
blank=True,
help_text="People who have reported this exp is bugged",
related_name="bugged_report",
)
broken = models.BooleanField(
default=False,
null=False,
help_text="Is the experience broken",
verbose_name="Broken ?",
)
broken_report = ParentalManyToManyField(
"auth.User",
blank=True,
help_text="People who have reported this is exp broken",
related_name="broken_report",
)
xp_farm = models.BooleanField(
default=False,
null=False,
help_text="Is the experience an xp farm",
verbose_name="XP farm ?",
)
xp_farm_report = ParentalManyToManyField(
"auth.User",
blank=True,
help_text="People who have reported this is exp an xp farm",
related_name="xp_farm_report",
)
first_publish = models.BooleanField(default=True, null=False)
liked_by = models.ManyToManyField("core.Profile", blank=True)
creators = ParentalManyToManyField(
"auth.User", blank=True, help_text="choose creators"
)
allow_editing = models.BooleanField(default=False, null=False)
is_mock = models.BooleanField(
default=False, null=False, help_text="Is this a mock experience ?"
)
parent_page_types = ["core.ExperiencesPage"]
subpage_types = []
content_panels = (
Page.content_panels
+ [
MultiFieldPanel(
[
FieldPanel("featured", classname="full", permission="superuser"),
FieldPanel("trending", classname="full", permission="superuser"),
*[
FieldPanel(field, classname="full", permission="superuser")
for field in ["bugged", "broken", "xp_farm"]
],
],
heading="Admin only",
),
MultiFieldPanel(
[
FieldPanel("allow_editing"),
AutocompletePanel("owner", target_model="core.Profile"),
FieldPanel("description", classname="full"),
FieldPanel("category", widget=forms.RadioSelect),
FieldPanel("sub_categories", widget=forms.CheckboxSelectMultiple),
AutocompletePanel("creators", target_model="core.Profile"),
],
heading="Title, Description, Categories, Creators",
classname="collapsed",
),
MultiFieldPanel(
[
FieldPanel("code", classname="full"),
FieldPanel("exp_url", classname="full"),
],
heading="Experience Url and Code",
classname="collapsed",
),
MultiFieldPanel(
[
FieldPanel("tags"),
FieldPanel("cover_img_url", classname="full"),
FieldPanel("vid_url", classname="full"),
],
heading="Tags, cover img, vid",
classname="collapsed",
),
MultiFieldPanel(
[
FieldPanel("no_players", classname="full"),
FieldPanel("no_bots", classname="full"),
],
heading="Players, Bots",
classname="collapsed",
),
*[
MultiFieldPanel(
[
AutocompletePanel(
f"{field}_report",
target_model="core.Profile",
classname="full",
),
],
heading=f"{field.replace('_', ' ').capitalize()} info",
classname="collapsed",
)
for field in ["bugged", "broken", "xp_farm"]
],
]
+ [CustomBasePage.content_panels[-1]]
)
@property
def like_count(self):
"""Return the number of likes this experience has."""
return self.liked_by.count()
@property
def exp_creators(self):
"""Return the creators of this experience."""
creators = [user_to_api_response(self.owner)]
creators.extend(
[user_to_api_response(creator) for creator in self.creators.all()]
)
return creators
@route(r"^edit/$")
def edit_page(self, request: HttpRequest): # noqa: D102
if request.user.is_authenticated:
if self.owner == request.user:
from core.views import edit_experience
return edit_experience(request, self)
else:
logger.debug(
f"{request.user} tried to edit a experience they dont own exp: {request.path}"
)
return HttpResponse("Unauthorized", status=401)
else:
return redirect(LOGIN_URL)
@route(r"^delete/$")
def delete_experience(self, request: HttpRequest): # noqa: D102
if request.user.is_authenticated:
if self.owner == request.user:
# todo actually delete it
print("Delete experience")
return redirect(request.META["HTTP_REFERER"])
else:
logger.debug(
f"{request.user} tried to delete a experience they dont own exp: {request.path}"
)
return HttpResponse("Unauthorized", status=401)
else:
return redirect(LOGIN_URL)
@staticmethod
def is_experience_page(): # noqa: D102
return True
class ExperiencesPage(RoutablePageMixin, CustomBasePage):
"""Class defining page that lists all experiences"""
max_count = 1
parent_page_types = ["core.HomePage"]
subpage_types = ["core.ExperiencePage", "core.BlogPage"]
def get_context(self, request, *args, **kwargs):
"""Adding custom stuff to our context."""
context = super().get_context(request, *args, **kwargs)
# Get all posts
context["posts"] = pagination_wrapper(
request,
ExperiencePage.objects.live().public().order_by("-first_published_at"),
)
return context
@route(r"^featured/$")
def featured_experiences(self, request: HttpRequest): # noqa: D102
return TemplateResponse(
request,
self.get_template(request),
{
"posts": ExperiencePage.objects.live()
.filter(featured__exact=True)
.order_by("-first_published_at"),
},
)