Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

#!/usr/bin/python
'''

@author: Rajveer
'''
import sys
import imaplib
import email
from shop2020.model.v1.order.impl import DataAccessors, DataService
from shop2020.model.v1.order.impl.DataService import Order
from shop2020.thriftpy.model.v1.order.ttypes import OrderStatus

if __name__ == '__main__' and __package__ is None:
    import os
    sys.path.insert(0, os.getcwd())

from_user = 'cnc.center@shop2020.in'
from_pwd = '5h0p2o2o'
to = ['cnc.center@shop2020.in', "amit.sirohi@shop2020.in", "sandeep.sachdeva@shop2020.in", "parmod.kumar@shop2020.in"]

def process_store_price_approval(orderId):
    subject = str(orderId)
    user = 'cnc.center@shop2020.in'
    pwd = '5h0p2o2o'
    
    # connecting to the gmail imap server
    m = imaplib.IMAP4_SSL("imap.gmail.com")
    m.login(user,pwd)
    m.select("Inbox", True)  # use m.list() to get all the mailboxes
    
    # you could filter using the IMAP rules here (check http://www.example-code.com/csharp/imap-search-critera.asp)
    resp, items = m.search(None, 'SUBJECT', subject, 'UNKEYWORD', 'processed')
    items = items[0].split() # getting the mail ids
    
    for emailid in reversed(items):
        # fetching the mail, "`(RFC822)`" means "get the whole stuff", but you can ask for headers only, etc
        resp, data = m.fetch(emailid, "(RFC822)")
        email_body = data[0][1] # getting the mail content
        mail = email.message_from_string(email_body) # parsing the mail content to get a mail object
        for part in mail.walk():
            if part.get_content_type() == "multipart/alternative":
                continue
            btext = part.get_payload(decode=1)
            btext = btext.strip()
            if btext.lower().startswith('approved'):
                DataAccessors.verify_order(orderId)
            if btext.lower().startswith('approved'):
                DataAccessors.refund_order(orderId, "Automatically", "Pricing not approved by approving authority")

def main():
    DataService.initialize()
    #DataService.initialize('transaction', '192.168.190.114')
    orders = Order.query.filter(Order.status == OrderStatus.COD_VERIFICATION_PENDING).filter(Order.source == 2).all()
    if orders:
        for order in orders:
            process_store_price_approval(order.id)

if __name__ == '__main__':
    main()