admin.py :  » Content-Management-Systems » PyLucid » PyLucid_standalone » dbpreferences » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Content Management Systems » PyLucid 
PyLucid » PyLucid_standalone » dbpreferences » admin.py
# coding: utf-8

"""
    dbpreferences.admin
    ~~~~~~~~~~~~~~~~~~~


    Last commit info:
    ~~~~~~~~~~~~~~~~~
    $LastChangedDate$
    $Rev$
    $Author$

    :copyleft: 2008 by the PyLucid team, see AUTHORS for more details.
    :license: GNU GPL v3 or above, see LICENSE for more details.
"""
from django.contrib import admin
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext
from django.conf.urls.defaults import patterns,url
from django.template.loader import render_to_string
from django.shortcuts import get_object_or_404,render_to_response

from dbpreferences.models import Preference
from dbpreferences.tools import forms_utils

#------------------------------------------------------------------------------

class PreferenceAdmin(admin.ModelAdmin):
    list_display = ("site", "app_label", "form_name", "lastupdatetime", "lastupdateby", "edit_link")
    list_display_links = ("form_name",)
    list_filter = ("site", "app_label",)
    search_fields = ("site", "app_label", "form_name",)

    def has_add_permission(self, request):
        """
        Deny add permission for every user. Becuase manually adding a
        preferences makes no sence, yet. (This hides the add button)
        
        Every user can only change existing preferences. Preferences entry are
        added automatically by first accessing the preferences form.
        
        TODO: If the forms must be registered, we can create all preferences
        into database on first startup.
        see also:
        http://code.google.com/p/django-dbpreferences/issues/detail?id=5
        """
        return False

    def edit_link(self, instance):
        """ For adding a edit link into django admin interface """
        context = {
            "instance": instance,
        }
        return render_to_string('dbpreferences/model_admin_edit_link.html', context)
    edit_link.allow_tags = True

    def edit_form(self, request, pk):
        """ edit a preference entry using the associated form """

        obj = get_object_or_404(Preference, pk=pk)
        form_class = obj.get_form_class()

        if request.method == 'POST':
            form = form_class(request.POST)
            if form.is_valid():
                # save new preferences
                obj.preferences = form.cleaned_data
                obj.lastupdateby = request.user
                obj.save()

                msg = "Preferences %s updated." % obj
                self.log_change(request, obj, msg)

                if request.POST.has_key("_continue"):
                    msg += ' ' + _("You may edit it again below.")

                    if request.REQUEST.has_key('_popup'):
                        next_url = request.path + "?_popup=1"
                    else:
                        next_url = request.path
                else:
                    next_url = reverse("admin:dbpreferences_preference_changelist")

                self.message_user(request, msg)
                return HttpResponseRedirect(next_url)
        else:
            preferences = obj.preferences
            form = form_class(preferences)

        # Append initial form values into all field help_text
        forms_utils.setup_help_text(form)

        context = {
            "title": _('Change %s') % obj,
            "obj": obj,
            "form_url": request.path,
            "form": form,
        }
        return render_to_response("dbpreferences/edit_form.html", context,
            context_instance=RequestContext(request))

    def get_urls(self):
        """ add own edit view into urls """
        urls = super(PreferenceAdmin, self).get_urls()
        my_urls = patterns('',
            url(r'^(?P<pk>\d+)/edit_form/$', self.admin_site.admin_view(self.edit_form),
            name="dbpref_edit_form")
        )
        return my_urls + urls



admin.site.register(Preference, PreferenceAdmin)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.