Last active
December 18, 2015 15:38
-
-
Save venkatesh22/5805421 to your computer and use it in GitHub Desktop.
Example for Python thread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
models.py | |
class PurchaseOrder(models.Model): | |
total_value = models.DecimalField(max_digits=12, decimal_places=2, null=True, blank=True) | |
def purchase_order_post_save_handler(sender, instance, **kwargs): | |
project_po_value_update_thread = accounting.threads.ProjectPoValueUpdateThread(instance) | |
project_po_value_update_thread.start() | |
post_save.connect(purchase_order_post_save_handler, sender=PurchaseOrder) | |
threads.py | |
class ProjectPoValueUpdateThread(threading.Thread): | |
""" | |
thread to update the po.project with the latest po values | |
called via the save method of the purchase order | |
""" | |
def __init__(self, purchase_order): | |
self.purchase_order = purchase_order | |
threading.Thread.__init__(self) | |
def run(self): | |
"starts the thread to update project po values" | |
try: | |
project = self.purchase_order.project | |
#update active po budget | |
active_po_sum = PurchaseOrder.objects.filter(project=project, is_active=True).aggregate(total_value_sum=Sum('total_value')) | |
active_total_value_sum = active_po_sum['total_value_sum'] | |
project.total_value = active_total_value_sum | |
project.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment