Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- If you want to be more extandable with Django Architecture cover your models with a layer.
- Instead of using your model use helper classes everywhere.
- I named the class that covers a model ModelController.
- I have covered only create and update methods now.
- """
- # company.py
- class Company(BaseModel):
- name = models.CharField(max_length=200, unique=True, db_index=True)
- guid = models.CharField(max_length=64, unique=True, db_index=True)
- username = models.CharField('WC Username', max_length=32, db_index=True,
- help_text='The username that Web Connector will use.')
- password = models.CharField('WC Password', max_length=100,
- help_text='The password that Web Connector will use.')
- connected = models.BooleanField(default=False)
- connected_at = models.DateTimeField(null=True, blank=True)
- invoice_item = models.CharField('Invoice Item', max_length=31, blank=True,
- help_text='QuickBooks item used on the invoice.')
- qwc_file = models.FileField('QWC File', blank=True, upload_to=get_qwc_filename,
- help_text='Web Connector configuration file.')
- qwc_file_id = models.CharField('QWC FileID', max_length=38, blank=True)
- class Meta:
- verbose_name = 'Company'
- verbose_name_plural = 'Companies'
- def __str__(self):
- return self.name
- def save(self, *args, **kwargs):
- if not self.pk:
- if not self.guid:
- self.guid = uuid.uuid4().hex,
- super(Company, self).save(*args, **kwargs)
- def check_password(self, plain_password):
- return plain_password == self.password
- # company_controller.py:
- class CompanyController:
- """ A helper class for Company model """
- def __init__(self, company):
- self.company = company
- @classmethod
- def create(cls, **kwargs):
- carrier = kwargs.get('carrier')
- return Company.objects.create(
- carrier=carrier,
- name=carrier.name,
- guid=kwargs.get('guid', uuid.uuid4().hex),
- username=kwargs.get('username', uuid.uuid4().hex),
- password=kwargs.get('password', str(randint(2000, 8000))),
- invoice_item=kwargs.get('invoice_item', 'Service'),
- connected=kwargs.get('connected', False),
- )
- def update(self, **kwargs):
- update_fields = []
- if 'connected' in kwargs:
- update_fields.append('connected')
- self.company.connected = kwargs.get('connected')
- if self.company.connected:
- self.company.connected_at = timezone.now()
- update_fields.append('connected_at')
- if 'invoice_item' in kwargs:
- update_fields.append('invoice_item')
- self.company.invoice_item = kwargs.get('invoice_item')
- if 'qwc_file_id' in kwargs:
- update_fields.append('qwc_file_id')
- self.company.qwc_file_id = kwargs.get('qwc_file_id')
- if 'qwc_file' in kwargs:
- update_fields.append('qwc_file')
- self.company.qwc_file.save(
- 'mysuperdispatch-{}.qwc'.format(randint(10, 99)),
- ContentFile(kwargs.get('qwc_file'))
- )
- if update_fields:
- self.company.save(update_fields=update_fields)
- return self.company
- def create_qwc_file(self):
- """
- Create QuickBooks Web Connector configuration file.
- OwnerID should be per web service - constant across multiple QWC files.
- Needed if app needs to store private data in the company file, e.g.
- for checking whether we had communicated with this company file before.
- FileID is stored with the company file along OwnerID.
- """
- qwc_file_template = 'quickbooks_desktop/mysuperdispatch.qwc'
- qwc_file_id = '{' + str(uuid.uuid4()).upper() + '}'
- context = {
- 'company': self.company,
- 'qwc_file_id': qwc_file_id,
- 'owner_id': settings.QUICKBOOKS_OWNER_ID,
- }
- file_content = render_to_string(qwc_file_template, context).encode('utf-8')
- self.update(qwc_file_id=qwc_file_id, qwc_file=file_content)
- # views.py
- class CreateConnectionView(DispatcherTokenAuthMixin, APIView):
- def post(self, request, *args, **kwargs):
- try:
- if self.company and not self.company.connected:
- company = CompanyController(company=company).update(connected=True)
- if not company:
- company = CompanyController.create(name=self.company_name)
- CompanyController(company=company).create_qwc_file()
- except Exception as e:
- logger.exception(e)
- return BadRequestResponse(request.id, user_message='Can not create connection. Contact to the support.')
- return SuccessResponse(request.id, data={
- 'qwc_url': company.qwc_file.url if company.qwc_file else '',
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement