Skip to content

Commit

Permalink
improve installation process by providing more information when the r…
Browse files Browse the repository at this point in the history
…equired template sets are not available the application should not throw unhandled exceptions but rather providing the user some feedback how to fix the issue #100
  • Loading branch information
scaphilo committed Jan 28, 2018
1 parent 850b839 commit cfffe68
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 6 deletions.
8 changes: 8 additions & 0 deletions koalixcrm/crm/documents/contract.py
Expand Up @@ -17,6 +17,7 @@
from koalixcrm.crm.const.purpose import *
from koalixcrm.crm.documents.invoice import InlineInvoice
from koalixcrm.crm.documents.quote import InlineQuote
from koalixcrm.crm.exceptions import *


class PostalAddressForContract(PostalAddress):
Expand Down Expand Up @@ -114,6 +115,13 @@ class Meta:
verbose_name = _('Contract')
verbose_name_plural = _('Contracts')

def get_template_set(self, calling_model):
if self.default_template_set:
required_template_set = str(type(calling_model).__name__)
return self.default_template_set.get_template_set(required_template_set)
else:
raise TemplateSetMissing("The Contract has no Default Template Set selected")

def create_invoice(self):
invoice = Invoice()
invoice.create_invoice(self)
Expand Down
2 changes: 1 addition & 1 deletion koalixcrm/crm/documents/deliverynote.py
Expand Up @@ -13,7 +13,7 @@ class DeliveryNote(SalesDocument):
def create_delivery_note(self, calling_model):
self.create_sales_document(calling_model)
self.status = 'C'
self.template_set = self.contract.default_template_set.delivery_note_template
self.template_set = self.contract.get_template_set(self)
self.save()
self.attach_sales_document_positions(calling_model)
self.attach_text_paragraphs()
Expand Down
2 changes: 1 addition & 1 deletion koalixcrm/crm/documents/invoice.py
Expand Up @@ -30,7 +30,7 @@ def create_invoice(self, calling_model):
self.payable_until = date.today() + \
timedelta(days=self.customer.defaultCustomerBillingCycle.time_to_payment_date)
self.date_of_creation = date.today().__str__()
self.template_set = self.contract.default_template_set.invoice_template
self.template_set = self.contract.get_template_set(self)
self.save()
self.attach_sales_document_positions(calling_model)
self.attach_text_paragraphs()
Expand Down
2 changes: 1 addition & 1 deletion koalixcrm/crm/documents/paymentreminder.py
Expand Up @@ -23,7 +23,7 @@ def create_payment_reminder(self, calling_model):
self.iteration_number = 1
self.payable_until = date.today() + \
timedelta(days=self.customer.defaultCustomerBillingCycle.payment_reminder_time_to_payment)
self.template_set = self.contract.default_template_set.payment_reminder_template
self.template_set = self.contract.get_template_set(self)
self.save()
self.attach_sales_document_positions(calling_model)
self.attach_text_paragraphs()
Expand Down
2 changes: 1 addition & 1 deletion koalixcrm/crm/documents/purchaseconfirmation.py
Expand Up @@ -9,7 +9,7 @@ class PurchaseConfirmation(SalesDocument):

def create_purchase_confirmation(self, calling_model):
self.create_sales_document(calling_model)
self.template_set = self.contract.default_template_set.purchase_confirmation_template
self.template_set = self.contract.get_template_set(self)
self.save()
self.attach_sales_document_positions(calling_model)
self.attach_text_paragraphs()
Expand Down
2 changes: 1 addition & 1 deletion koalixcrm/crm/documents/purchaseorder.py
Expand Up @@ -14,7 +14,7 @@ class PurchaseOrder(SalesDocument):
def create_purchase_order(self, calling_model):
self.create_sales_document(calling_model)
self.status = 'O'
self.template_set = self.contract.default_template_set.purchase_order_template
self.template_set = self.contract.get_template_set(self)
self.save()
self.attach_sales_document_positions(calling_model)
self.attach_text_paragraphs()
Expand Down
2 changes: 1 addition & 1 deletion koalixcrm/crm/documents/quote.py
Expand Up @@ -18,7 +18,7 @@ def create_quote(self, calling_model):
self.status = 'I'
self.valid_until = date.today().__str__()
self.date_of_creation = date.today().__str__()
self.template_set = self.contract.default_template_set.quote_template
self.template_set = self.contract.get_template_set(self)
self.save()
self.attach_sales_document_positions(calling_model)
self.attach_text_paragraphs()
Expand Down
45 changes: 45 additions & 0 deletions koalixcrm/crm/views.py
Expand Up @@ -54,3 +54,48 @@ def export_pdf(calling_model_admin, request, document, redirect_to):
else:
raise Http404
return response

def create_new_document(calling_model_admin, request, document, redirect_to):
"""This method exports PDFs provided by different Models in the crm application
Args:
calling_model_admin (ModelAdmin): The calling ModelAdmin must be provided for error message response.
request: The request User is to know where to save the error message
document (Contract): The model from which a new document shall be created
redirect_to (str): String that describes to where the method should redirect in case of an error
Returns:
HTTpResponse with a PDF when successful
HTTpResponseRedirect when not successful
Raises:
raises Http404 exception if anything goes wrong"""
try:
pdf = document.create_pdf()
response = HttpResponse(FileWrapper(open(pdf, 'rb')), content_type='application/pdf')
response['Content-Length'] = path.getsize(pdf)
except (TemplateSetMissing, UserExtensionMissing, CalledProcessError, UserExtensionEmailAddressMissing, UserExtensionPhoneAddressMissing) as e:
if isinstance(e, UserExtensionMissing):
response = HttpResponseRedirect(redirect_to)
calling_model_admin.message_user(request, _("User Extension Missing"))
elif isinstance(e, UserExtensionEmailAddressMissing):
response = HttpResponseRedirect(redirect_to)
calling_model_admin.message_user(request, _("User Extension Email Missing"))
elif isinstance(e, UserExtensionPhoneAddressMissing):
response = HttpResponseRedirect(redirect_to)
calling_model_admin.message_user(request, _("User Extension Phone Missing"))
elif isinstance(e, TemplateSetMissing):
response = HttpResponseRedirect(redirect_to)
calling_model_admin.message_user(request, _("Templateset Missing"))
elif isinstance(e, TemplateFOPConfigFileMissing):
response = HttpResponseRedirect(redirect_to)
calling_model_admin.message_user(request, _("Fop Config File Missing in TemplateSet"))
elif isinstance(e, TemplateXSLTFileMissing):
response = HttpResponseRedirect(redirect_to)
calling_model_admin.message_user(request, _("XSLT File Missing in TemplateSet"))
elif type(e) == CalledProcessError:
response = HttpResponseRedirect(redirect_to)
calling_model_admin.message_user(request, e.output)
else:
raise Http404
return response
17 changes: 17 additions & 0 deletions koalixcrm/djangoUserExtension/models.py
Expand Up @@ -127,6 +127,23 @@ class Meta:
def __str__(self):
return xstr(self.id) + ' ' + xstr(self.title)

def get_template_set(self, required_template_set):
mapping_class_to_templates = {"Invoice": self.invoice_template,
"Quote": self.quote_template,
"DeliveryNote": self.delivery_note_template,
"PaymentReminder": self.payment_reminder_template,
"PurchaseConfirmation": self.purchase_confirmation_template,
"PurchaseOrder": self.purchase_order_template,
"ProfitLossStatement": self.profit_loss_statement_template,
"BalanceSheet": self.balance_sheet_statement_template}
try:
if mapping_class_to_templates[required_template_set]:
return mapping_class_to_templates[required_template_set]
else:
raise TemplateSetMissing("The TemplateSet does not contain a template for " +
required_template_set)
except KeyError:
raise IncorrectUseOfAPI("")

class UserExtensionPostalAddress(crm.contact.postaladdress.PostalAddress):
purpose = models.CharField(verbose_name=_("Purpose"), max_length=1, choices=PURPOSESADDRESSINUSEREXTENTION)
Expand Down

0 comments on commit cfffe68

Please sign in to comment.