Rev 7757 | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/pythonfrom functools import partialfrom shop2020.clients.PromotionClient import PromotionClientfrom shop2020.utils.EmailAttachmentSender import mail, mail_htmlimport datetimeimport osimport sysimport threadingdef notify():message = ""curTime = datetime.datetime.now()client = PromotionClient().get_client()dayExpiry = []expired = []allPromotions = client.getAllPromotions()for promotion in allPromotions:endOn = datetime.datetime.fromtimestamp(promotion.endOn/1000)diff1 = curTime-endOnif diff1.days >= 15:if client.removeAllCouponsByPromotionId(promotion.id) > 0:message += "<br>All coupons related to promotion id <b>" +`promotion.id` + "(" + promotion.name + ")</b> have been deleted. <br>This promotion was expired on <b>" + str(endOn) + "</b>.<br>"promotionIds = [16,18,26,27,33]for promotionId in promotionIds:coupons = client.getAllCouponsByPromotionId(promotionId)for coupon in coupons:endTime = Noneargs = eval(coupon.arguments)if args:if 'end_on' in args:endTime = datetime.datetime.strptime(args['end_on'], '%Y-%m-%d')elif 'endOn' in args:endTime = datetime.datetime.fromtimestamp(args['endOn']/1000)else:continuediff = endTime - curTimehoursDiff = diff.seconds/3600daysDiff = diff.daysif daysDiff == 0:dayExpiry.append("<b>" + coupon.couponCode + "(" + coupon.promotion.name + ")</b> will expire in " + str(hoursDiff) + " hours")elif daysDiff == 1:dayExpiry.append("<b>" + coupon.couponCode + "(" + coupon.promotion.name + ")</b> will expire in " + str(24 + hoursDiff) + " hours")elif daysDiff == -1:expired.append("<b>" + coupon.couponCode + "(" + coupon.promotion.name + ")</b> has been expired " + str(24 - hoursDiff) + " hours ago")if (dayExpiry or expired):if dayExpiry:message += "<br>".join(dayExpiry) + "<br><br>"if expired:message += "<br>".join(expired)message += "<br><br>Please take appropriate action if required"if message != '':print message__send_mail(message)def main():notify()def __send_mail(message):try:thread = threading.Thread(target=partial(mail_html, "cnc.center@shop2020.in", "5h0p2o2o", ["pramit.singh@shop2020.in", "khushal.bhatia@shop2020.in", "chandan.kumar@shop2020.in", "chaitnaya.vats@shop2020.in", "rajneesh.arora@shop2020.in", "amit.sirohi@shop2020.in","manoj.kumar@shop2020.in"], 'Coupons Expiry Alert', message))thread.start()except Exception as ex:print exif __name__ == '__main__':main()