def apply_rules(event): rules = cache.get('rules', get_rules()) for r in rules: if (r['event'] == event.action) and (r['match'] == event.description): if r['action'] == "set_queue": order = event.content_object order.set_queue(r['data'], event.triggered_by) if r['action'] == "send_sms": number = 0 order = event.content_object try: number = order.customer.get_standard_phone() except Exception as e: continue note = Note(order=order, created_by=event.triggered_by) note.body = r['data'] note.render_body({'order': order}) note.save() return note.send_sms(number, event.triggered_by)
def copy(request, pk): """Copy a note with its attachments and labels.""" note = get_object_or_404(Note, pk=pk) new_note = Note(created_by=request.user) new_note.body = note.body new_note.order = note.order new_note.subject = note.subject new_note.save() new_note.labels = note.labels.all() for a in note.attachments.all(): # also copy the attachments a.pk = None a.content_object = new_note a.save() new_note.attachments.add(a) return redirect(edit, pk=new_note.pk, order_id=note.order_id)
def copy(request, pk): """ Copies a note with its attachments and labels """ from servo.lib.shorturl import from_time note = get_object_or_404(Note, pk=pk) new_note = Note(created_by=request.user) new_note.body = note.body new_note.order = note.order new_note.subject = note.subject new_note.save() new_note.labels = note.labels.all() for a in note.attachments.all(): a.pk = None a.content_object = new_note a.save() new_note.attachments.add(a) return redirect(edit, pk=new_note.pk, order_id=note.order_id)
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 apply_rules(event): """ Applies configured rules event is the Event object that was triggered """ counter = 0 rules = cache.get('rules', get_rules()) order = event.content_object user = event.triggered_by for r in rules: match = r.get('match', event.description) if (r['event'] == event.action and match == event.description): if isinstance(r['data'], dict): tpl_id = r['data']['template'] r['data'] = Template.objects.get(pk=tpl_id).render(order) else: r['data'] = Template(content=r['data']).render(order) if r['action'] == "set_queue": order.set_queue(r['data'], user) if r['action'] == "set_priority": pass if r['action'] == "send_email": try: email = order.customer.valid_email() except Exception: continue # skip customers w/o valid emails note = Note(order=order, created_by=user) note.body = r['data'] note.recipient = email note.render_subject({'note': note}) note.save() try: note.send_mail(user) except ValueError as e: print('Sending email failed (%s)' % e) if r['action'] == "send_sms": number = 0 try: number = order.customer.get_standard_phone() except Exception: continue # skip customers w/o valid phone numbers note = Note(order=order, created_by=user) note.body = r['data'] note.save() try: note.send_sms(number, user) except ValueError as e: print('Sending SMS to %s failed (%s)' % (number, e)) counter += 1 return '%d/%d rules processed' % (counter, len(rules))
def edit(request, pk=None, order_id=None, parent=None, recipient=None, customer=None): """ Edits 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) 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())