Subversion Repositories SmartDukaan

Rev

Rev 7008 | Rev 7233 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7008 amit.gupta 1
#!/usr/bin/python
2
from functools import partial
3
from shop2020.clients.PromotionClient import PromotionClient
4
from shop2020.utils.EmailAttachmentSender import mail, mail_html
5
import datetime
6
import os
7
import sys
8
import threading
9
 
10
 
11
def notify():
12
    client = PromotionClient().get_client()
13
    dayExpiry = []
14
    expired = []
15
    promotionIds = [16,18,26,27,33]
16
    for promotionId in promotionIds:
17
        coupons = client.getAllCouponsByPromotionId(promotionId)
18
        for coupon in coupons:
19
            endTime = None
20
            args = eval(coupon.arguments)
21
            if args:
22
                if 'end_on' in args:
23
                    endTime = datetime.datetime.strptime(args['end_on'], '%Y-%m-%d')
24
                elif 'endOn' in args:
25
                    endTime =  datetime.datetime.fromtimestamp(args['endOn']/1000)
26
                else:
27
                    continue
28
                curTime = datetime.datetime.now()
29
                diff = endTime - curTime 
30
                hoursDiff = diff.seconds/3600
31
                daysDiff = diff.days
32
                if daysDiff == 0:
33
                    dayExpiry.append("<b>" + coupon.couponCode + "(" + coupon.promotion.name + ")</b> will expire in " + str(hoursDiff) + " hours")
34
                elif daysDiff == 1:
35
                    dayExpiry.append("<b>" + coupon.couponCode +  "(" + coupon.promotion.name + ")</b> will expire in " + str(24 + hoursDiff) + " hours")
36
                elif daysDiff == -1:
37
                    expired.append("<b>" + coupon.couponCode +  "(" + coupon.promotion.name + ")</b> has been expired " + str(24 - hoursDiff) + " hours ago")
38
 
39
    if (dayExpiry or expired):
40
        if dayExpiry:
41
            message = "<br>".join(dayExpiry) + "<br><br>"
42
 
43
        if expired:
44
            message += "<br>".join(expired)
45
 
46
        message += "<br><br>Please take appropriate action if required" 
47
 
48
        __send_mail(message)
49
 
50
def main():
51
    notify()
52
 
53
def __send_mail(message):
54
    try:
7009 amit.gupta 55
        thread = threading.Thread(target=partial(mail, "cnc.center@shop2020.in", "5h0p2o2o", ["pramit.singh@shop2020.in", "khushal.bhatia@shop2020.in", "chandan.kumar@shop2020.in",  "chaitnaya.vats@shop2020.in", "amit.gupta@shop2020.in", "rajneesh.arora@shop2020.in", "rajveer.singh@shop2020.in"], 'Coupons Expiry Alert', message))
7008 amit.gupta 56
        thread.start()
57
    except Exception as ex:
58
        print ex    
59
 
60
if __name__ == '__main__':
61
    main()