def recipient(request, context, recipientid=None): validationMsg ="" if not recipientid or int(recipientid) == 0: recipient = None else: recipient = Recipient.objects.get(id=recipientid) if request.method == 'POST': form = RecipientForm(request.POST) if form.is_valid(): if recipient: recipient.first_name = form.cleaned_data['firstName'] recipient.last_name = form.cleaned_data['lastName'] recipient.identity = form.cleaned_data['identity'] recipient.active = form.cleaned_data['active'] recipient.save() validationMsg =_(u"You have successfully updated the recipient") else: try: recipient = Recipient(first_name=form.cleaned_data['firstName'] ,\ last_name=form.cleaned_data['lastName'],\ identity=form.cleaned_data['identity'],\ active=form.cleaned_data['active'],\ site = context['user'].site) recipient.save() validationMsg = "You have successfully inserted a recipient %s." % form.cleaned_data['firstName'] except Exception, e : validationMsg = "Failed to add new recipient %s." % e #recipients = Recipient.objects.all() mycontext = {'validationMsg':validationMsg} context.update(mycontext) #return render_to_response(request, 'recipients_list.html', context) return redirect(list)
def manage_recipients(request,context): RecipientFormSet = formset_factory(BulkRecipientForm, extra=3) if request.method == 'POST': groupid = request.POST['group'] if groupid and groupid.__len__() > 0: group = Group.objects.get(id=groupid) #groupSite = Group.objects.get(site=context['user'].site) #print groupSite #if groupSite.id <> context['user'].site.id : #raise forms.ValidationError('Invalid user site : user site is different than entered site??') formset = RecipientFormSet(request.POST, request.FILES) if formset.is_valid(): try: for form in formset.forms: if form.is_valid() and form.cleaned_data: recipient = Recipient(first_name=form.cleaned_data['firstName'] ,\ last_name=form.cleaned_data['lastName'],\ identity=form.cleaned_data['identity'],\ active=form.cleaned_data['active'],\ site = context['user'].site) recipient.save() if groupid and groupid.__len__() > 0: group.recipients.add(recipient) except Exception, e : validationMsg = "Failed to add new recipient %s." % e raise mycontext = {} #{'validationMsg':validationMsg} context.update(mycontext) return redirect(list) else: print "ddsadsd"