Skip to content

Commit bffda9b

Browse files
authored
Merge branch 'master' into interaction-cooldown
2 parents d65ede2 + d9169b4 commit bffda9b

File tree

6 files changed

+65
-32
lines changed

6 files changed

+65
-32
lines changed

dmoj/settings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@
536536

537537
BLEACH_USER_SAFE_ATTRS = {
538538
'*': ['id', 'class', 'style', 'data', 'height'],
539-
'img': ['src', 'alt', 'title', 'width', 'height', 'data-src'],
539+
'img': ['src', 'alt', 'title', 'width', 'height', 'data-src', 'align'],
540540
'a': ['href', 'alt', 'title'],
541541
'iframe': ['src', 'height', 'width', 'allow'],
542542
'abbr': ['title'],
@@ -548,6 +548,7 @@
548548
'audio': ['autoplay', 'controls', 'crossorigin', 'muted', 'loop', 'preload', 'src'],
549549
'video': ['autoplay', 'controls', 'crossorigin', 'height', 'muted', 'loop', 'poster', 'preload', 'src', 'width'],
550550
'source': ['src', 'srcset', 'type'],
551+
'li': ['value'],
551552
}
552553

553554
MARKDOWN_STAFF_EDITABLE_STYLE = {

judge/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ def widget_attrs(self, widget):
460460
class TOTPForm(Form):
461461
TOLERANCE = settings.DMOJ_TOTP_TOLERANCE_HALF_MINUTES
462462

463-
totp_or_scratch_code = NoAutoCompleteCharField(required=False)
463+
totp_or_scratch_code = NoAutoCompleteCharField(required=False, widget=forms.TextInput(attrs={'autofocus': True}))
464464

465465
def __init__(self, *args, **kwargs):
466466
self.profile = kwargs.pop('profile')

judge/management/commands/import_polygon_package.py

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from django.conf import settings
1212
from django.contrib.sites.models import Site
1313
from django.core.files import File
14+
from django.core.files.storage import default_storage
1415
from django.core.management.base import BaseCommand, CommandError
1516
from django.db import transaction
1617
from django.urls import reverse
@@ -255,23 +256,39 @@ def parse_statements(problem_meta, root, package):
255256
problem_meta['translations'] = []
256257
problem_meta['tutorial'] = ''
257258

258-
image_cache = {}
259+
def process_images(text):
260+
image_cache = problem_meta['image_cache']
259261

260-
def save_image(image_path):
261-
norm_path = os.path.normpath(os.path.join(statement_folder, image_path))
262-
sha1 = hashlib.sha1()
263-
sha1.update(package.open(norm_path, 'r').read())
264-
sha1 = sha1.hexdigest()
262+
def save_image(image_path):
263+
norm_path = os.path.normpath(os.path.join(statement_folder, image_path))
264+
sha1 = hashlib.sha1()
265+
sha1.update(package.open(norm_path, 'r').read())
266+
sha1 = sha1.hexdigest()
265267

266-
if sha1 not in image_cache:
267-
image = File(
268-
file=package.open(norm_path, 'r'),
269-
name=os.path.basename(image_path),
268+
if sha1 not in image_cache:
269+
image = File(
270+
file=package.open(norm_path, 'r'),
271+
name=os.path.basename(image_path),
272+
)
273+
data = json.loads(django_uploader(image))
274+
image_cache[sha1] = data['link']
275+
276+
return image_cache[sha1]
277+
278+
for image_path in set(re.findall(r'!\[image\]\((.+?)\)', text)):
279+
text = text.replace(
280+
f'![image]({image_path})',
281+
f'![image]({save_image(image_path)})',
270282
)
271-
data = json.loads(django_uploader(image))
272-
image_cache[sha1] = data['link']
273283

274-
return image_cache[sha1]
284+
for img_tag in set(re.findall(r'<\s*img[^>]*>', text)):
285+
image_path = re.search(r'<\s*img[^>]+src\s*=\s*(["\'])(.*?)\1[^>]*>', text).group(2)
286+
text = text.replace(
287+
img_tag,
288+
img_tag.replace(image_path, save_image(image_path)),
289+
)
290+
291+
return text
275292

276293
def parse_problem_properties(problem_properties):
277294
description = ''
@@ -304,20 +321,6 @@ def parse_problem_properties(problem_properties):
304321
description += '\n## Notes\n\n'
305322
description += pandoc_tex_to_markdown(problem_properties['notes'])
306323

307-
# Images
308-
for image_path in set(re.findall(r'!\[image\]\((.+?)\)', description)):
309-
description = description.replace(
310-
f'![image]({image_path})',
311-
f'![image]({save_image(image_path)})',
312-
)
313-
314-
for img_tag in set(re.findall(r'<\s*img[^>]*>', description)):
315-
image_path = re.search(r'<\s*img[^>]+src\s*=\s*(["\'])(.*?)\1[^>]*>', description).group(2)
316-
description = description.replace(
317-
img_tag,
318-
img_tag.replace(image_path, save_image(image_path)),
319-
)
320-
321324
return description
322325

323326
def input_choice(prompt, choices):
@@ -351,7 +354,7 @@ def input_choice(prompt, choices):
351354
description = parse_problem_properties(problem_properties)
352355
translations.append({
353356
'language': language,
354-
'description': description,
357+
'description': process_images(description),
355358
})
356359

357360
tutorial = problem_properties['tutorial']
@@ -378,6 +381,9 @@ def input_choice(prompt, choices):
378381
elif len(tutorials) > 0:
379382
problem_meta['tutorial'] = tutorials[0]['tutorial']
380383

384+
# Process images for only the selected tutorial
385+
problem_meta['tutorial'] = process_images(problem_meta['tutorial'])
386+
381387
for t in translations:
382388
language = t['language']
383389
description = t['description']
@@ -545,6 +551,7 @@ def handle(self, *args, **options):
545551

546552
# A dictionary to hold all problem information.
547553
problem_meta = {}
554+
problem_meta['image_cache'] = {}
548555
problem_meta['code'] = problem_code
549556
problem_meta['tmp_dir'] = tempfile.TemporaryDirectory()
550557
problem_meta['authors'] = problem_authors
@@ -555,6 +562,11 @@ def handle(self, *args, **options):
555562
parse_statements(problem_meta, root, package)
556563
create_problem(problem_meta)
557564
except Exception:
565+
# Remove imported images
566+
for image_url in problem_meta['image_cache'].values():
567+
path = default_storage.path(os.path.join(settings.MARTOR_UPLOAD_MEDIA_DIR, os.path.basename(image_url)))
568+
os.remove(path)
569+
558570
raise
559571
finally:
560572
problem_meta['tmp_dir'].cleanup()

judge/views/user.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from django.shortcuts import get_object_or_404, render
2424
from django.urls import reverse
2525
from django.utils import timezone
26+
from django.utils.decorators import method_decorator
2627
from django.utils.formats import date_format
2728
from django.utils.functional import cached_property
2829
from django.utils.safestring import mark_safe
@@ -286,9 +287,23 @@ def get_context_data(self, **kwargs):
286287

287288
return context
288289

290+
@method_decorator(require_POST)
291+
def delete_comments(self, request, *args, **kwargs):
292+
if not request.user.is_superuser:
293+
raise PermissionDenied()
294+
295+
user_id = User.objects.get(username=kwargs['user']).id
296+
user = Profile.objects.get(user=user_id)
297+
for comment in Comment.get_newest_visible_comments(viewer=request.user, author=user,
298+
batch=2 * self.paginate_by):
299+
comment.get_descendants(include_self=True).update(hidden=True)
300+
return HttpResponseRedirect(reverse('user_comment', args=(user.user.username,)))
301+
289302
def dispatch(self, request, *args, **kwargs):
290303
if not self.request.user.is_superuser:
291304
raise PermissionDenied()
305+
if request.method == 'POST':
306+
return self.delete_comments(request, *args, **kwargs)
292307
return super().dispatch(request, *args, **kwargs)
293308

294309

@@ -535,7 +550,7 @@ class UserList(QueryStringSortMixin, InfinitePaginationMixin, DiggPaginatorMixin
535550
paginate_by = 100
536551
all_sorts = frozenset(('points', 'problem_count', 'rating', 'performance_points'))
537552
default_desc = all_sorts
538-
default_sort = '-performance_points'
553+
default_sort = '-rating'
539554

540555
def get_queryset(self):
541556
return (Profile.objects.filter(is_unlisted=False).order_by(self.order)

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ dmoj-wpadmin @ git+https://github.com/DMOJ/dmoj-wpadmin.git
1313
lxml
1414
Pygments<2.12
1515
mistune<2
16-
social-auth-app-django
16+
social-auth-core==4.3.0
17+
social-auth-app-django==5.0.0
1718
pytz
1819
django-statici18n
1920
pika

templates/user/comment.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
{% block body %}
1515
{% block before_comments %}{% endblock %}
16+
<form action="{{url('user_comment', user.username)}}" method="post">
17+
{% csrf_token %}
18+
<button type="submit">Delete all comments</button>
19+
</form>
1620
<ul class="comments top-level-comments new-comments">
1721
{% set logged_in = request.user.is_authenticated %}
1822
{% for comment in comments %}

0 commit comments

Comments
 (0)