Beispiel #1
0
    def get(self):
        url_route = self.request.uri
        route = url_route.split("/")

        logging.info('ROUTE INFORMATION')
        logging.info(route)
        
        status_int = 200

        if 'categories' in route:

            category_requests = Categories.query()
            categories_list = category_requests.fetch()

            response_data = []

            for category in categories_list:
                response_data.append(category.to_dict())

        elif 'products' in route:
            
            id = str(route[len(route) - 1])
            router = str(route[len(route) - 2])
            request = str(route[len(route) - 3])

            logging.info('PRODUCT ID')
            logging.info(id)

            if id == 'products':

                products_requests = Products.query()
                products_list = products_requests.fetch()
                
                response_data = []
                for product in products_list:
                    response_data.append(product.to_dict())

            elif router == 'user':

                products_request = Products.query(Products.uid == id)
                products_list = products_request.fetch()

                response_data = []
                for product in products_list:
                    response_data.append(product.to_dict())

            # requests here equals product requests

            elif router == 'search':

                search_text = id
                products_request = Products.query()
                products_list = products_request.fetch()
                
                response_data = []

                for product in products_list:
                    if (product.product_name != None) and (search_text.lower() in product.product_name.lower()):
                        response_data.append(product.to_dict())
                    elif (product.description != None) and (search_text.lower() in product.description.lower()):
                        response_data.append(product.to_dict())
                    elif (product.price != None) and search_text.lower() in product.price.lower():
                        response_data.append(product.to_dict())
                    else:
                        pass                                                        

            elif request == 'requests':
                # /requests/${uid}/${id}
                uid = router

                # id in here is the same as product_id in productRequests 
                this_query = ProductRequests.query(ProductRequests.product_id == id)
                this_product_requests_list = this_query.fetch()

                                
                response_data = []

                for product_request in this_product_requests_list:
                    # this insures that it wont be possible to get product requests if you are not the owner of the product
                    if product_request.uid == uid:
                        response_data.append(product_request.to_dict())
                    else:
                        pass

            else :
                products_requests = Products.query(Products.id == id)
                products_list = products_requests.fetch()

                if len(products_list) > 0:
                    product = products_list[0]
                    response_data = product.to_dict()
                else:
                    status_int = 403
                    response_data = {'message':'product not found'}

        elif 'services' in route:
            id = str(route[len(route) - 1])
            router = str(route[len(route) - 2])

            # fetch list of services
            if id == 'services':
                services_requests = Services.query()
                services_list = services_requests.fetch()

                response_data = []
                for service in services_list:
                    response_data.append(service.to_dict())

            elif router == 'user':
                
                services_request = Services.query(Services.uid == id)
                services_list = services_request.fetch()

                response_data = []
                for service in services_list:
                    response_data.append(service.to_dict())

            elif router == 'search':
                search_text = id
                services_request = Services.query()
                services_list = services_request.fetch()

                response_data = []

                for service in services_list:
                    if (service.service_name != None) and (search_text.lower() == service.service_name.lower()):
                        response_data.append(service.to_dict())
                    elif (service.description != None) and (search_text.lower() == service.description.lower()):
                        response_data.append(service.to_dict())
                    elif (service.price != None) and (search_text.lower() == service.price.lower()):
                        response_data.append(service.to_dict())
                    else:
                        pass

                    
            else:
                # fetch a single service
                services_requests = Services.query(Services.id == id)
                services_list = services_requests.fetch()

                if len(services_list) > 0:
                    service = services_list[0]

                    response_data = service.to_dict()
                else:
                    status_int = 403
                    response_data = {'message':'service not found'}

        elif 'physical-address' in route:

            uid = route[len(route) - 1]

            physical_request = PhysicalAddress.query(PhysicalAddress.uid == uid)
            physical_list = physical_request.fetch()

            if (len(physical_list) > 0):
                physical_address = physical_list[0]
                response_data = physical_address.to_dict()
            else:
                status_int = 403
                response_data ={'message':'physical address not found'}

        elif 'contact-details' in route:

            uid = route[len(route) - 1]

            contact_details_request = ContactDetails.query(ContactDetails.uid == uid)
            contact_details_list = contact_details_request.fetch()

            if (len(contact_details_list) > 0):
                contact_details = contact_details_list[0]
                response_data = contact_details.to_dict()
            else:
                status_int = 403
                response_data = {'message': 'contact details not found'}

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

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

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

            else:
                status_int = 403
                response_data = {
                    'status': False,
                    'cart_items': [],
                    'cart': {},
                    'error': {'message':'cart items not found'}
                }

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

            this_user = User()
            this_user = this_user.getUser(uid=uid)
            if this_user != '':
                response_data = this_user.to_dict()
            else:
                status_int = 400
                response_data = {'message': 'user not found in the system'}

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

            this_store = Store()
            this_store = this_store.getStore(uid)

            if this_store != '':
                response_data = this_store.to_dict()
            else:
                status_int = 403
                response_data = {'message': 'store not found'}

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

            this_transactions  = Transactions()
            transactions_list = this_transactions.fetchTransactions(uid)
            response_data = []
            for transaction in transactions_list:
                response_data.append(transaction.to_dict())

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

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

            if ('payments' in route) and user.is_admin:
                
                payments_requests = Transactions.query()
                payments_list = payments_requests.fetch()

                response_data = []
                for payment in payments_list:
                    response_data.append(payment.to_dict())

            elif ('contacts' in route) and user.is_admin:
                contacts_requests = Contact.query()
                contacts_list = contacts_requests.fetch()

                response_data = []
                for contact in contacts_list:
                    response_data.append(contact.to_dict())

            elif ('users' in route) and user.is_admin:

                users_requests = User.query()
                user_list = users_requests.fetch()

                response_data = []

                for user in user_list:
                    response_data.append(user.to_dict())

            elif 'banking' in route and user.is_admin:

                banking_requests = Banking.query(Banking.is_admin_account == True)
                banking_list = banking_requests.fetch()

                response_data = []

                for banking in banking_list:
                    response_data.append(banking.to_dict())


            else:
                status_int = 403
                response_data = {'message':'you are not authorized to access dashboard'}
                    
        elif 'sms' in route:
            uid = route[len(route) - 1]

            # TODO use cron jobs to synchronize
            # local bundles with bundles on sasms crud

            user = User()
            this_user = user.getUser(uid=uid)
            response_data = []
            if ('bundles' in route) and (this_user != ''):

                bundles = SMSBundles()
                results = bundles.fetchBundles()

                for bundle in results:
                    response_data.append(bundle.to_dict())

            elif ('contact-lists' in route) and (this_user != ''):

                contacts_list = ContactLists()
                results = contacts_list.getContactList(uid=uid)

                for contact_list in results:
                    response_data.append(contact_list.to_dict())
                    
            elif ('bylistname' in route) and (this_user != ''):

                list_name = route[len(route) - 2]

                this_contact = SMSContacts()
                contact_list = this_contact.getContactByListName(list_name=list_name)

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

            elif ('bycontactid' in route) and (this_user != ''):
                list_id = route[len(route) - 2]

                this_contact = SMSContacts()
                contact_list = this_contact.getContactsByContactID(id=list_id)

                for contact in contact_list:
                    response_data.append(contact.to_dict())
            elif ('byuid' in route) and (this_user != ''):
                this_contact = SMSContacts()
                contact_list = this_contact.getContactsByUserID(uid=uid)

                for contact in contact_list:
                    response_data.append(contact.to_dict())
                
            else:
                status = 303
                response_data = {'message':'request not understood'}

        elif 'chat-rooms' in route:
            uid = route[len(route) - 1]
            chat_id = route[len(route) - 2]
            user = User()
            this_user = user.getUser(uid=uid)
            response_data = {}
            if this_user != '':
                chat_room_instance = ChatRoom()
                chat_room = chat_room_instance.getChatRoom(chat_id=chat_id)

                chat_messages = []
                chat_users = []
                if chat_room != '':
                    messages_instance = ChatMessages()
                    messages = messages_instance.getChatMessages(chat_id=chat_id)

                    for message in messages:
                        chat_messages.append(message.to_dict())

                    chat_users_instance = ChatUsers()
                    users = chat_users_instance.getChatUsers(chat_id=chat_id)

                    for user in users:
                        chat_users.append(user.to_dict())
                
                response_data = {
                    'chat_id' : chat_id,
                    'created_by' : chat_room.created_by,
                    'messages' : chat_messages,
                    'users' : chat_users
                };

            else:
                status_int = 401
                response_data = {
                    'message': 'user not recognised'
                }



        elif 'news' in route:
            pass













        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)