~mhall119/ubuntu-website/django-foundations

« back to all changes in this revision

Viewing changes to src/common/widgets.py

  • Committer: Michael Hall
  • Date: 2010-10-31 21:16:39 UTC
  • Revision ID: mhall119@gmail.com-20101031211639-ai39d8vdgz4rv42i
Begin incorporating pieces of loco-directory into base apps

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Modified copy of django.contrib.admin.widgets.RelatedFieldWidgetWrapper
 
2
# Removed reverences to admin_site, and instead accept a popup_url
 
3
 
 
4
from django import forms
 
5
from django.conf import settings
 
6
from django.utils.translation import ugettext as _
 
7
from django.utils.safestring import mark_safe
 
8
import copy
 
9
 
 
10
class PopupRelatedFieldWidgetWrapper(forms.Widget):
 
11
    """
 
12
    This class is a wrapper to a given widget to add the add icon for the
 
13
    admin interface.
 
14
    """
 
15
    def __init__(self, widget, popup_url):
 
16
        self.is_hidden = widget.is_hidden
 
17
        self.needs_multipart_form = widget.needs_multipart_form
 
18
        self.attrs = widget.attrs
 
19
        self.choices = widget.choices
 
20
        self.widget = widget
 
21
        self.popup_url = popup_url
 
22
 
 
23
    def __deepcopy__(self, memo):
 
24
        obj = copy.copy(self)
 
25
        obj.widget = copy.deepcopy(self.widget, memo)
 
26
        obj.attrs = self.widget.attrs
 
27
        memo[id(self)] = obj
 
28
        return obj
 
29
 
 
30
    def _media(self):
 
31
        wm = self.widget.media
 
32
        wm.add_js(['%sjs/admin/RelatedObjectLookups.js'%settings.ADMIN_MEDIA_PREFIX,])
 
33
        return wm
 
34
    media = property(_media)
 
35
 
 
36
    def render(self, name, value, *args, **kwargs):
 
37
 
 
38
        self.widget.choices = self.choices
 
39
        output = [self.widget.render(name, value, *args, **kwargs)]
 
40
        output.append(u'<a href="%s" class="add-another" id="add_id_%s" onclick="return showAddAnotherPopup(this);"> ' % \
 
41
            (self.popup_url, name))
 
42
        output.append(u'<img src="%simg/admin/icon_addlink.gif" width="10" height="10" alt="%s"/></a>' % (settings.ADMIN_MEDIA_PREFIX, _('Add Another')))
 
43
        return mark_safe(u''.join(output))
 
44
 
 
45
    def build_attrs(self, extra_attrs=None, **kwargs):
 
46
        "Helper function for building an attribute dictionary."
 
47
        self.attrs = self.widget.build_attrs(extra_attrs=None, **kwargs)
 
48
        return self.attrs
 
49
 
 
50
    def value_from_datadict(self, data, files, name):
 
51
        return self.widget.value_from_datadict(data, files, name)
 
52
 
 
53
    def _has_changed(self, initial, data):
 
54
        return self.widget._has_changed(initial, data)
 
55
 
 
56
    def id_for_label(self, id_):
 
57
        return self.widget.id_for_label(id_)
 
58