示例#1
0
 def save(self, force_insert=False, force_update=False, commit=True):
     zeros = len(self.cleaned_data['start_index']) - 1
     if not self.cleaned_data['price']:
         price = self.cleaned_data['model'].price
         service_interval = self.cleaned_data['model'].service_interval
     else:
         price = self.cleaned_data['price']
         service_interval = self.cleaned_data['service_interval']
     for n in range(int(self.cleaned_data['start_index']),
                    self.cleaned_data['end_index']+1):
         name = (self.cleaned_data['prefix'] + 
                 self.get_zeros(n, zeros) + str(n))
         tool = Tool(name = name, 
                     price = price, 
                     service_interval = service_interval,
                     model = self.cleaned_data['model'],
                     invoice_number = self.cleaned_data['invoice_number'],
                     secondary_name = self.cleaned_data['secondary_name'],
                     buy_date = self.cleaned_data['buy_date'],
                     container = self.cleaned_data['container'])
         tool.save()
         event = Event(event_type = "Oprettelse", tool = tool)
         event.save()
示例#2
0
def form(request, class_name, form_name):
    if not request.user.customer:
        return HttpResponseRedirect(reverse('admin_index'))

    if request.POST:
        obj_id = request.POST.get('id')

        if obj_id is not None:
            obj = get_object_or_404(class_name, id = obj_id)
            logger.info('%s is editing a %s (%s)' % (request.user, 
                                                     class_name.__name__,
                                                     obj.name))
            form = form_name(data = request.POST, instance = obj,
                             customer = request.user.customer)

            if form.is_valid():
                form.save()
                logger.info('%s edited successfully' % class_name.__name__)
                form = form_name(customer=request.user.customer)

                response = {'status': 'success',
                            'response': 'Objekt redigeret'}
            else:
                logger.info('%s not edited' % class_name.__name__)
                context = {'form': form,
                           'object_type': class_name.verbose_name}
                response = {'status': 'failure',
                            'response': render_to_string('form.html', RequestContext(request, context))}

        else:
            logger.info('%s is creating a %s (%s)' % (request.user,
                                                      class_name.__name__,
                                                      request.POST.get('name')))
            form = form_name(data=request.POST, customer=request.user.customer)
            if form.is_valid():
                obj = form.save()
                logger.info('%s successfully created' % class_name.__name__)

                if isinstance(obj, Tool):
                    event = Event(event_type="Oprettelse", tool=obj)
                    event.save()
                
                response = {'status': 'success',
                            'response': 'Objekt oprettet'}
            else:
                logger.info('%s not created' % class_name.__name__)
                context = {'form': form,
                           'object_type': class_name.verbose_name}
                response = {'status': 'failure',
                            'response': render_to_string('form.html', RequestContext(request, context))}

        return HttpResponse(simplejson.dumps(response), 
                            mimetype="application/json")

    obj_id = request.GET.get('id')

    if obj_id:
        obj = get_object_or_404(class_name, id = obj_id)
        form = form_name(instance=obj, customer=request.user.customer)
        context = {'form': form,
                   'object_type': obj.verbose_name,
                   'id': obj.id}
    else:
        form = form_name(request.user.customer)
        context = {'form': form,
                   'object_type': class_name.verbose_name}

    return render(request, 'form.html', context)