Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. """
  2. If you want to be more extandable with Django Architecture cover your models with a layer.
  3. Instead of using your model use helper classes everywhere.
  4. I named the class that covers a model ModelController.
  5. I have covered only create and update methods now.
  6. """
  7.  
  8. # company.py
  9. class Company(BaseModel):
  10. name = models.CharField(max_length=200, unique=True, db_index=True)
  11.  
  12. guid = models.CharField(max_length=64, unique=True, db_index=True)
  13. username = models.CharField('WC Username', max_length=32, db_index=True,
  14. help_text='The username that Web Connector will use.')
  15. password = models.CharField('WC Password', max_length=100,
  16. help_text='The password that Web Connector will use.')
  17.  
  18. connected = models.BooleanField(default=False)
  19. connected_at = models.DateTimeField(null=True, blank=True)
  20.  
  21. invoice_item = models.CharField('Invoice Item', max_length=31, blank=True,
  22. help_text='QuickBooks item used on the invoice.')
  23.  
  24. qwc_file = models.FileField('QWC File', blank=True, upload_to=get_qwc_filename,
  25. help_text='Web Connector configuration file.')
  26. qwc_file_id = models.CharField('QWC FileID', max_length=38, blank=True)
  27.  
  28. class Meta:
  29. verbose_name = 'Company'
  30. verbose_name_plural = 'Companies'
  31.  
  32. def __str__(self):
  33. return self.name
  34.  
  35. def save(self, *args, **kwargs):
  36. if not self.pk:
  37. if not self.guid:
  38. self.guid = uuid.uuid4().hex,
  39.  
  40. super(Company, self).save(*args, **kwargs)
  41.  
  42. def check_password(self, plain_password):
  43. return plain_password == self.password
  44.  
  45.  
  46. # company_controller.py:
  47. class CompanyController:
  48. """ A helper class for Company model """
  49. def __init__(self, company):
  50. self.company = company
  51.  
  52. @classmethod
  53. def create(cls, **kwargs):
  54. carrier = kwargs.get('carrier')
  55. return Company.objects.create(
  56. carrier=carrier,
  57. name=carrier.name,
  58. guid=kwargs.get('guid', uuid.uuid4().hex),
  59. username=kwargs.get('username', uuid.uuid4().hex),
  60. password=kwargs.get('password', str(randint(2000, 8000))),
  61. invoice_item=kwargs.get('invoice_item', 'Service'),
  62. connected=kwargs.get('connected', False),
  63. )
  64.  
  65. def update(self, **kwargs):
  66. update_fields = []
  67.  
  68. if 'connected' in kwargs:
  69. update_fields.append('connected')
  70. self.company.connected = kwargs.get('connected')
  71.  
  72. if self.company.connected:
  73. self.company.connected_at = timezone.now()
  74. update_fields.append('connected_at')
  75.  
  76. if 'invoice_item' in kwargs:
  77. update_fields.append('invoice_item')
  78. self.company.invoice_item = kwargs.get('invoice_item')
  79.  
  80. if 'qwc_file_id' in kwargs:
  81. update_fields.append('qwc_file_id')
  82. self.company.qwc_file_id = kwargs.get('qwc_file_id')
  83.  
  84. if 'qwc_file' in kwargs:
  85. update_fields.append('qwc_file')
  86. self.company.qwc_file.save(
  87. 'mysuperdispatch-{}.qwc'.format(randint(10, 99)),
  88. ContentFile(kwargs.get('qwc_file'))
  89. )
  90.  
  91. if update_fields:
  92. self.company.save(update_fields=update_fields)
  93.  
  94. return self.company
  95.  
  96. def create_qwc_file(self):
  97. """
  98. Create QuickBooks Web Connector configuration file.
  99. OwnerID should be per web service - constant across multiple QWC files.
  100. Needed if app needs to store private data in the company file, e.g.
  101. for checking whether we had communicated with this company file before.
  102. FileID is stored with the company file along OwnerID.
  103. """
  104. qwc_file_template = 'quickbooks_desktop/mysuperdispatch.qwc'
  105. qwc_file_id = '{' + str(uuid.uuid4()).upper() + '}'
  106.  
  107. context = {
  108. 'company': self.company,
  109. 'qwc_file_id': qwc_file_id,
  110. 'owner_id': settings.QUICKBOOKS_OWNER_ID,
  111. }
  112. file_content = render_to_string(qwc_file_template, context).encode('utf-8')
  113.  
  114. self.update(qwc_file_id=qwc_file_id, qwc_file=file_content)
  115.  
  116.  
  117. # views.py
  118. class CreateConnectionView(DispatcherTokenAuthMixin, APIView):
  119. def post(self, request, *args, **kwargs):
  120. try:
  121. if self.company and not self.company.connected:
  122. company = CompanyController(company=company).update(connected=True)
  123.  
  124. if not company:
  125. company = CompanyController.create(name=self.company_name)
  126.  
  127. CompanyController(company=company).create_qwc_file()
  128.  
  129. except Exception as e:
  130. logger.exception(e)
  131. return BadRequestResponse(request.id, user_message='Can not create connection. Contact to the support.')
  132.  
  133. return SuccessResponse(request.id, data={
  134. 'qwc_url': company.qwc_file.url if company.qwc_file else '',
  135. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement