1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
6 from functools import wraps
11 from django.conf import settings
12 from django.contrib import admin
13 from django.core.cache import cache
14 from django.core.mail import send_mail
15 from django.template.loader import render_to_string
16 from django.utils.translation import get_language
17 from django.conf import settings
18 from django.utils.safestring import mark_safe
22 return dt.strftime('%Y/%m/%d %H:%M:%S UTC')
26 if not os.path.isdir(path):
30 def send_noreply_mail(subject, message, recipient_list, **kwargs):
32 '[WolneLektury] ' + subject,
33 message + "\n\n-- \nWiadomość wysłana automatycznie. Prosimy nie odpowiadać.",
34 'no-reply@wolnelektury.pl', recipient_list, **kwargs)
37 # source: https://docs.python.org/2/library/csv.html#examples
38 class UnicodeCSVWriter(object):
40 A CSV writer which will write rows to CSV file "f",
41 which is encoded in the given encoding.
44 def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
45 # Redirect output to a queue
46 self.queue = BytesIO()
47 self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
49 self.encoder = codecs.getincrementalencoder(encoding)()
51 def writerow(self, row):
52 self.writer.writerow([s.encode("utf-8") for s in row])
53 # Fetch UTF-8 output from the queue ...
54 data = self.queue.getvalue()
55 data = data.decode("utf-8")
56 # ... and reencode it into the target encoding
57 data = self.encoder.encode(data)
58 # write to the target stream
59 self.stream.write(data)
61 self.queue.truncate(0)
63 def writerows(self, rows):
68 # the original re.escape messes with unicode
70 return re.sub(r"[(){}\[\].*?|^$\\+-]", r"\\\g<0>", s)
73 def get_cached_render_key(instance, property_name, language=None):
75 language = get_language()
76 return 'cached_render:%s.%s:%s:%s' % (
77 type(instance).__name__,
84 def cached_render(template_name, timeout=24 * 60 * 60):
85 def decorator(method):
88 key = get_cached_render_key(self, method.__name__)
89 content = cache.get(key)
91 context = method(self)
92 content = render_to_string(template_name, context)
93 cache.set(key, str(content), timeout=timeout)
95 content = mark_safe(content)
101 def clear_cached_renders(bound_method):
102 for lc, ln in settings.LANGUAGES:
104 get_cached_render_key(
105 bound_method.__self__,
106 bound_method.__name__,
112 class YesNoFilter(admin.SimpleListFilter):
113 def lookups(self, request, model_admin):
119 def queryset(self, request, queryset):
120 if self.value() == 'yes':
121 return queryset.filter(self.q)
122 elif self.value() == 'no':
123 return queryset.exclude(self.q)
126 def is_ajax(request):
127 return request.headers.get('x-requested-with') == 'XMLHttpRequest'