forked from nathanborror/django-basic-apps
/
shortcuts.py
42 lines (34 loc) · 1.23 KB
/
shortcuts.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
import os.path
import hashlib
import datetime
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.http import HttpResponseRedirect
def build_filename(instance, filename):
"""
Converts an image filename to a hash.
"""
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
name = hashlib.md5('%s' % now).hexdigest()
ext = os.path.splitext(filename)
return os.path.join('%s/%s' % (instance._meta.app_label, instance._meta.module_name), '%s%s' % (name, ext[1]))
def render(request, *args, **kwargs):
"""
Simple wrapper for render_to_response.
"""
kwargs['context_instance'] = RequestContext(request)
return render_to_response(*args, **kwargs)
def redirect(request, obj=None):
"""
Simple wrapper for HttpResponseRedirect that checks the request for a
'next' GET parameter then falls back to a given object or url string.
"""
next = request.GET.get('next', None)
redirect_url = '/'
if next:
redirect_url = next
elif isinstance(obj, str):
redirect_url = obj
elif obj and hasattr(obj, 'get_absolute_url'):
redirect_url = obj.get_absolute_url()
return HttpResponseRedirect(redirect_url)