Skip to content

Conversation

@bogdanpetrea
Copy link
Contributor

@bogdanpetrea bogdanpetrea commented May 22, 2017

Depends on #532.


# Transition related document too, if needed
if document.related_document and document.state != document.related_document.state:
state_transition_map = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a useful business logic block that can be extracted in BillingDocumentBase model

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be...

transition_name = state_transition_map[document.state]

bound_transition_method = getattr(document.related_document, transition_name)
bound_transition_method()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will also save the document.related_document instance?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can think to move this logic into a reconcile loop. (not necessarily now)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Yes, it will also save the related document, because the save method gets automatically called after a successful transition.
  2. I'd rather have this happen synchronously. Having a draft proforma and an issued invoice, even for a short period of time, doesn't sound good.


document = instance

if hasattr(document, '.recently_transitioned'):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Negating this will make the entire logic a little easier to read.

create_transaction_for_document(document)

# Generate a PDF
document.set_pdf_for_generation()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍
I would name it mark_for_generation (in this way you will leave some options to have more renders: pdf, html, xml. Maybe, in a near feature, each invoice will have a well know format, xml based, making them easier to import/export (I think that the Czech Republic has this system implemented)).

return self.pdf_file.url if self.pdf_file else None

def generate(self, template, context, upload=True):
pdf_file_object = HttpResponse(content_type='application/pdf')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to return an HttpResponse from model.
I don't think that models should be coupled with any type of http response.

Copy link
Contributor Author

@bogdanpetrea bogdanpetrea Jul 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That HttpResponse will be used as a file_object for the generate_pdf_template_object method.

'(invoice or proforma).'
)

lock = redis.lock(lock_key, timeout=5) # 5 seconds should be more than enough to save
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Magic number detected

setattr(self, 'previous_instance', previous_instance)

def _clean_and_save():
if not getattr(self, '.cleaned', False):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like a method.
Do you use the closure, or you just want to reuse the code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just want to reuse the code, but I don't want to expose it, because calling that method would evict the locking.

lock = redis.lock(lock_key, timeout=5) # 5 seconds should be more than enough to save

if not lock.acquire(blocking_timeout=5):
raise ValidationError("Couldn't create a transaction for the given document.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like a ValidationError.
Also, maybe some details regarding why the transaction can't be created will help debugging.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well it's a validation step that doesn't pass. How about: raise ValidationError("A transaction for the same billing document is currently being created.")?


lock = redis.lock(lock_key, timeout=5) # 5 seconds should be more than enough to save

if not lock.acquire(blocking_timeout=5):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Magic number detected.

@bogdanpetrea bogdanpetrea merged commit 854cd0a into master Jul 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants