changeset 1 | fda1c66b25f9 |
0:9a77edda77b7 | 1:fda1c66b25f9 |
---|---|
1 # -*- coding: utf-8 -*- |
|
2 #python |
|
3 import urllib |
|
4 import datetime |
|
5 import re |
|
6 from random import randint |
|
7 |
|
8 #django |
|
9 from django.http import HttpResponseRedirect |
|
10 |
|
11 def kiwipycon_quote(string, encoding="utf-8"): |
|
12 """Encodes string to encoding before quoting. |
|
13 """ |
|
14 return urllib.quote(string.encode(encoding)) |
|
15 |
|
16 # from LFS |
|
17 def set_message_cookie(url, msg): |
|
18 """Creates response object with given url and adds message cookie with passed |
|
19 message. |
|
20 """ |
|
21 |
|
22 # We just keep the message two seconds. |
|
23 max_age = 2 |
|
24 expires = datetime.datetime.strftime( |
|
25 datetime.datetime.utcnow() + |
|
26 datetime.timedelta(seconds=max_age), "%a, %d-%b-%Y %H:%M:%S GMT") |
|
27 |
|
28 response = HttpResponseRedirect(url) |
|
29 response.set_cookie("message", kiwipycon_quote(msg), max_age=max_age, expires=expires) |
|
30 |
|
31 return response |
|
32 |
|
33 # from django-snippets |
|
34 def slugify(inStr): |
|
35 removelist = ["a", "an", "as", "at", "before", "but", "by", "for","from","is", "in", "into", "like", "of", "off", "on", "onto","per","since", "than", "the", "this", "that", "to", "up", "via","with"]; |
|
36 for a in removelist: |
|
37 aslug = re.sub(r'\b'+a+r'\b','',inStr) |
|
38 aslug = re.sub('[^\w\s-]', '', aslug).strip().lower() |
|
39 aslug = re.sub('\s+', '-', aslug) |
|
40 return len(aslug) > 50 and '%s-%d' % (aslug[:43], randint(100000,999999)) or aslug |