Example #1
0
    def post(self):
        url = self.request.uri
        route = url.split('/')
        status_int = 200

        if 'contact' in route:
            json_data = json.loads(self.request.body)
            logging.info(json_data)

            this_contact = Contact()
            contact_key = this_contact.new_contact(contact=json_data)

            if contact_key != None:
                response_data = {'message': 'Thank you ' + str(json_data['names']) + ' for sending us this message we will be responding to you through this email ' + str(json_data['email']) }
            else:
                response_data = {'message' : 'Unfortunately we cannot process any more contact messages from you'}

        elif 'categories' in route:
            json_data = json.loads(self.request.body)                            
            this_category = Categories();
            this_category = this_category.addCategory(category=json_data)

            if this_category == '':
                status_int = 403
                response_data={'message':'error category already exist'}
            else:
                response_data = this_category.to_dict()

        elif 'products' in route:
            json_data = json.loads(self.request.body)            
            this_products = Products()
            this_products = this_products.addProduct(product=json_data)
            logging.info(this_products)            
            if this_products != None:
                response_data = {'message': 'product successfully added'}
            else:
                response_data = {'message': 'duplicate product error'}

        elif 'services' in route:            
            json_data = json.loads(self.request.body)                    
            this_service = Services()
            this_service = this_service.addService(service=json_data)

            if this_service != '':    
                response_data = this_service.to_dict()
            else:
                status_int = 403
                response_data = { 'message':'duplicate service'}

        elif 'physical-address' in route:
            json_data = json.loads(self.request.body)                        
            physical_address = PhysicalAddress()
            physical_address = physical_address.addPhysicalAddress(physical=json_data)
            response_data = physical_address.to_dict()

        elif 'contact-details' in route:
            json_data = json.loads(self.request.body)
            contact_details = ContactDetails()
            contact_details = contact_details.addContactDetails(contact=json_data)
            response_data = contact_details.to_dict()

        elif 'cart' in route:            
            
            json_data = json.loads(self.request.body)

            cart_owner_id = json_data['uid']
            item =  json_data['item']
            logging.info(item)

            cart_request = Cart.query(Cart.uid == cart_owner_id)
            cart_list = cart_request.fetch()

            if len(cart_list) > 0 :
                cart = cart_list[0]
            else:
                cart = Cart()
                cart.uid = cart_owner_id
                cart.cart_id = cart.create_cart_id()
                cart.is_active = True
                today = datetime.now()
                today = today.strftime("%d-%b-%Y")
                cart.date_created = today

                cart.sub_total = str(0)
                cart.tax = str(0)
                cart.total = str(0)

            items = Items()
            try:
                product_name = item['product_name']
                items.item_type = 'products'
                items.quantity = str(json_data['quantity'])
            except:
                items.item_type = 'services'
                items.quantity = str(json_data['quantity'])

            items.item_id = items.create_item_id()

            items.id_service_product = item['id']
            items.cart_id = cart.cart_id
            items.price = item['price']

            items.sub_total = str(float(items.price) * int(items.quantity))

            cart.sub_total = str(float(cart.sub_total) + float(items.sub_total))
            cart.tax = str(0)
            cart.total = str(float(cart.sub_total) + float(cart.tax))


            cart.total_items = str(int(cart.total_items) + int(items.quantity))

            cart.put() 
            items.put() 

            # add product to product requests and then add service to service requests

            if items.item_type == 'products':
                this_request = ProductRequests()
                this_request = this_request.addProduct(product=item,uid=cart_owner_id,total=items.quantity)
            else:
                this_request = ServiceRequests() 
                this_request = this_request.addService(service=item,uid=cart_owner_id,total=items.quantity)
                

            items_request = Items.query(Items.cart_id == cart.cart_id)
            items_list = items_request.fetch()
            ser_items = []
            for item in items_list:
                ser_items.append(item.to_dict())
            cart = cart.to_dict()
            # let results = {status : true, cart_items : [], cart : {}, error: {} };
            response_data = {
                    'status' : 'True' , 
                    'cart_items': ser_items,
                    'cart' : cart,
                    'error' : {}
                    }

        elif 'user' in route:
            json_data = json.loads(self.request.body)
            logging.info('User Data')
            logging.info(json_data)

            this_user = User()
            this_user = this_user.addUser(json_user=json_data)
            if this_user != '':
                response_data = this_user.to_dict()
            else:
                status_int = 403
                response_data = {'message': 'user not found'}

        elif 'store' in route:
            json_data = json.loads(self.request.body)

            this_store = Store()
            this_store = this_store.addStore(store=json_data)
            if(this_store != ''):
                response_data = this_store.to_dict()
            else:
                status_int = 403
                response_data = {'message': 'store not stored'}

        elif 'transactions' in route:
            json_data = json.loads(self.request.body)

            this_transaction = Transactions()
            result = this_transaction.addTransaction(transaction=json_data)
            logging.info(this_transaction)

            response_data = this_transaction.to_dict()

        elif 'sms' in route:
            uid = route[len(route) - 1]

            user = User()
            this_user = user.getUser(uid)

            if ('send' in route) and (this_user != ''):
                sms_message = json.loads(self.request.body)
                sms = SMSMessage()

                # once the message is sent results will be returned
                # through a webhook
                results = sms.addSMSmessage(sms_message=sms_message)
                response_data = {}
                response_data = results.to_dict()

            elif ('contact-lists' in route) and (this_user != ''):
                json_contact_lists = json.loads(self.request.body)

                contacts_lists = ContactLists()
                results = contacts_lists.addList(contact_list=json_contact_lists)
                response_data = {}
                response_data = results.to_dict()

            elif ('smscontact' in route) and (this_user != ''):
                json_contact = json.loads(self.request.body)
                logging.info(json_contact)

                sms_contact = SMSContacts()
                results = sms_contact.addContact(contact=json_contact)

                contact_list = sms_contact.fetchContactsByListID(id=json_contact['list_id'])

                response_data = []

                for contact in contact_list:
                    response_data.append(contact.to_dict())

            else:
                status_int = 303
                response_data = {'message':'request not understood'}

        elif 'dashboard' in route:
            uid = route[len(route) - 1]

            admin_user_query = User.query(User.uid == uid)
            admin_user_list = admin_user_query.fetch()

            response_data = ''

            if len(admin_user_list) > 0:
                admin_user = admin_user_list[0]
            
                if ('contact' in route) and admin_user.is_admin :

                    if 'response' in route :
                        response_message = json.loads(self.request.body)

                        this_response = Response()
                        results = this_response.createResponse(response_data=response_message, admin_user=admin_user)
                        response_data = results.to_dict()
                    else:
                        status_int = 400
                        response_data = {'message': 'request could not be understood'}

                elif ('banking' in route) and admin_user.is_admin:

                    banking_details = json.loads(self.request.body)

                    this_banking = Banking()
                    results =  this_banking.addAdminBank(banking_details=banking_details)
                    if (results != ''):
                        response_data = results.to_dict()
                    else:
                        status_int = 401
                        response_data = {'message': 'error saving banking details'}

                else:
                    status_int = 400
                    response_data = {
                        'message': 'request could not be understood'}
            else:
                status_int = 400
                response_data = {
                    'message': 'request could not be understood'}

        else:
            status_int = 400
            response_data = {'message': 'the server cannot process this request'}


        self.response.headers['Content-Type'] = "application/json"
        self.response.status_int = status_int
        json_data = json.dumps(response_data)
        self.response.write(json_data)