def edit(request, pk=None, order_id=None, parent=None, recipient=None, customer=None): """ Edit a note. @FIXME: Should split this up into smaller pieces """ to = [] order = None command = _('Save') note = Note(order_id=order_id) excluded_emails = note.get_excluded_emails() if recipient is not None: to.append(recipient) command = _('Send') if order_id is not None: order = get_object_or_404(Order, pk=order_id) if order.user and (order.user != request.user): note.is_read = False if order.user.email not in excluded_emails: to.append(order.user.email) if order.customer is not None: customer = order.customer_id if customer is not None: customer = get_object_or_404(Customer, pk=customer) note.customer = customer if order_id is None: to.append(customer.email) tpl = template.Template(note.subject) note.subject = tpl.render(template.Context({'note': note})) note.recipient = ', '.join(to) note.created_by = request.user note.sender = note.get_default_sender() fields = escalations.CONTEXTS try: note.escalation = Escalation(created_by=request.user) except Exception as e: messages.error(request, e) return redirect(request.META['HTTP_REFERER']) AttachmentFormset = modelformset_factory(Attachment, fields=('content', ), can_delete=True, extra=3, exclude=[]) formset = AttachmentFormset(queryset=Attachment.objects.none()) if pk is not None: note = get_object_or_404(Note, pk=pk) formset = AttachmentFormset(queryset=note.attachments.all()) if parent is not None: parent = get_object_or_404(Note, pk=parent) note.parent = parent note.body = parent.quote() if parent.subject: note.subject = _(u'Re: %s') % parent.clean_subject() if parent.sender not in excluded_emails: note.recipient = parent.sender if parent.order: order = parent.order note.order = parent.order note.customer = parent.customer note.escalation = parent.escalation note.is_reported = parent.is_reported title = note.subject form = NoteForm(instance=note) if note.escalation: contexts = json.loads(note.escalation.contexts) escalation_form = EscalationForm(prefix='escalation', instance=note.escalation) if request.method == "POST": escalation_form = EscalationForm(request.POST, prefix='escalation', instance=note.escalation) if escalation_form.is_valid(): note.escalation = escalation_form.save() form = NoteForm(request.POST, instance=note) if form.is_valid(): note = form.save() formset = AttachmentFormset(request.POST, request.FILES) if formset.is_valid(): files = formset.save(commit=False) for f in files: f.content_object = note try: f.save() except ValueError as e: messages.error(request, e) return redirect(note) note.attachments.add(*files) if form.cleaned_data.get('attach_confirmation'): from servo.views.order import put_on_paper response = put_on_paper(request, note.order_id, fmt='pdf') filename = response.filename content = response.render().content content = ContentFile(content, filename) attachment = Attachment(content=content, content_object=note) attachment.save() attachment.content.save(filename, content) note.attachments.add(attachment) note.save() try: msg = note.send_and_save(request.user) messages.success(request, msg) except ValueError as e: messages.error(request, e) return redirect(note) return render(request, "notes/form.html", locals())
def create_order(request): try: data = json.loads(request.body) except ValueError as e: return client_error('Malformed request: %s' % e) cdata = data.get('customer') problem = data.get('problem') if not cdata: return client_error('Cannot create order without customer info') if not problem: return client_error('Cannot create order without problem description') try: customer, created = Customer.objects.get_or_create( name=cdata['name'], email=cdata['email'] ) except Exception as e: return client_error('Invalid customer details: %s' % e) if request.user.customer: customer.parent = request.user.customer if cdata.get('city'): customer.city = cdata.get('city') if cdata.get('phone'): customer.phone = cdata.get('phone') if cdata.get('zip_code'): customer.zip_code = cdata.get('zip_code') if cdata.get('street_address'): customer.street_address = cdata.get('street_address') customer.save() order = Order(created_by=request.user, customer=customer) order.save() note = Note(created_by=request.user, body=problem, is_reported=True) note.order = order note.save() if data.get('attachment'): import base64 from servo.models import Attachment from django.core.files.base import ContentFile attachment = data.get('attachment') try: filename = attachment.get('name') content = base64.b64decode(attachment.get('data')) except Exception as e: return client_error('Invalid file data: %s' %e) content = ContentFile(content, filename) attachment = Attachment(content=content, content_object=note) attachment.save() attachment.content.save(filename, content) note.attachments.add(attachment) if data.get('device'): try: GsxAccount.default(request.user) except Exception as e: pass ddata = data.get('device') try: device = order.add_device_sn(ddata.get('sn'), request.user) except Exception as e: device = Device(sn=ddata.get('sn', '')) device.description = ddata.get('description', '') device.save() order.add_device(device) for a in ddata.get('accessories', []): a = Accessory(name=a, order=order, device=device) a.save() return ok(order.code)