예제 #1
0
    def get(self):
        user_id = get_jwt_identity()
        user = UserModel.find_by_id(user_id)

        if user:
            name = user.username
            user_channels = ChannelModel.find_channels_by_user(name)
            owner_channels = ChannelModel.find_channels_by_owner(name)

            channel_list = []

            for channel in owner_channels:
                if name in json.loads(channel.owners):
                    channel_list.append(channel.json())

            for channel in user_channels:
                if name in json.loads(channel.users) and (channel.json()
                                                          not in channel_list):
                    channel_list.append(channel.json())

            if channel_list:
                return {'channels': channel_list}, 200
            else:
                return {'message': 'User hasn\'t got any channel'}, 200
        return {'message': 'Channels not found'}, 404
예제 #2
0
    def post(self, channel_name):

        channel_id = ChannelModel.find_id_by_name(channel_name)

        if channel_id:
            random_data = randomData()
            message_id = hashData(str(random_data))

            if MessageModel.find_msg_by_channel_id_msg_id(
                    channel_id, message_id):
                return {'message': 'Bad message id'}, 400

            data = Message.parser.parse_args()
            date_now = datetime.utcnow()

            user_id = get_jwt_identity()
            user = UserModel.find_by_id(user_id)
            print(user.username)
            message = MessageModel(message_id, channel_id, data['content'],
                                   date_now, user.username,
                                   UserModel.get_avatar(user.username))

            message.save_to_db()
            return message.json(), 201
        else:

            return {'message': 'Channel not exist.'}, 404
예제 #3
0
    def channel_list_current(self, request):
        logging.info('channel_list_current()')
        query = ChannelModel.get_channels()
        channels = [channel.to_message(current=True) for channel in query.fetch()]

        channelList = ChannelListResponse()
        channelList.channels = channels

        return channelList
예제 #4
0
 def get(self, channel_name):
     user_id = get_jwt_identity()
     user = UserModel.find_by_id(user_id)
     channel = ChannelModel.find_by_name(channel_name)
     if channel:
         if user.username in channel.owners:
             return {'is_owner': True}, 200
         return {'is_owner': False}, 403
     return {'message': 'Channel not found'}, 404
예제 #5
0
 def delete(self, name):
     channel = ChannelModel.find_by_name(name)
     if channel is not None:
         messages = MessageModel.find_msgs_by_channel_id(channel.channel_id)
         if messages is not None:
             for message in messages:
                 message.delete_from_db()
         channel.delete_from_db()
         return {'message': 'Channel deleted'}, 201
     return {'message': "An channel not found"}, 404
예제 #6
0
    def post(self, name):
        check = ChannelModel.find_by_name(name)
        if check:
            return {
                'message': "An channel with that name already exists."
            }, 400

        data = Channel.parser.parse_args()

        random_data = randomData()

        timestamp = datetime.utcnow().timestamp()

        channel_id = hashData(random_data, str(timestamp) + str(name))

        user_id = get_jwt_identity()
        user = UserModel.find_by_id(user_id)

        owner = user.username

        owners = set()
        owners.add(owner)

        for item in data["owners"]:
            owners.add(item)

        print(owners)

        users = set()
        users.add(owner)
        for item in data["users"]:
            users.add(item)
        print(users)

        try:
            owners.remove('')
        except Exception as err:
            pass

        try:
            users.remove('')
        except Exception as err:
            pass

        # None is used because the user will not set the channel id.
        try:
            channel = ChannelModel(channel_id, str(name),
                                   json.dumps(list(owners)),
                                   json.dumps(list(users)))
            channel.save_to_db()
            channel.save_to_db()

        except Exception as err:
            return {
                'message': 'An error occured inserting the item\n',
                'error': err
            }, 500

        return channel.json(), 201
예제 #7
0
    def delete(self, channel_name, message_id):
        #claims = get_jwt_claims()
        #if not claims['is_admin']:
        #    return {'message': 'Admin privilege required.'}, 401

        channel_id = ChannelModel.find_id_by_name(channel_name)
        if channel_id:
            message = MessageModel.find_msg_by_channel_id_msg_id(
                channel_id, message_id)

            if message:
                message.delete_from_db()
                return {'message': 'Message was deleted.'}, 201
            else:
                return {'message': 'Message not found.'}, 404
예제 #8
0
    def get(self, channel_name):
        user_id = get_jwt_identity()

        channel_id = ChannelModel.find_id_by_name(channel_name)
        if channel_id:
            messages_list = [
                message.json() for message in MessageModel.query.filter_by(
                    channel_id=channel_id).all()
            ]

            if user_id:
                return {'messages': messages_list}, 200
            else:
                return {'messages': 'Not available for non-logged users.'}, 403
        else:
            return {'message': 'Channel with that name not found.'}, 404
예제 #9
0
    def channel_details(self, request):
        logging.info('channel_details()')

        cache_key = 'channels:details:%s' % request.id
        channel = memcache.get(cache_key)
        if channel is None:
            channel = ChannelModel.get_details(request)

            # Cache results
            memcache.add(cache_key, channel)

            logging.info('Channel details cached')
        else:
            logging.info('Channel details read from cache')


        return channel.to_message()
예제 #10
0
    def channel_list(self, request):
        logging.info('channel_list()')

        cache_key = 'channels:list'
        channels = memcache.get(cache_key)
        if channels is None:
            query = ChannelModel.get_channels()
            channels = [entity.to_message() for entity in query.fetch()]

            # Cache results
            memcache.add(cache_key, channels)

            logging.info('Channel list cached')
        else:
            logging.info('Channel list read from cache')

        channelList = ChannelListResponse()
        channelList.channels = channels

        return channelList
예제 #11
0
 def find_by_channel(cls, channel_name):
     channel_id = ChannelModel.find_id_by_name(channel_name)
     return cls.query.filter_by(channel_id=channel_id).all()
예제 #12
0
 def channel_add_post(self, request):
     channel = ChannelModel.add_video(request)
     return channel.to_message()
예제 #13
0
 def channel_insert(self, request):
     entity = ChannelModel.put_from_message(request)
     return entity.to_message()
예제 #14
0
 def get(self, name):
     channel = ChannelModel.find_by_name(name)
     if channel:
         return channel.json()
     return {'message': 'Channel not found'}, 404
예제 #15
0
    def put(self, name):
        channel = ChannelModel.find_by_name(name)
        data = Channel.parser.parse_args()
        user_id = get_jwt_identity()
        user = UserModel.find_by_id(user_id)

        if channel is None:
            timestamp = datetime.utcnow().timestamp()
            random_data = randomData()

            channel_id = hashData(random_data, str(timestamp) + str(name))

            owner = user.username
            owners = set()
            owners.add(owner)

            if data["owners"] and hasattr(data["owners"], '__iter__'):
                for item in data["owners"]:
                    owners.add(item)

            users = set()
            users.add(owner)

            if data["users"] and hasattr(data["users"], '__iter__'):
                for item in data["users"]:
                    users.add(item)

            print(owners, users)

            try:
                owners.remove('')
                owners.remove('null')
            except Exception as err:
                pass

            try:
                users.remove('')
                users.remove('null')
            except Exception as err:
                pass

            channel = ChannelModel(channel_id, name, json.dumps(list(owners)),
                                   json.dumps(list(users)))

        elif (not ChannelModel.find_by_name(data['name'])
              or data['name'] == name):

            if user.username not in channel.owners:
                return {'message': 'You arn\'t owner channel: ' + channel.name}
            owner = user.username
            owners = set()
            owners.add(owner)
            if data["owners"] and hasattr(data["owners"], '__iter__'):
                for item in data["owners"]:
                    owners.add(item)

            users = set()
            users.add(owner)
            if data["users"] and hasattr(data["users"], '__iter__'):
                for item in data["users"]:
                    users.add(item)

            try:
                owners.remove('')
            except Exception as err:
                pass

            try:
                users.remove('')
            except Exception as err:
                pass

            channel.owners = json.dumps(list(owners))
            channel.users = json.dumps(list(users))

        else:
            return {
                'message':
                "An channel with name '{}' already exists".format(data['name'])
            }, 404
        channel.save_to_db()
        return channel.json(), 201