This repository has been archived by the owner on Oct 21, 2018. It is now read-only.
/
helper_functions.py
executable file
·76 lines (69 loc) · 3.48 KB
/
helper_functions.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from django.db.models import AutoField
from django.db import models
from django.core.exceptions import PermissionDenied
from django.contrib import admin
class Callable:
def __init__(self, anycallable):
self.__call__ = anycallable
def copy_model_instance(obj):
""" Django snippit 1040
Create a copy of a model instance.
Works in model inheritance case where instance.pk = None is not good enough, since the subclass instance refers to the parent_link's primary key during save.
M2M relationships are currently not handled, i.e. they are not copied.
"""
initial = dict([(f.name, getattr(obj, f.name))
for f in obj._meta.fields
if not isinstance(f, AutoField) and\
not f in obj._meta.parents.values()])
return obj.__class__(**initial)
class Struct(object):
def __unicode__(self):
return ""
class CharNullField(models.CharField):
description = "CharField that stores NULL but returns ''"
def to_python(self, value): #this is the value right out of the db, or an instance
if isinstance(value, models.CharField): #if an instance, just return the instance
return value
if value==None: #if the db has a NULL (==None in Python)
return "" #convert it into the Django-friendly '' string
else:
return value #otherwise, return just the value
def get_db_prep_value(self, value, *args, **kwargs): #catches value right before sending to db
if value=="": #if Django tries to save '' string, send the db None (NULL)
return None
else:
return super(CharNullField, self).get_db_prep_value(value, *args, **kwargs)
class ReadPermissionModelAdmin(admin.ModelAdmin):
""" based on http://gremu.net/blog/2010/django-admin-read-only-permission/
Admin model that allows users to read
"""
def has_change_permission(self, request, obj=None):
if getattr(request, 'readonly', False):
return True
return super(ReadPermissionModelAdmin, self).has_change_permission(request, obj)
def changelist_view(self, request, extra_context=None):
try:
return super(ReadPermissionModelAdmin, self).changelist_view(
request, extra_context=extra_context)
except PermissionDenied:
pass
perm_name = '%s.view_%s' % (self.model._meta.app_label, unicode.lower(unicode(self.model._meta.object_name)))
if request.method == 'POST' or not perm_name in request.user.get_all_permissions():
# Only allow POST if export to xls and nothing evil
# It's not all that secure, but we assume authenticated users aren't trying to hack things.
if not (request.POST['action'] == 'export_simple_selected_objects' and len(request.POST) <= 4) :
raise PermissionDenied
request.readonly = True
return super(ReadPermissionModelAdmin, self).changelist_view(
request, extra_context=extra_context)
def change_view(self, request, object_id, extra_context=None):
try:
return super(ReadPermissionModelAdmin, self).change_view(
request, object_id, extra_context=extra_context)
except PermissionDenied:
pass
if request.method == 'POST':
raise PermissionDenied
request.readonly = True
return super(ReadPermissionModelAdmin, self).change_view(
request, object_id, extra_context=extra_context)