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

        status_int = 200

        if 'room' in route:

            rooms_instance = ChatRoom()
            room = rooms_instance.get_chat_room(uid=route[len(route) - 1],
                                                chat_id=route[len(route) - 2])
            if room != '':
                response_data = room.to_dict()
            else:
                status_int = 401
                response_data = {'message': 'chat room not found'}

        elif 'rooms' in route:

            rooms_instance = ChatRoom()
            rooms = rooms_instance.fetch_chat_rooms()

            response_data = []
            for room in rooms:
                response_data.append(room.to_dict())

        elif 'user' in route:
            uid = route[len(route) - 1]
            users_instance = ChatUsers()
            response = users_instance.get_user(uid=uid)

            if response != '':
                response_data = response.to_dict()
            else:
                status_int = 404
                response_data = {'message': 'user not found'}

        elif 'users' in route:
            users_instance = ChatUsers()
            response_data = users_instance.get_chat_users(
                chat_id=route[len(route) - 1])

        elif 'messages' in route:
            chat_id = route[len(route) - 1]

            messages_instance = ChatMessages()
            response_data = messages_instance.get_chat_message(chat_id=chat_id)

        else:
            status_int = 401
            response_data = {'message': 'could not understand 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)
예제 #2
0
    def put(self):
        url_route = self.request.uri
        url_routes = url_route.split("/")
        route = url_routes

        status_int = 200
        if 'room' in route:
            room_instance = ChatRoom()
            response = room_instance.update_chat_room(
                room_detail=json.loads(self.request.body),
                uid=route[len(route) - 1],
                chat_id=route[len(route) - 1])
            if response != '':
                response_data = response.to_dict()
            else:
                status_int = 401
                response_data = {'message': 'cannot update chat room'}

        else:
            status_int = 401
            response_data = {'message': 'could not understand 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)
예제 #3
0
    def post(self):
        url_route = self.request.uri
        url_routes = url_route.split("/")
        route = url_routes

        status_int = 200

        if 'room' in route:
            room_detail = json.loads(self.request.body)

            room_instance = ChatRoom()
            response = room_instance.add_chat_room(room_detail=room_detail,
                                                   uid=route[len(route) - 1])
            if response != '':
                response_data = response.to_dict()
            else:
                status_int = 401
                response_data = {'message': 'chat room already present'}

        elif 'user' in route:
            user_details = json.loads(self.request.body)

            user_instance = ChatUsers()
            response = user_instance.add_user(user_details=user_details)
            response_data = response.to_dict()

        elif 'message' in route:
            message_detail = json.loads(self.request.body)
            logging.info('Message : {}'.format(message_detail))
            messages_instance = ChatMessages()
            response = messages_instance.add_message(
                message_detail=message_detail, chat_id=route[len(route) - 1])

            if response != '':
                response_data = response.to_dict()
            else:
                status_int = 401
                response_data = {'message': 'error adding new chat message'}

        else:
            status_int = 401
            response_data = {'message': 'could not understand 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)
예제 #4
0
    def delete(self):
        url_route = self.request.uri
        url_routes = url_route.split("/")
        route = url_routes

        status_int = 200
        if 'room' in route:
            room_instance = ChatRoom()
            response = room_instance.delete_chat_room()
            if response != '':
                response_data = response.to_dict()
            else:
                status_int = 401
                response_data = {'message': 'cannot delete chat room'}
        else:
            status_int = 401
            response_data = {'message': 'could not understand 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)
예제 #5
0
    def get(self):
        url_route = self.request.uri
        url_routes = url_route.split("/")
        
        status_int = 200
        if 'media' in url_routes:

            uid = url_routes[len(url_routes) - 1]

            if 'albums' in url_routes:
                album_instance = Album()
                album_list = album_instance.getAllAlbums(uid=uid)
                response_data = []

                for album in album_list:
                    response_data.append(album.to_dict())

            elif 'music' in url_routes:
                music_instance = Music()
                music_list = music_instance.getAllMusicFiles(uid=uid)

                response_data = []

                for music in music_list:
                    response_data.append(music.to_dict())

            else:
                status_int = 400
                response_data = {
                    'message': 'error accessing media route'}


        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 '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'
                }


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

        self.response.headers['Content-Type'] = "application/json"
        self.response.status_int = status_int
        json_data = json.dumps(response_data)
        self.response.write(json_data)
예제 #6
0
 def __init__(self, sock):
     self.running = True
     # 下面的socks就是经过绑定协议/IP地址/端口号的socks
     self.sock = sock
     self.room = ChatRoom()
     super(ChatServer, self).__init__()
예제 #7
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)