PonyConf/cfp/utils.py

42 lines
1.3 KiB
Python
Raw Normal View History

2017-05-29 20:48:49 +00:00
from django.utils.crypto import get_random_string
from django.contrib.sites.shortcuts import get_current_site
from django.db.models import Q, Sum
from django.db.models.functions import Coalesce
from django.utils.safestring import mark_safe
from markdown import markdown
import bleach
2017-07-30 14:57:38 +00:00
from .models import Conference
def get_current_conf(request):
site = get_current_site(request)
return Conference.objects.get(site=site)
2017-05-29 20:48:49 +00:00
def query_sum(queryset, field):
return queryset.aggregate(s=Coalesce(Sum(field), 0))['s']
2017-07-30 14:57:38 +00:00
def generate_user_uid():
return get_random_string(length=12, allowed_chars='abcdefghijklmnopqrstuvwxyz0123456789')
2017-05-29 20:48:49 +00:00
def allowed_talks(talks, request):
if not Participation.objects.get(site=get_current_site(request), user=request.user).is_orga():
talks = talks.filter(Q(topics__reviewers=request.user) | Q(speakers=request.user) | Q(proposer=request.user))
return talks.distinct()
2017-07-30 14:57:38 +00:00
2017-05-29 20:48:49 +00:00
def markdown_to_html(md):
html = markdown(md)
allowed_tags = bleach.ALLOWED_TAGS + ['p', 'pre', 'span' ] + ['h%d' % i for i in range(1, 7) ]
html = bleach.clean(html, tags=allowed_tags)
return mark_safe(html)
2017-07-30 14:57:38 +00:00
def is_staff(request, user):
conference = get_current_conf(request)
return user.is_authenticated and (user.is_superuser or conference.staff.filter(pk=user.pk).exists())