gdebure

Untitled

Aug 19th, 2011
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. #####################
  2. # models.py
  3. #####################
  4.  
  5. from django.db import models
  6. from users.models import Employee
  7.  
  8. class Domain (models.Model):
  9.     '''A class to handle the domain for services'''
  10.     name = models.CharField(max_length=64)
  11.     is_active = models.BooleanField()
  12.     owner = models.ForeignKey(Employee)
  13.     description = models.TextField(null=True)
  14.    
  15.     class Meta:
  16.         ordering = ['name']
  17.  
  18.     def __unicode__(self):
  19.         return self.name
  20.        
  21.     def get_absolute_url(self):
  22.         return '/services/domains/' + str(self.id)
  23.        
  24.     def get_service_families(self):
  25.         return self.servicefamily_set.all()
  26.  
  27.  
  28. #####################
  29. # forms.py
  30. #####################
  31.  
  32. from services.models import Domain
  33. from django import forms
  34.  
  35. from guardian.shortcuts import assign, remove_perm, get_users_with_perms
  36.  
  37. class DomainForm(forms.ModelForm):
  38.    
  39.     class Meta:
  40.         model = Domain
  41.        
  42.     def save(self, commit=True):
  43.         domain = super(forms.ModelForm, self).save(commit=commit)
  44.        
  45.         # Remove existing rights on this domain
  46.         users_with_perms = get_users_with_perms(domain)
  47.         for user in users_with_perms:
  48.             remove_perms('update_domain',user,domain)
  49.        
  50.         # Assign the right to update this domain to the domain owner
  51.         assign('domains.update_domain', domain.owner.user, domain)
  52.        
  53.         return domain
  54.  
  55.  
  56. #####################
  57. # forms.py
  58. #####################
  59.  
  60.  
  61. from django.conf.urls.defaults import *
  62.  
  63. from django.views.generic import UpdateView
  64. from services.models import Domain
  65. from services.forms import DomainForm
  66.  
  67. urlpatterns = patterns('',
  68.     ##################################
  69.     # Domains
  70.     # .... (removed unnecessary lines)
  71.     (r'^domains/(?P<pk>\d+)/update/$', UpdateView.as_view( model=DomainForm, success_url='/services/domains/%(id)s' ), ),
  72.     # ...
  73.     )
Advertisement
Add Comment
Please, Sign In to add comment