def createRoom(request):
    values = json.loads(request.body.decode('utf-8'))
    token = request.headers['Authorization'].split("'")

    username = checkJwt(token[1])

    if username != 'error' or username != None:
        room = values['roomName']
        tag = values['roomTags']

        tagList = tag.split(',')
        tags = ', '.join('"{0}"'.format(t.strip()) for t in tagList)
        tags = "{" + tags + "}"

        roomid = generateRandomString()
        createtion_date = ' '.join(today.strftime("%B %d, %Y").split(','))
        member = "{" + username + "}"
        # insert values into database
        try:
            cursor.execute(
                f"insert into room values('{roomid}', '{username}', '{createtion_date}', '{member}', '{room}', '{tags}')"
            )
            data = {
                'status': '200',
                'roomid': f'{roomid}',
            }
            return JsonResponse(data)
        except Exception as e:
            print(e)
            return HttpResponseServerError("Server error")
    else:
        HttpResponseNotFound("User not found")
    async def receive(self, text_data):
        text_data_json = json.loads(text_data)

        message = text_data_json['message']
        roomid = text_data_json['roomid']
        times = text_data_json['times']

        # creation_time = now.strftime("%H:%M")
        # creation_date =' '.join(today.strftime("%B %d, %Y").split(','))
        # creation_time_date=creation_time+' '+creation_date

        os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"

        token = text_data_json['token'].split("'")
        username = checkJwt(token[1])
        if username != 'error':
            # insert chats into database
            chatid = generateRandomString()
            try:
                cursor.execute(
                    f"insert into chat values('{chatid}','{roomid}','{username}','{message}','{times}')"
                )
            except Exception as e:
                print(e)
                print("Insert exception")

        # Send message to room group
        await self.channel_layer.group_send(self.room_group_name, {
            'type': 'chat_message',
            'message': message
        })
Beispiel #3
0
def list(request):
    if request.method == "GET":
        token = request.headers['Authorization'].split("'")
        username = checkJwt(token[1])
        if username != 'error' or username != None:
            cursor.execute('select * from task_taskmodel')
            tasks = cursor.fetchall()
            print(tasks)
            return JsonResponse({"task": tasks})
        else:
            return HttpResponseBadRequest("Unauthorised")
    else:
        return HttpResponseBadRequest("Method not allowed")
def join_room(request):
    values = json.loads(request.body.decode('utf-8'))
    roomid = values['roomid']
    token = request.headers['Authorization'].split("'")
    username = checkJwt(token[1])
    if username != 'error' or username != None:
        try:
            # update aa set b = array_append(b, 5) where a = 1
            cursor.execute(
                f"update room set members=array_append(members,'{username}') where roomid = '{roomid}'"
            )
            return JsonResponse({'status': 201})
        except Exception as e:
            print(e)
            return HttpResponseServerError("Server error")
    else:
        HttpResponseBadRequest("Unauthorised")
def chats(request):
    values = json.loads(request.body.decode('utf-8'))
    roomid = values['roomid']
    token = request.headers['Authorization'].split("'")
    username = checkJwt(token[1])
    if username != 'error' or username != None:
        try:
            cursor.execute(
                f"select username, message, time from chat where roomid='{roomid}'"
            )
            chats = cursor.fetchall()
            return JsonResponse({"status": 200, "chats": chats})
        except Exception as e:
            print(e)
            print("Database error")
            return HttpResponseServerError("Server error")
    else:
        return HttpResponseBadRequest("Unauthorised")
def sendRooms(request):
    token = request.headers['Authorization'].split("'")
    username = checkJwt(token[1])
    if username != 'error' or username != None:
        try:
            cursor.execute(
                f"select roomid,roomname,tags from room where '{username}'=any(members)"
            )
            rooms = cursor.fetchall()
            data = {
                'rooms': rooms,
            }
            return JsonResponse(data)
        except Exception as e:
            print(e)
            return HttpResponseServerError("Server error")
    else:
        return HttpResponseBadRequest("Unauthorised")
def checkInRoom(request):
    values = json.loads(request.body.decode('utf-8'))
    roomid = values['roomid']

    token = request.headers['Authorization'].split("'")
    username = checkJwt(token[1])
    if username != 'error' or username != None:
        try:
            cursor.execute(
                f"select * from room where roomid='{roomid}' and '{username}'=any(members)"
            )
            isUser = cursor.fetchall()
            if len(isUser) > 0:
                data = {'status': 200}
                return JsonResponse(data)
            else:
                return HttpResponseBadRequest("Unauthorised")
        except Exception as e:
            print(e)
            return HttpResponseServerError("Server error")
    else:
        return HttpResponseBadRequest("Unauthorised")
def trending(request):
    token = request.headers['Authorization'].split("'")
    username = checkJwt(token[1])
    rooms = []
    if username != 'error' or username != None:
        try:
            # get top trending tags
            # cursor.execute('with tags as(select unnest(tags) as tagname from room) select count(tagname),tagname from tags group by tagname')
            cursor.execute(
                'with tags as(select unnest(tags) as tagname from room) select count(tagname),tagname from tags group by tagname order by count(tagname) desc limit 5'
            )
            tags = cursor.fetchall()

            # get trending rooms
            # cursor.execute('select count(chat.roomid), chat.roomid, room.roomname from chat, room where chat.roomid=room.roomid group by chat.roomid, room.roomname order by count(chat.roomid) desc')
            cursor.execute(
                'select count(roomid), roomid from chat group by roomid order by count(roomid) desc limit 5'
            )
            roomsRows = cursor.fetchall()
            for room in roomsRows:
                cursor.execute(
                    f"select roomname, roomid, tags from room where roomid='{room[1]}'"
                )
                row = cursor.fetchall()
                rooms.append(row[0])

            # get trending username
            cursor.execute(
                'select count(username), username from chat group by username order by count(username) desc limit 5'
            )
            users = cursor.fetchall()

            return JsonResponse({'tags': tags, 'rooms': rooms, 'users': users})
        except Exception as e:
            return HttpResponseServerError("Server error")
    else:
        return HttpResponseBadRequest("Unauthorised")
Beispiel #9
0
def create(request):
    if request.method == "POST":
        token = request.headers['Authorization'].split("'")
        username = checkJwt(token[1])
        try:
            cursor.execute(
                f"select * from user_usermodel where username='******' and usertype='admin'")
            isAdmin = cursor.fetchall()
            if len(isAdmin) > 0:
                value = json.loads(request.body.decode('utf-8'))
                des = value['value']['des']
                tasktype = value['value']['type']
                appid = value['value']['appid']
                taskid = value['value']['taskid']
                cursor.execute(
                    f"insert into task_taskmodel values('{appid}','{taskid}','{des}','{tasktype}','Un-assigned','Open')")
                return JsonResponse({"code": "200"})
            else:
                return HttpResponseNotAllowed("Unauthorised")
        except Exception as e:
            print(e)
            return HttpResponseServerError("Server error")
    else:
        return HttpResponseBadRequest("Method not allowed")