Esempio n. 1
0
def add_new_model(request, model_name):
    if (model_name.lower() == model_name):
        normal_model_name = model_name.capitalize()
    else:
        normal_model_name = model_name
    
    app_list = get_apps()
    for app in app_list:
        for model in get_models(app):
            if model.__name__ == normal_model_name:
                form = modelform_factory(model)
                
                if normal_model_name == 'Death_notice':
                    dn_name = model._meta.verbose_name
                    form = Death_noticeForm
                    service_form = ServiceFormSet
                    dn_os_formset = DeathNoticeOtherServicesFormSet
                
                if request.method == 'POST':
                    form = form(request.POST)
                    service_form = service_form(request.POST)
                    dn_os_formset = DeathNoticeOtherServicesFormSet(request.POST)
                    if form.is_valid() and service_form.is_valid() and dn_os_formset.is_valid():
                        try:
                            if normal_model_name == 'Death_notice':
                                new_obj = form.save(commit=False)
                                new_obj.funeral_home = request.user
                                new_obj.save()
                                service_form = ServiceFormSet(request.POST, instance=new_obj)
                                service_form.save()
                                dn_os_formset = DeathNoticeOtherServicesFormSet(request.POST, instance=new_obj)
                                dn_os_formset.save()
                            else:
                                new_obj = form.save()
                        except forms.ValidationError, error:
                            new_obj = None
                        
                        if new_obj:
                             return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script>' % \
                                    (escape(new_obj._get_pk_val()), escape(new_obj)))
                else:
                   form = form()
                
                page_context = {'form': form, 'service_form': service_form, 'other_services_formset': dn_os_formset, 'field': normal_model_name, 'dn_verbose_name': dn_name }
                return render_to_response('dn_popup.html', page_context, context_instance=RequestContext(request))
Esempio n. 2
0
def manage_death_notice(request, death_notice_id=None):
    if request.method == 'POST':
        if request.POST.has_key('delete_death_notice'):
            Death_notice.objects.filter(funeral_home__username=request.user.username).get(pk=death_notice_id).delete()
            msg = ugettext('The %(verbose_name)s was deleted.') %\
                { 'verbose_name': Death_notice._meta.verbose_name }
            messages.success(request, msg, fail_silently=True)
            return HttpResponseRedirect(reverse('death_notice_index'))
        
        if death_notice_id:
            death_notice = Death_notice.objects.get(pk=death_notice_id)
            form = Death_noticeForm(request.POST, request.FILES, instance=death_notice)
            formset = ServiceFormSet(request.POST, instance=death_notice)
            dn_os_formset = DeathNoticeOtherServicesFormSet(request.POST, instance=death_notice)
        else:
            form = Death_noticeForm(request.POST, request.FILES)
            formset = ServiceFormSet(request.POST)
            dn_os_formset = DeathNoticeOtherServicesFormSet(request.POST)
        
        if form.is_valid() and formset.is_valid() and dn_os_formset.is_valid():
            death_notice = form.save(commit=False)
            death_notice.funeral_home = request.user
            death_notice.save()
            formset = ServiceFormSet(request.POST, instance=death_notice)
            formset.save()
            dn_os_formset = DeathNoticeOtherServicesFormSet(request.POST, instance=death_notice)
            msg = ugettext('Death notice for %s %s saved.' % (death_notice.first_name, death_notice.last_name))
            messages.success(request, msg, fail_silently=True)
            dn_os_formset.save()
            if request.POST.has_key('add_another'):
                return HttpResponseRedirect(reverse('add_death_notice'))
            else:
                return HttpResponseRedirect(reverse('death_notice_index'))
    else:
        if death_notice_id:
            death_notice = Death_notice.objects.get(pk=death_notice_id)
            form = Death_noticeForm(instance=death_notice)
            formset = ServiceFormSet(instance=death_notice)
            dn_os_formset = DeathNoticeOtherServicesFormSet(instance=death_notice)
        else:
            form = Death_noticeForm()
            formset = ServiceFormSet(instance=Death_notice())
            dn_os_formset = DeathNoticeOtherServicesFormSet(instance=Death_notice())
    
    response_dict = {
        'form': form,
        'formset': formset,
        'other_services_formset': dn_os_formset,
        'death_notice_id': death_notice_id,
    }
    
    # To be used for Preview
    if death_notice_id:
        response_dict['object_list'] = [death_notice]
    
    t = loader.get_template('manage_death_notice.html')
    c = RequestContext(request, response_dict)
    data = t.render(c)
    r = HttpResponse(data)
    return r
    return render_to_response('manage_death_notice.html', response_dict, context_instance=RequestContext(request))