rendered paste bodyfrom django.shortcuts import render_to_responsefrom django.template import RequestContextfrom django.template.loader import render_to_stringfrom intranet.forms import ImgErrorListfrom django import newforms as formsfrom intranet.information.models import Company, Positionfrom django.core.mail import EmailMessagefrom django.http import HttpResponseRedirect, Http404from django.core.urlresolvers import reversefrom intranet.forms import YesNoBooleanField### FORMS ### class FormBase(forms.Form): action_date = forms.CharField() other = forms.CharField() contact_name = forms.CharField() contact_phone = forms.CharField() contact_company = forms.ChoiceField(choices=[tuple(x.values()) for x in Company.objects.all().values('id', 'company_name').order_by('company_name')]) first_name = forms.CharField() last_name = forms.CharField() company = forms.ChoiceField(choices=[tuple(x.values()) for x in Company.objects.all().values('id', 'company_name').order_by('company_name')]) position = forms.ChoiceField(choices=[tuple(x.values()) for x in Position.objects.all().values('id', 'title').order_by('title')]) reporting_to = forms.CharField() replacement = forms.BooleanField(widget=forms.CheckboxInput(attrs={'class': 'check'})) replacing = forms.CharField(required=False) def clean(self): #raise Exception if self.cleaned_data['replacement'] == True and self.cleaned_data['replacing'] == '': raise forms.ValidationError(u'If the employee is replacing another then you must specify who is being replaced.') return self.cleaned_data class NewForm(FormBase): #previously_employed = forms.BooleanField(widget=forms.RadioSelect(renderer=InlineRadioRenderer, choices=yes_no_choices, attrs={'class': 'radio'})) previously_employed = forms.YesNoBooleanField() class ChangeForm(FormBase): pay_change = forms.BooleanField(widget=forms.CheckboxInput(attrs={'class': 'check'})) new_position = forms.ChoiceField(choices=[tuple(x.values()) for x in Position.objects.all().values('id', 'title').order_by('title')]) class TransferForm(FormBase): new_company = forms.ChoiceField(choices=[tuple(x.values()) for x in Company.objects.all().values('id', 'company_name').order_by('company_name')]) new_position = forms.ChoiceField(choices=[tuple(x.values()) for x in Position.objects.all().values('id', 'title').order_by('title')]) pay_change = forms.BooleanField(widget=forms.CheckboxInput(attrs={'class': 'check'})) ### VIEWS ### def do_notification(request, action): if action == 'new': frm = NewForm template = 'early_notification/new_employee.html' title_string = 'New Employee' email_template = 'early_notification/new_employee_email.txt' elif action == 'change': frm = ChangeForm template = 'early_notification/change_position.html' title_string = 'Employee Position Change' email_template = 'early_notification/change_position_email.txt' elif action == 'transfer': frm = TransferForm template = 'early_notification/transfer.html' title_string = 'Employee Transfer' email_template = 'early_notification/transfer_email.txt' elif action == 'termination': frm = FormBase template = 'early_notification/termination.html' title_string = 'Employee Termination' email_template = 'early_notification/termination_email.txt' else: raise Http404 if request.method == 'POST': f = frm(request.POST, error_class=ImgErrorList) if f.is_valid(): f.cleaned_data['company'] = Company.objects.get(pk=f.cleaned_data['company']).company_name f.cleaned_data['contact_company'] = Company.objects.get(pk=f.cleaned_data['contact_company']).company_name if 'new_company' in f.cleaned_data: f.cleaned_data['new_company'] = Company.objects.get(pk=f.cleaned_data['new_company']).company_name f.cleaned_data['position'] = Position.objects.get(pk=f.cleaned_data['position']).title if 'new_position' in f.cleaned_data: f.cleaned_data['new_position'] = Position.objects.get(pk=f.cleaned_data['new_position']).title f.cleaned_data['replacement'] = 'Yes' if 'replacement' in f.cleaned_data else 'No' f.cleaned_data['previously_employed'] = 'Yes' if 'previously_employed' in f.cleaned_data else 'No' f.cleaned_data['pay_change'] = 'Yes' if 'pay_change' in f.cleaned_data else 'No' email_body = render_to_string(email_template, f.cleaned_data) arguments = { 'subject': "Early Notification - %s - %s %s" % (title_string, f.cleaned_data['first_name'], f.cleaned_data['last_name']), 'body': email_body, 'from_email': 'intranet@cmv.com.au', 'to': ['matt.magin@cmv.com.au'], 'headers': {'Reply-To': 'matt.magin@cmv.com.au'} } email = EmailMessage(**arguments) email.send() return HttpResponseRedirect("/early_notification/thanks/") else: f = frm(error_class=ImgErrorList) return render_to_response(template, {'form': f}, context_instance=RequestContext(request))