Пример #1
0
def catalog_add(request):
    response = {'error': 'ko'}
    if request.POST:
        form = CatalogForm(request.POST)
        if form.is_valid():
            item = form.save(commit=False)
            section, created = CatalogSection.objects.get_or_create(name=form.cleaned_data.get('section_label'),
                                                                    owner=request.user,
                                                                    defaults={'name': form.cleaned_data.get('section_label'),
                                                                              'owner': request.user})
            item.section = section
            item.owner = request.user
            item.save()
            response['error'] = 'ok'
            response['id'] = item.id
            response['label'] = item.label
            response['category'] = item.category
            response['category_label'] = item.get_category_display()
            response['unit_price'] = localize(item.unit_price)
            response['vat_rate'] = localize(item.vat_rate) or ''
            response['vat_rate_label'] = item.get_vat_rate_display() or ''
            response['section'] = item.section.id
            response['section_label'] = unicode(item.section)
        else:
            response['error_msg'] = []
            for key, msg in form.errors.items():
                response['error_msg'].append("%s : %s" % (unicode(form[key].label), " ".join(msg)))

    return HttpResponse(simplejson.dumps(response),
                        mimetype='application/javascript')
Пример #2
0
def catalog_edit(request):
    id = request.GET.get('id')
    item = get_object_or_404(CatalogItem, pk=id, owner=request.user)
    response = {'error': 'ko'}
    if request.POST:
        form = CatalogForm(request.POST, instance=item)
        form.fields['section_label'].required = False
        if form.is_valid():
            item = form.save()
            response['error'] = 'ok'
            response['id'] = item.id
            response['label'] = item.label
            response['category'] = item.category
            response['category_label'] = item.get_category_display()
            response['unit_price'] = localize(item.unit_price)
            response['vat_rate'] = localize(item.vat_rate) or ''
            response['vat_rate_label'] = item.get_vat_rate_display() or ''
            response['section'] = item.section.id
            response['section_label'] = unicode(item.section)
        else:
            response['error_msg'] = []
            for key, msg in form.errors.items():
                response['error_msg'].append("%s : %s" % (unicode(form[key].label), " ".join(msg)))

    return HttpResponse(simplejson.dumps(response),
                        mimetype='application/javascript')
Пример #3
0
def catalog_list(request):
    user = request.user
    catalog_sections = CatalogSection.objects.filter(owner=user)
    catalogForm = CatalogForm()
    sectionForm = SectionForm()
    sectionForm.fields['section'].queryset = CatalogSection.objects.filter(owner=user)
    sectionRenameForm = SectionRenameForm()

    return render_to_response('catalog/list.html',
                              {'active': 'business',
                               'title': _('Catalog'),
                               'catalog': catalog_sections,
                               'catalogForm': catalogForm,
                               'sectionForm': sectionForm,
                               'sectionRenameForm': sectionRenameForm},
                               context_instance=RequestContext(request))