Exemplo n.º 1
0
def register():
    if current_user.is_authenticated:  
        return Respone(False, {}, "Logout before sign in")  

    info = request.get_json()['info']
    username = info['username']
    email = info['email']
    password = info['password']
    role = info['role']

    user = User.objects.filter(username=username).first() 
    if not user: 
        hashed_password = bcrypt.generate_password_hash(password).decode('utf-8') 
        user = User(username=username, email=email,password=hashed_password, role=role)
        user.save()
        
        payload = {
            "username":user.username,
            "email":user.email,
            "image_file":user.image_file,
            "role":user.role
        } 
        message = "Create User Success"
        answer = Respone(True, payload, message)   
    else:
        answer = Respone(False, {}, "Email is exist")   
    return json.dumps(answer) 
Exemplo n.º 2
0
def update():
    info = request.get_json()['info']
    print(request.get_json())
    id_task = info['codeTask']
    name_task = info['nameTask']
    content = info['content']
    startTime = info['startTime']
    finishTime = info['finishTime']
    friend = info['dataFriendChecked']
    tags = info['listTag']
    list_image = info['listImage']
    images = info['images']
    list_path = []
    task = Task.objects(id=id_task).first()
    if task is not None:
        task.name = name_task
        task.content = content
        task.tags = tags
        list_user = []
        tokens = []
        for username in friend:
            user = User.objects(username=username).first()
            list_user.append(user)
            tokens.append(user.expo_token)

            token = user.expo_token
            message = "Task " + task.name + " has recently updated by " + current_user.username + " successfully"
            id_board = ""
            id_category = ""
            id_task = task.id
            name = task.name
            image = task.images[0]
            activity = "update"
            obj = "task"
            send_notification(token=token,
                              message=message,
                              user=user.username,
                              id_board=id_board,
                              id_category=id_category,
                              id_task=id_task,
                              activity=activity,
                              obj=obj,
                              name=name,
                              image=image)

        for im in images:
            list_path.append(convert_and_save(im, "category"))
        task.list_user_task = list_user
        task.save()

        result = []
        result.append(tokens)
        result.append(task.to_json())
        answer = Respone(True, result, "Update task success")
    else:
        answer = Respone(False, {}, "Update task failed")
    print(answer)
    return dumps(answer)
Exemplo n.º 3
0
def update():
    info = request.get_json()['info']
    code_board = info['codeBoard']
    id_cate = info['codeCategory']
    name_category = info['nameCategory']
    content = info['content']
    create_time = info['createTime']
    friend = info['dataFriendChecked']
    list_image = info['listImage']
    images = info['images']
    list_path = []
    board = Board.objects(id=code_board).first()
    for im in images:
        list_path.append(convert_and_save(im, "category"))

    cate = Category.objects(id=id_cate).first()
    if cate is not None and board is not None:
        cate.content = content
        cate.name = name_category
        cate.image = list_path
        cate.save()
        tokens = []
        for user in cate.list_user:
            tokens.append(user.expo_token)

            token = user.expo_token
            message = "Category " + cate.name + " has just been updated by " + current_user.username + " . Check-it \"@@!\""
            id_board = board.id
            id_category = cate.id
            name = cate.name
            image = cate.images[0]
            id_task = ""
            activity = "update"
            obj = "category"
            send_notification(token=token,
                              message=message,
                              user=user.username,
                              id_board=id_board,
                              id_category=id_category,
                              id_task=id_task,
                              activity=activity,
                              obj=obj,
                              name=name,
                              image=image)
        result = []
        result.append(tokens)
        result.append(cate.to_json())

        answer = Respone(True, result, "Update category success")
    else:
        answer = Respone(False, {}, "Update category failed")
    return dumps(answer)
Exemplo n.º 4
0
def get_by_id():  
    info = request.get_json()['info'] 
    print(info)
    username = info['username'] 
    user = User.objects(username=username).first()
    if user is not None:
        noties = []
        for item in user.list_notifications: 
            noties.append(item.to_json())   
        answer = Respone(True, noties, "Get notifications success")
    else:
        answer = Respone(True, {}, "Can't find username")
    print(answer)
    return json.dumps(answer)  
Exemplo n.º 5
0
def upload():
    # print(request.get_json())
    res = request.get_json()['data']
    convert_and_save(res)
    # i=Image.open(res['uri'])
    # print(i)
    # save_picture(res['uri'])

    # if current_user.is_authenticated:
    #     # user = User.objects.filter(username=username).first()
    #     # token = user.get_login_token(600)
    #     # answer = Respone(True, {'token':token}, "User have been already login")
    #     answer = Respone(True, {}, "User have been already login")
    #     return json.dumps(answer)

    # user = User.objects.filter(username=username).first()
    # if user and bcrypt.check_password_hash(user.password, password):
    #     time_expires = 600 #milisecond
    #     login_user(user, remember=False,duration=time_expires) #save user login
    #     token = user.get_login_token(time_expires)
    #     payload = {
    #         "username":user.username,
    #         "email":user.email,
    #         "role":user.role,
    #         "token": token
    #     }
    #     message="Login Successful"
    #     answer = Respone(True, payload, message)
    # else:
    answer = Respone(False, {}, "Email or Password isn't correct")
    return json.dumps(answer)
Exemplo n.º 6
0
def create():
    info = request.get_json()['info']
    code_board = info['codeBoard']
    name_category = info['nameCategory']
    content = info['content']
    create_time = info['createTime']
    friend = info['dataFriendChecked']
    list_image = info['listImage']
    images = info['images']
    list_path = []
    list_user = []
    tokens = []
    board = Board.objects(id=code_board).first()

    for im in images:
        list_path.append(convert_and_save(im, "category"))
        cate = Category(name=name_category,
                        content=content,
                        list_task=[],
                        images=list_path,
                        tags=[]).save()
    for username in friend:
        user = User.objects(username=username).first()
        list_user.append(user)
        tokens.append(user.expo_token)

        token = user.expo_token
        message = current_user.username + " has just created category " + cate.name + " . Hoorayyyy ^^!"
        id_board = board.id
        id_category = cate.id
        id_task = ""
        name = cate.name
        image = cate.images[0]
        activity = "create"
        obj = "category"
        send_notification(token=token,
                          message=message,
                          user=user.username,
                          id_board=id_board,
                          id_category=id_category,
                          id_task=id_task,
                          activity=activity,
                          obj=obj,
                          name=name,
                          image=image)

    cate.list_user = list_user
    cate.save()

    board.list_category.append(cate)
    board.save()

    result = []
    result.append(tokens)
    result.append(cate.to_json())

    answer = Respone(True, result, "Create category success")
    return dumps(answer)
Exemplo n.º 7
0
def get_device(): 
    list_cus = Customer.objects()
    data = []
    print(len(list_cus))
    for item in list_cus:
        data.append(item.to_json())
 
    answer = Respone(False, data, "Get Data")  
    return json.dumps(answer)  
Exemplo n.º 8
0
def upload(): 
    res = request.get_json()['data'] 
    token = res['token']
    
    cus = Customer.objects(token=token).first()
    if cus is None:
        print(res)
        altitude = str(res['location']['coords']['altitude'])
        altitudeAccuracy = str(res['location']['coords']['altitudeAccuracy'])
        latitude = str(res['location']['coords']['latitude'])
        longitude = str(res['location']['coords']['longitude'])
        heading = str(res['location']['coords']['heading'])
        speed = str(res['location']['coords']['speed'])
        cus = Customer(token=token,altitude=altitude,altitudeAccuracy=altitudeAccuracy,latitude=latitude,longitude=longitude,heading=heading,speed=speed).save()
        answer = Respone(True, {}, "Create new token") 
    else:
        answer = Respone(True, {}, "Token was exist"); 

    return json.dumps(answer)   
Exemplo n.º 9
0
def check_id():  
    info = request.get_json()['info'] 
    id_noti = info['idNoti'] 
    username = info['username'] 
    user = User.objects(username=username).first()
    if user is not None:
        noties = []
        for item in user.list_notifications: 
            noti = Notification.objects(id=item.id).first()
            print(id_noti, noti.id)
            if str(noti.id) == str(id_noti) :
                print("cheked",noti.id)
                noti.is_checked = True
            noti.save()
            noties.append(noti.to_json())   
        answer = Respone(True, noties, "Get notifications success")
    else:
        answer = Respone(True, {}, "Can't find username")
    print(answer)
    return json.dumps(answer)  
Exemplo n.º 10
0
def get_by_ids():
    print(request.get_json())
    info = request.get_json()['info']
    id_cate = info['id']
    cate = Category.objects(id=id_cate).first()
    list_task = []
    for item in cate.list_task:
        try:
            list_task.append(item.to_json())
        except:
            continue
    answer = Respone(True, list_task, "Get task success")
    return dumps(answer)
Exemplo n.º 11
0
def remove():
    info = request.get_json()['info']
    id_task = info['codeTask']
    task = Task.objects(id=id_task).first()
    if task is not None:
        tokens = []
        for user in task.list_user_task:
            tokens.append(user.expo_token)

            token = user.expo_token
            message = "Task " + task.name + " has removed by " + current_user.username + " ~~!"
            id_board = ""
            id_category = ""
            id_task = ""
            name = task.name
            image = task.images[0]
            activity = "remove"
            obj = "task"
            send_notification(token=token,
                              message=message,
                              user=user.username,
                              id_board=id_board,
                              id_category=id_category,
                              id_task=id_task,
                              activity=activity,
                              obj=obj,
                              name=name,
                              image=image)

        result = []
        result.append(tokens)
        result.append(task.to_json())
        task.delete()
        answer = Respone(True, result, "Remove task success")
    else:
        answer = Respone(False, {}, "Can't find task")
    return dumps(answer)
Exemplo n.º 12
0
def get_by_ids():
    info = request.get_json()['info']
    id_board = info['id']
    board = Board.objects(id=id_board).first()
    list_category = []

    for item in board.list_category:
        try:
            list_category.append(item.to_json())
        except:
            continue

    print("list_catego", list_category)
    answer = Respone(True, list_category, "Get category success")
    return dumps(answer)
Exemplo n.º 13
0
def remove():
    info = request.get_json()['info']
    id_cate = info['id_cate']
    cate = Category.objects(id=id_cate).first()
    if cate is not None:
        tokens = []
        for user in cate.list_user:
            tokens.append(user.expo_token)

            token = user.expo_token
            message = "Recently, category " + cate.name + " has been removed by " + current_user.username
            id_board = ""
            id_category = cate.id
            id_task = ""
            name = cate.name
            image = cate.images[0]
            activity = "remove"
            obj = "category"
            send_notification(token=token,
                              message=message,
                              user=user.username,
                              id_board=id_board,
                              id_category=id_category,
                              id_task=id_task,
                              activity=activity,
                              obj=obj,
                              name=name,
                              image=image)
        result = []
        result.append(tokens)
        result.append(cate.to_json())
        cate.delete()
        answer = Respone(True, result, "Remove category success")
    else:
        answer = Respone(False, {}, "Can't find category")
    return dumps(answer)
Exemplo n.º 14
0
def login():
    info = request.get_json()['info']
    username = info['username']
    password = info['password']
    expo_token = info['expoPushToken']
    if current_user.is_authenticated: 
        # user = User.objects.filter(username=username).first()
        # token = user.get_login_token(600)
        # answer = Respone(True, {'token':token}, "User have been already login")
        answer = Respone(True, {}, "User have been already login")
        return json.dumps(answer)   

    user = User.objects(username=username).first()
    if user is not None and bcrypt.check_password_hash(user.password, password): 
        time_expires = 600 #milisecond
        login_user(user, remember=False,duration=time_expires,force=True) #save user login 

        if current_user.is_anonymous is False:
            token = user.get_login_token(time_expires) 
            user.expo_token = expo_token
            user.save()
            payload = {
                "username":user.username,
                "email":user.email, 
                "name":user.first_name + " " + user.last_name,
                "expoToken": user.expo_token,
                "token": token
            } 
            message="Login Successful"
            answer = Respone(True, payload, message)   
        else:
            answer = Respone(False, {}, "Email or Password isn't correct")  
    else:
        answer = Respone(False, {}, "Email or Password isn't correct")  
    print(answer)
    return json.dumps(answer)   
Exemplo n.º 15
0
def logout():
    if current_user.is_authenticated: 
        logout_user()
        answer = Respone(True, {}, "Logout Success")  
        return json.dumps(answer)
    return render_template('index.html')
Exemplo n.º 16
0
def create():
    info = request.get_json()['info']
    code_board = info['codeBoard']
    code_cate = info['codeCategory']
    name_task = info['nameTask']
    content = info['content']
    startTime = info['startTime']
    finishTime = info['finishTime']
    friend = info['dataFriendChecked']
    tags = info['listTag']
    print(code_board, code_cate, name_task, content, friend, startTime,
          finishTime, tags)
    list_image = info['listImage']
    images = info['images']
    list_path = []
    for im in images:
        list_path.append(convert_and_save(im, "category"))
    list_user = []
    tokens = []

    board = Board.objects(id=code_board).first()
    category = Category.objects(id=code_cate).first()
    task = Task(name=name_task, images=list_path, content=content,
                tags=tags).save()
    category.list_task.append(task)
    category.save()

    for username in friend:
        user = User.objects(username=username).first()
        list_user.append(user)

        tokens.append(user.expo_token)
        token = user.expo_token
        message = current_user.username + " has just created task " + task.name + " successfully"
        id_board = board.id
        id_category = category.id
        id_task = task.id
        name = task.name
        image = task.images[0]
        activity = "create"
        obj = "task"
        send_notification(token=token,
                          message=message,
                          user=user.username,
                          id_board=id_board,
                          id_category=id_category,
                          id_task=id_task,
                          activity=activity,
                          obj=obj,
                          name=name,
                          image=image)

    task.list_user_task = list_user
    task.save()

    result = []
    result.append(tokens)
    result.append(category.to_json())
    answer = Respone(True, result, "Create task success")
    print(answer)
    return dumps(answer)
Exemplo n.º 17
0
def upload():
    res = request.get_json()['data']
    convert_and_save(res)
    answer = Respone(False, {}, "Email or Password isn't correct")
    return json.dumps(answer)