SHARE
TWEET

Untitled

a guest Sep 12th, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3.  
  4. import jdatetime
  5. from django.db.models.signals import post_save, pre_save
  6. from django.dispatch import receiver
  7.  
  8. from ads.models import Ad
  9.  
  10.  
  11. @receiver(pre_save, sender=Ad)
  12. def ad_pre_save_receiver(sender, instance, **kwargs):
  13.     # save publish date if ad's status is changed to: approved
  14.     try:
  15.         obj = sender.objects.get(pk=instance.pk)
  16.     except sender.DoesNotExist:
  17.         pass  # Object is new, so field hasn't technically changed, but you may want to do something else here.
  18.     else:
  19.         if not obj.status == instance.status:  # Field has changed
  20.             if instance.status == 'approved':
  21.                 instance.publish_date = jdatetime.datetime.now()
  22.  
  23.  
  24. @receiver(post_save, sender=Ad)
  25. def ad_post_save_receiver(sender, instance, created, **kwargs):
  26.     if not instance.publish_date:
  27.         instance.publish_date = jdatetime.datetime.now()
  28.         instance.save()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top