Friday, May 30, 2014

Re: Problem hiding fields from admin page based on another fields value

from django.db import models
from django.contrib.auth.models import User
from django.forms import CheckboxSelectMultiple
from django.contrib import admin



class Enviroment (models.Model):
enviroment = models.AutoField (primary_key=True)
EnviromentName = models.CharField("Enviroment", max_length = 50)
def __unicode__(self):
return '%s' % (self.EnviromentName)

class ChangeType (models.Model):
change = models.AutoField (primary_key=True)
changeName = models.CharField(verbose_name="Type of change", max_length = 50)

def __unicode__(self):
return '%s' % (self.changeName)


class Status (models.Model):
status = models.AutoField (primary_key=True)
statusName = models.CharField("Status", max_length = 50)
def __unicode__(self):
return '%s' % (self.statusName)

class Resource (models.Model):
resource = models.AutoField (primary_key=True)
resourceFirstName = models.CharField("First name", max_length = 50)
resourceLastName = models.CharField("Last name", max_length = 50)
organization = models.CharField("Organization", max_length = 50)
resourcePhone = models.IntegerField("Phone number")
resourceRole = models.CharField("Role", max_length=50)
def __unicode__(self):
return '%s %s' % (self.resourceFirstName, self.resourceLastName)

class ReasonForChange(models.Model):
reasonForChange = models.AutoField (primary_key=True)
reasonForChangeName = models.CharField("Reason for change", max_length=50)
def __unicode__(self):
return '%s' % (self.reasonForChangeName)

class GoalOfChange (models.Model):
goalOfChange = models.AutoField(primary_key=True)
goalOfChangeName = models.CharField("Goal of change", max_length=50)
def __unicode__(self):
return '%s' % (self.goalOfChangeName)


class RequestForChange (models.Model):
formfield_overrides = {
models.ManyToManyField: {'widget': CheckboxSelectMultiple},
}
One = '1'
Two = '2'
Three = '3'
Four = '4'
EFFECT_CHOICES = (
(One, '1'),
(Two, '2'),
(Three, '3'),
(Four, '4'),
)
rfc = models.AutoField (primary_key=True, help_text="This is the grey text")
user = models.ForeignKey(User, verbose_name="Change owner")
changeId = models.IntegerField("Frontrange ID", blank=True, null=True)
TFSId = models.IntegerField("TFS ID", blank=True, null=True)
enviroment = models.ForeignKey(Enviroment, verbose_name="Enviroment")
status = models.ForeignKey(Status, verbose_name="Status")
heading = models.CharField("Heading", max_length=50)
reasonForChange = models.ManyToManyField(ReasonForChange, verbose_name="Reason for change", blank=True, null=True)
reasonForChange = models.ManyToManyField(ReasonForChange, verbose_name="Reason for change", blank=True, null=True)
goalOfChange = models.ManyToManyField(GoalOfChange, verbose_name="Goal of change test",blank=True, null=True )
rfcDateCreated = models.DateField("Date created")
acceptAT = models.NullBooleanField("Is the change accepted by customer?", blank=True, null=True)
testReport = models.NullBooleanField("Do we have a testreport?", blank=True, null=True)
critical = models.NullBooleanField("Time critical", blank=True, null=True)
criticalDate = models.DateField("Critical date", blank=True, null=True)
CriticalDateDescription = models.TextField("Description", max_length=250, blank=True, null=True)
effect = models.CharField(choices=EFFECT_CHOICES, verbose_name="Effect", max_length=2, blank=True, null=True)
urgency = models.CharField(choices=EFFECT_CHOICES, verbose_name="Effect", max_length=2, blank=True, null=True)
priority = models.IntegerField("Priority", blank=True, null=True)
priorityComments = models.TextField("Comments", max_length=100, blank=True, null=True)
budget = models.TextField("Description of budget", max_length=250, blank=True, null=True)
estimatedTime = models.CharField("Estimated time for deploy of change", max_length=30, blank=True, null=True)
changeType = models.ForeignKey(ChangeType, verbose_name="Type of change")
resource = models.ManyToManyField(Resource, verbose_name="Responsible for change?", blank=True, null=True)
stakeholder = models.ManyToManyField(Resource, related_name='stakeholder', verbose_name="Stakeholders", blank=True, null=True)
effectOnEnviroments = models.ManyToManyField(Enviroment, related_name="EffectOnEnviroment", verbose_name="Enviroment this change effects", blank=True, null=True)
effectOnOtherChanges = models.TextField("Effect on other changes", max_length=100, blank=True, null=True)
effectOnUsers = models.TextField("Effect on users", max_length=100, blank=True, null=True)
effectOnSecurityPolicy = models.TextField("Risk and security policy", max_length=100, blank=True, null=True)
consIfNotImplemented = models.TextField("Consequences if the change is not implemented", max_length=250, blank=True, null=True)
codeChange = models.NullBooleanField("Is this a change in code", blank=True, null=True)
codeChangePerson = models.CharField("Who has done the code quality", max_length=250, blank=True, null=True)
manualOperationOnDeploy = models.IntegerField("Number of manual operations on intstalation", blank=True, null=True)
otherCriticalSucessFactors = models.TextField("Other critical sucess factors", max_length=250, blank=True, null=True)

installationDescription = models.FileField(upload_to='documents/%Y/%m/%d', verbose_name="Installation description", blank=True, null=True)
rollBackPlan = models.TextField("Rollback plan", max_length=250, blank=True, null=True)
lastRollbackTime = models.DateField("Point of no return", blank=True, null=True)
rollBackOwner = models.ForeignKey(Resource, verbose_name="Owner of roleback", related_name="rollBackOwner")
rollBackTested = models.NullBooleanField("Is the rollback tested?", blank=True, null=True)
resourcesForChange = models.ManyToManyField(Resource, related_name='resourcesForChange', verbose_name="Resources needed for completion of change", blank=True, null=True)
informationRequirements = models.NullBooleanField("Will the change require information to users?", blank=True, null=True)
informationRequirementsDescription = models.TextField("Description of information requirements", max_length=250, blank=True, null=True)
readiness = models.NullBooleanField("Readiness/stand by", blank=True, null=True)
readinessDescription = models.TextField("Description of readiness / stand by", max_length=250, blank=True, null=True)
learningRequirements = models.NullBooleanField("Will the change require education? ", blank=True, null=True)
learningDescription = models.TextField("Description of education", max_length=250, blank=True, null=True)



def __unicode__(self):
return '%s %s' % (self.rfc, self.heading)




from django.contrib import admin
from django.db import models
from RequestForChange.models import Enviroment
from RequestForChange.models import ChangeType
from RequestForChange.models import Resource
from RequestForChange.models import Status, RequestForChange, ReasonForChange, GoalOfChange
import time
from django.forms import CheckboxSelectMultiple
from django import forms
from django.forms import ModelForm, Textarea


class ChangeTypeAdmin(admin.ModelAdmin):
list_display = ('change', 'changeName')


class RequestForChangeInline(admin.TabularInline):
model = RequestForChange

class RequestForChangeForm(forms.ModelForm):
class Meta:
model = RequestForChange
#exclude = ['heading',]

class RequestForChangeAdminForm(forms.ModelForm):
class Meta:
model = RequestForChange


class RequestForChangeAdmin(admin.ModelAdmin):
formfield_overrides = {
models.ManyToManyField: {'widget': CheckboxSelectMultiple},
}
list_display = ('rfc', 'heading', 'enviroment', 'status')
search_fields = ('changeId',)
list_filter = ('enviroment', 'acceptAT', 'status')
date_hierarchy = 'criticalDate'
inline = [RequestForChangeInline]

fieldsets = (
('RFC basic information', {
'fields': ('changeId', 'TFSId', 'rfcDateCreated', 'changeType','status', 'enviroment','heading', 'reasonForChange', 'goalOfChange', 'acceptAT', 'testReport'),
}),
('Roles', {
'classes': ('collapse',),
'fields': ('user', 'resource', 'stakeholder', ),
}),

('Priority and criticality', {
'classes': ('collapse',),
'fields': (('effect', 'urgency', 'priority', 'priorityComments'), ('critical','criticalDate', 'CriticalDateDescription'), ),
}),
('Risk evaluation', {
'classes': ('collapse',),
'fields': ( 'effectOnEnviroments', 'effectOnOtherChanges', 'effectOnUsers',
'effectOnSecurityPolicy', 'consIfNotImplemented', ('codeChange', 'codeChangePerson'),
'manualOperationOnDeploy', 'otherCriticalSucessFactors')
}),
('Completion', {
'classes': ('collapse',),
'fields': ( 'installationDescription', 'rollBackPlan', 'lastRollbackTime', 'rollBackOwner', 'rollBackTested', 'resourcesForChange')
}),
('Documentation', {
'classes': ('collapse',),
'fields': ( 'informationRequirements', 'informationRequirementsDescription', 'readiness', 'readinessDescription', 'learningRequirements', 'learningDescription')
}),
)




readonly_fields = ('heading','enviroment',)

#def get_form(self, request, obj=None, **kwargs):
# if request.user.is_superuser:
# self.readonly_fields = ()
# else:
# for group in request.user.groups.all():
# if str(group) == 'Hasteendring':
# return RequestForChangeForm
# return super(RequestForChangeAdmin, self).get_form(request, obj, **kwargs)
#return RequestForChangeAdminForm

def get_form(self, request, obj=None, **kwargs):
form = super(RequestForChangeAdmin, self).get_form(request, obj, **kwargs)
print "HEIA"
if obj.rfc:
print "HEIA"
self.exclude = ("readinessDescription", )
form = super(RequestForChangeAdmin, self).get_form(request, obj, **kwargs)
return form


#filter_horizontal = ('stakeholder', 'goalOfChange', 'reasonForChange_temp')




admin.site.register(Enviroment)
admin.site.register(ChangeType, ChangeTypeAdmin)
admin.site.register(Resource)
admin.site.register(Status)
admin.site.register(ReasonForChange)
admin.site.register(GoalOfChange)
admin.site.register(RequestForChange, RequestForChangeAdmin)
I have uploaded my model and admin file and hope someone can tell me why I can't get my get_form method to work :)

Regards,
Hilde

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/494dcd61-e77f-4844-8243-d838169bea5d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment