Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.42 KB | None | 0 0
  1. def content_post_saved_callback(sender, **kwargs):
  2.     import threading
  3.  
  4.     if kwargs["created"]:
  5.         class Thrdng(threading.Thread):
  6.             def run (self):
  7.  
  8.                 from core.models import Quser, Notification, Ticket_status, Ticket_status_type
  9.                 import settings
  10.                 from core.utils import getLanguageCode, getCC
  11.                 from django.utils.translation import activate
  12.                 from django.utils.translation import ugettext as _
  13.                 from django.contrib.sites.models import Site
  14.                 from core.templatetags import JS
  15.  
  16.                 kwargs = self.locals["kwargs"]
  17.                 sender = self.locals["sender"]
  18.                
  19.                 try:
  20.                     #gestituisce la propagazione delle notifiche
  21.                     content = kwargs['instance']
  22.                    
  23.                     if(type(content).__name__ in ["Qpost", "SegnalazioneToolOdori", "Qalert_zanzare", "Qalert_mosche"]):
  24.                         punto = content.point
  25.                        
  26.                        
  27.                         if content.visibilityType.name == "Private":
  28.                             lista_utenti_notificati = []
  29.                         else:
  30.                             lista_utenti_notificati = list(Quser.objects \
  31.                                 .exclude(id = content.userCreatore.id) \
  32.                                 .exclude(user__is_active = False) \
  33.                                 .filter(point__distance_lte = (punto, 10000)))
  34.  
  35.                             if content.visibilityType.name == "Custom":
  36.                                 lista_utenti_notificati = lista_utenti_notificati.filter(user__username__in = content.get_visibility_definition()["users"])
  37.  
  38.                         lista_utenti_notificati.append(Quser.objects.get(user__username = "Qcumber"))
  39.                        
  40.                         if settings.URL == "https://www.q-cumber.org/":
  41.                             lista_utenti_notificati.append(Quser.objects.get(user__username = "cinzia"))
  42.                             lista_utenti_notificati.append(Quser.objects.get(user__username = "Giovanni86"))
  43.                             lista_utenti_notificati.append(Quser.objects.get(user__username = "stefyp"))                       
  44.                        
  45.                         default_protocol = getattr(settings, 'DEFAULT_HTTP_PROTOCOL', 'http')
  46.                         current_domain = Site.objects.get_current().domain
  47.                         site_url = default_protocol + '://' + current_domain
  48.  
  49.                         for Qu in lista_utenti_notificati:
  50.                             try:
  51.  
  52.                                 langCode = getLanguageCode(Qu)
  53.                                 activate(langCode)
  54.                                    
  55.                                
  56.                                 mess = None
  57.                                
  58.                                 if type(content).__name__ == "Qpost":
  59.                                    
  60.                                     if content.isTicket():
  61.  
  62.  
  63.                                         if content.getCurrentTicketStatus() == None:#it's "opened"
  64.                                             ts = Ticket_status()
  65.                                             ts.content = content
  66.                                             ts.status_type = Ticket_status_type.objects.get(codice = "opened")
  67.                                             ts.changedBy = content.userCreatore
  68.                                             ts.save()
  69.  
  70.  
  71.                                         mess = {
  72.                                             "subject": _("New red Qpost near you with notification to institution"),
  73.                                            
  74.                                             "body": _(u'''%(creator_user)s added a new red Qpost near you with the title "%(title)s".
  75. This Qpost was notified to %(ticket_recipient)s.
  76. To watch it, to comment on it and to follow the issue evolution visit this link: %(link1)s.
  77. The current status of the issue is: \"%(status)s\"
  78.  
  79. To confirm this report add a new red Qpost in the same point, follow this link: %(link2)s''') % {
  80.                                                 "title": content.titolo,
  81.                                                 "creator_user": content.userCreatore.getAlias(),
  82.                                                 "link1": site_url + "/permalink/Qpost/" + str(content.id) + "/",
  83.                                                 "link2": site_url + "/WorkStation/__oT__/add_Qpost/" + (JS.JS_encodeURI(JS.toJSON({ "type": "rosso", "lng": content.point[0], "lat": content.point[1] }))),
  84.                                                 "ticket_recipient": content.ticket_recipient.getAlias(),
  85.                                                 "status": _("Opened"),
  86.                                             },
  87.                                             "body_html": _(u'''<b style="font-size: 11pt;">%(creator_user)s</b> added a new red Qpost near you with the title "<b>%(title)s</b>".<br>
  88. This Qpost was notified to <b style="font-size: 11pt;">%(ticket_recipient)s</b>.<br>
  89. To watch it, to comment on it and to follow the issue evolution <a href="%(link1)s" style="color: #082E2C;">Click here</a>.<br>
  90. <br>
  91. The current status of the issue is: \"<b style="color: #83BFBC;">%(status)s</b>\"<br>
  92. <br>
  93. To confirm this report <a href="%(link2)s" style="color: #082E2C;">add a new red Qpost in the same point</a>''') % {
  94.                                                 "title": content.titolo,
  95.                                                 "creator_user": content.userCreatore.getAlias(),
  96.                                                 "link1": site_url + "/permalink/Qpost/" + str(content.id) + "/",
  97.                                                 "link2": site_url + "/WorkStation/__oT__/add_Qpost/" + (JS.JS_encodeURI(JS.toJSON({ "type": "rosso", "lng": content.point[0], "lat": content.point[1] }))),
  98.                                                 "ticket_recipient": content.ticket_recipient.getAlias(),
  99.                                                 "status": _("Opened"),
  100.                                             },
  101.                                         }
  102.                                        
  103.                                     else:
  104.                                        
  105.                                         if content.tipo.codice == "rosso":
  106.                                             mess = {
  107.                                                 "subject": _("New red Qpost near you"),
  108.                                                
  109.                                                 "body": _(u'''%(creator_user)s added a new Qpost near you with the title "%(title)s".
  110. To watch it and to comment on it follow this link: %(link1)s
  111.  
  112. To add a new Red Qpost in the same point follow this link: %(link2)s.''') % {
  113.                                                     "title": content.titolo,
  114.                                                     "creator_user": content.userCreatore.getAlias(),
  115.                                                     "link1": site_url + "/permalink/Qpost/" + str(content.id) + "/",
  116.                                                     "link2": site_url + "/WorkStation/__oT__/add_Qpost/" + (JS.JS_encodeURI(JS.toJSON({ "type": "rosso", "lng": content.point[0], "lat": content.point[1] }))),
  117.                                                 },
  118.                                                 "body_html": _(u'''<b style="font-size: 11pt;">%(creator_user)s</b> added a new Red Qpost near you with the title "<b>%(title)s</b>".<br>
  119. To watch it and to comment on it <a href="%(link1)s" style="color: #082E2C;">Click here</a><br>
  120. <br>
  121. To add a new Red Qpost in the same point <a href="%(link2)s" style="color: #082E2C;">Click here</a>.''') % {
  122.                                                     "title": content.titolo,
  123.                                                     "creator_user": content.userCreatore.getAlias(),
  124.                                                     "link1": site_url + "/permalink/Qpost/" + str(content.id) + "/",
  125.                                                     "link2": site_url + "/WorkStation/__oT__/add_Qpost/" + (JS.JS_encodeURI(JS.toJSON({ "type": "rosso", "lng": content.point[0], "lat": content.point[1] }))),
  126.                                                 },
  127.                                             }
  128.                                         elif content.tipo.codice == "verde":
  129.                                             mess = {
  130.                                                 "subject": _("New green Qpost near you"),
  131.                                                
  132.                                                 "body": _(u'''%(creator_user)s added a new Qpost near you with the title "%(title)s".
  133. To watch it and to comment on it follow this link: %(link1)s.
  134.  
  135. To add a new Green Qpost in the same point follow this link: %(link2)s.''') % {
  136.                                                     "title": content.titolo,
  137.                                                     "creator_user": content.userCreatore.getAlias(),
  138.                                                     "link1": site_url + "/permalink/Qpost/" + str(content.id) + "/",
  139.                                                     "link2": site_url + "/WorkStation/__oT__/add_Qpost/" + (JS.JS_encodeURI(JS.toJSON({ "type": "verde", "lng": content.point[0], "lat": content.point[1] }))),
  140.                                                 },
  141.                                                 "body_html": _(u'''<b style="font-size: 11pt;">%(creator_user)s</b> added a new Green Qpost near you with the title "<b>%(title)s</b>".<br>
  142. To watch it and to comment on it <a href="%(link1)s" style="color: #082E2C;">Click here</a>.<br>
  143. <br>
  144. To add a new Green Qpost in the same point <a href="%(link2)s" style="color: #082E2C;">Click here</a>.''') % {
  145.                                                     "title": content.titolo,
  146.                                                     "creator_user": content.userCreatore.getAlias(),
  147.                                                     "link1": site_url + "/permalink/Qpost/" + str(content.id) + "/",
  148.                                                     "link2": site_url + "/WorkStation/__oT__/add_Qpost/" + (JS.JS_encodeURI(JS.toJSON({ "type": "verde", "lng": content.point[0], "lat": content.point[1] }))),
  149.                                                 },
  150.                                             }
  151.                                            
  152.                                         elif content.tipo.codice == "grigio":
  153.                                             mess = {
  154.                                                 "subject": _("New grey Qpost near you"),
  155.                                                
  156.                                                 "body": _(u'''%(creator_user)s added a new Qpost near you with the title "%(title)s".
  157. To watch it and to comment on it follow this link: %(link1)s.
  158.  
  159. To add a new Grey Qpost in the same point follow this link: %(link2)s.''') % {
  160.                                                     "title": content.titolo,
  161.                                                     "creator_user": content.userCreatore.getAlias(),
  162.                                                     "link1": site_url + "/permalink/Qpost/" + str(content.id) + "/",
  163.                                                     "link2": site_url + "/WorkStation/__oT__/add_Qpost/" + (JS.JS_encodeURI(JS.toJSON({ "type": "grigio", "lng": content.point[0], "lat": content.point[1] }))),
  164.                                                 },
  165.                                                 "body_html": _(u'''<b style="font-size: 11pt;">%(creator_user)s</b> added a new Grey Qpost near you with the title "<b>%(title)s</b>".<br>
  166. To watch it and to comment on it <a href="%(link1)s" style="color: #082E2C;">Click here</a>.<br>
  167. <br>
  168. To add a new Grey Qpost in the same point <a href="%(link2)s" style="color: #082E2C;">Click here</a>.''') % {
  169.                                                     "title": content.titolo,
  170.                                                     "creator_user": content.userCreatore.getAlias(),
  171.                                                     "link1": site_url + "/permalink/Qpost/" + str(content.id) + "/",
  172.                                                     "link2": site_url + "/WorkStation/__oT__/add_Qpost/" + (JS.JS_encodeURI(JS.toJSON({ "type": "grigio", "lng": content.point[0], "lat": content.point[1] }))),
  173.                                                 },
  174.                                             }
  175.                                            
  176.                                         elif content.tipo.codice == "giallo":
  177.                                             mess = {
  178.                                                 "subject": _("New yellow Qpost near you"),
  179.                                                
  180.                                                 "body": _(u'''%(creator_user)s added a new Qpost near you with the title "%(title)s".
  181. To watch it and to comment on it follow this link: %(link1)s.
  182.  
  183. To add a new Yellow Qpost in the same point follow this link: %(link2)s.''') % {
  184.                                                     "title": content.titolo,
  185.                                                     "creator_user": content.userCreatore.getAlias(),
  186.                                                     "link1": site_url + "/permalink/Qpost/" + str(content.id) + "/",
  187.                                                     "link2": site_url + "/WorkStation/__oT__/add_Qpost/" + (JS.JS_encodeURI(JS.toJSON({ "type": "giallo", "lng": content.point[0], "lat": content.point[1] }))),
  188.                                                 },
  189.                                                 "body_html": _(u'''<b style="font-size: 11pt;">%(creator_user)s</b> added a new Yellow Qpost near you with the title "<b>%(title)s</b>".<br>
  190. To watch it and to comment on it <a href="%(link1)s" style="color: #082E2C;">Click here</a>.<br>
  191. <br>
  192. To add a new Yellow Qpost in the same point <a href="%(link2)s" style="color: #082E2C;">Click here</a>.''') % {
  193.                                                     "title": content.titolo,
  194.                                                     "creator_user": content.userCreatore.getAlias(),
  195.                                                     "link1": site_url + "/permalink/Qpost/" + str(content.id) + "/",
  196.                                                     "link2": site_url + "/WorkStation/__oT__/add_Qpost/" + (JS.JS_encodeURI(JS.toJSON({ "type": "giallo", "lng": content.point[0], "lat": content.point[1] }))),
  197.                                                 },
  198.                                             }
  199.                                    
  200.                                    
  201.                                 elif type(content).__name__ == "Qalert_zanzare":
  202.                                     mess = {
  203.                                         "subject": _("New Mosquitoes Qalert near you"),
  204.                                        
  205.                                         "body": _(u'''%(creator_user)s added a new Mosquitoes Qalert near you: amount "%(amount)s".
  206. To watch it follow this link: %(link1)s''') % {
  207.                                             "creator_user": content.userCreatore.getAlias(),
  208.                                             "amount": unicode(content.quantita),
  209.                                             "link1": site_url + "/WorkStation/__rgT__/WS/vediContenuto/Qalert_zanzare/" + str(content.id) + "/",
  210.                                         },
  211.                                             "body_html": _(u'''<b style="font-size: 11pt;">%(creator_user)s</b> added a new Mosquitoes Qalert near you: amount "<b>%(amount)s</b>".<br>
  212. To watch it <a href="%(link1)s" style="color: #082E2C;">Click here</a>.''') % {
  213.                                             "creator_user": content.userCreatore.getAlias(),
  214.                                             "amount": unicode(content.quantita),
  215.                                             "link1": site_url + "/WorkStation/__rgT__/WS/vediContenuto/Qalert_zanzare/" + str(content.id) + "/",
  216.                                         },
  217.                                     }
  218.                                    
  219.                                 elif type(content).__name__ == "Qalert_mosche":
  220.                                     mess = {
  221.                                         "subject": _("New Flies Qalert near you"),
  222.                                        
  223.                                         "body": _(u'''%(creator_user)s added a new Flies Qalert near you: amount "%(amount)s".<br>
  224. To watch it follow this link: %(link1)s''') % {
  225.                                             "creator_user": content.userCreatore.getAlias(),
  226.                                             "amount": unicode(content.quantita),
  227.                                             "link1": site_url + "/WorkStation/__rgT__/WS/vediContenuto/Qalert_mosche/" + str(content.id) + "/",
  228.                                         },
  229.                                             "body_html": _(u'''<b style="font-size: 11pt;">%(creator_user)s</b> added a new Flies Qalert near you: amount "<b>%(amount)s</b>".<br>
  230. To watch it <a href="%(link1)s" style="color: #082E2C;">Click here</a>.''') % {
  231.                                             "creator_user": content.userCreatore.getAlias(),
  232.                                             "amount": unicode(content.quantita),
  233.                                             "link1": site_url + "/WorkStation/__rgT__/WS/vediContenuto/Qalert_mosche/" + str(content.id) + "/",
  234.                                         },
  235.                                     }
  236.                                    
  237.                                 elif type(content).__name__ == "SegnalazioneToolOdori":
  238.                                     mess = {
  239.                                         "subject": _("New Odour Qalert near you"),
  240.                                        
  241.                                         "body": _(u'''%(creator_user)s added a new Odour Qalert near you: estimation "%(estimation)s".
  242. To watch it follow this link: %(link1)s
  243.  
  244. If you sense smell also in your area follow this link: %(link2)s''') % {
  245.                                             "creator_user": content.userCreatore.getAlias(),
  246.                                             "estimation": unicode(content),
  247.                                             "link1": site_url + "/permalink/QalertOdor/" + str(content.id) + "/",
  248.                                             "link2": site_url + "/WorkStation/__oT__/add_Qalert/",
  249.                                         },
  250.                                         "body_html": _(u'''<b style="font-size: 11pt;">%(creator_user)s</b> added a new Odour Qalert near you: estimation "<b>%(estimation)s</b>".<br>
  251. To watch it <a href="%(link1)s" style="color: #082E2C;">Click here</a>.<br>
  252. <br>
  253. If you sense smell also in your area <a href="%(link2)s" style="color: #082E2C;">Click here</a>.''') % {
  254.                                             "creator_user": content.userCreatore.getAlias(),
  255.                                             "estimation": unicode(content),
  256.                                             "link1": site_url + "/permalink/QalertOdor/" + str(content.id) + "/",
  257.                                             "link2": site_url + "/WorkStation/__oT__/add_Qalert/",
  258.                                         },
  259.                                     }
  260.  
  261.                            
  262.                                 if mess:
  263.                                     n = Notification.create(
  264.                                         type_name_code = "newContent",
  265.                                         QuserNotified = Qu,
  266.                                         metaOpts = {
  267.                                             "content": {
  268.                                                 "class": type(content).__name__,
  269.                                                 "cc": getCC(content),
  270.                                                 "id": content.id,
  271.                                             },
  272.                                             "message": mess,
  273.                                         }
  274.                                     )
  275.  
  276.                             except:
  277.                                 import traceback, time
  278.                                 logs = open(settings.BASE_PATH + "/var/log/email_sending.txt", "a")
  279.                                
  280.                                 logs.write('-'*60 + "\n")
  281.                                 logs.write(time.asctime() + "\n\n")
  282.                                 traceback.print_exc(limit=3, file=logs)
  283.                                 logs.write('-'*60 + "\n")
  284.                    
  285.                                 logs.close()
  286.  
  287.                 except:
  288.                     #raise
  289.                     pass
  290.  
  291.         thrdng = Thrdng()
  292.         thrdng.locals = locals()
  293.         thrdng.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement