示例#1
0
def delete_item(list_id):
    if 'access_token' in session:
        print 'list_id: %s, access_token: %s' % (list_id, request.args.get('access_token'))
        access_token = session.get('access_token', None)
        db = connect_db ()
        try:
            user_info = auth.authenticate_using_access_token(access_token, db)
            user_id = user_info['id']
            list_info = dm.fetch_list(list_id, user_id, db)
        except AccessTokenExpiredError:
            flash("Access Token Expired. Login again")
            return redirect(url_for('login'))
        except InvalidAccessTokenError:
            flash("In Valid Access Token. Login again")
            return redirect(url_for('login'))
        except NoListFoundError:
            flash("No List found with this id:%d" %(list_id))
            return redirect(url_for('users'))
    else:
        return redirect(url_for('login'))

    try:
        item_id = request.form['item_id']
        dm.delete_item(user_id, list_id, item_id, db)
        list_info = dm.fetch_list(list_id, user_id, db)
    except NoItemFoundError:
        flash("No item found")
        return redirect(url_for('users'))
    except NoListFoundError:
        flash("No List found with this id:%d" %(list_id))
        return redirect(url_for('users'))
    response = make_response(render_template('list_info.html', list_info=list_info))
    return redirect(url_for('lists', list_id = list_id))
def delete_item_handler(update, context):
    user_id = update.message.from_user.id
    arguments = context.args
    if dm.check_user_exist(str(user_id)) is True:
        if len(arguments) > 0:
            name = ' '.join(arguments)
            deleted = dm.delete_item(str(user_id), name)
            update.message.delete()
            if deleted is True:
                update.message.reply_text(f'*Item {name} deleted*',
                                          parse_mode=ParseMode.MARKDOWN)
            else:
                update.message.reply_text(f"*Item {name} doesn't exist*",
                                          parse_mode=ParseMode.MARKDOWN)
        else:
            update.message.delete()
            update.message.reply_text(f'*You should type correct name*',
                                      parse_mode=ParseMode.MARKDOWN)
    else:
        update.message.reply_text(
            f'You are new here, *{update.message.from_user.username}*, type */start* first',
            parse_mode=ParseMode.MARKDOWN)
示例#3
0
def Item_modification(list_id, item_id):
    query_access_token = request.args.get('access_token')
    db = connect_db ()
    try:
        user_info = auth.authenticate_using_access_token(query_access_token, db)
        user_id = user_info['id']
    except AccessTokenExpiredError:
        response_data = {
            "meta" : {},
            "data" : {
                "users" : [{
                    "authentication" : 'Expired',
                    "message" : "Access token Expired"
                }]
            }
        }
        status = 401
        body = json.dumps(response_data)
        headers = {
            'Content-Type' : 'application/json'
        }
        return (body, status, headers)
    except InvalidAccessTokenError:
        response_data = {
            "meta" : {},
            "data" : {
                "users" : [
                    {
                        "message" : "Invalid access token : %s" % (query_access_token)
                    }
                ]
            }
        }
        status = 400
        body = json.dumps(response_data)
        headers = {
            'Content-Type' : 'application/json'
        }
        return (body, status, headers)

    #Change requests for name and status of an item
    if request.method == 'PUT':
        data = request.json['data']['lists'][0]['items'][0]
        print data
        if 'name' in data:
            try:
                list_item_info = dm.change_item_name(user_id, list_id, item_id, data['name'], db)
            except NoItemFoundError:
                response_data = {
                    "meta" : {},
                    "data" : {
                        "lists" : [{
                            "message" : "No Item found of this list"
                        }]
                    }
                }
                body = json.dumps(response_data)
                status = 400
                headers = {
                    "Content-Type" : 'application/json'
                }
                return (body,status,headers)
            except NoListFoundError:
                response_data = {
                    "meta" : {},
                    "data" : {
                        "lists" : [{
                            "message" : "No List found of this user"
                        }]
                    }
                }
                body = json.dumps(response_data)
                status = 400
                headers = {
                    "Content-Type" : 'application/json'
                }
                return (body,status,headers)
            response_data = {
                "meta" : {},
                "data" : {
                    "lists" : [
                        list_item_info
                    ]
                }
            }
            status = 400
            body = json.dumps(response_data)
            headers = {
                "Content-Type" : 'application/json'
            }
            return (body,status,headers)
        else:
            try:
                list_item_info = dm.change_item_status(user_id, list_id, item_id, data['status'], db)
            except NoItemFoundError:
                response_data = {
                    "meta" : {},
                    "data" : {
                        "lists" : [{
                            "message" : "No Item found of this list"
                        }]
                    }
                }
                body = json.dumps(response_data)
                status = 400
                headers = {
                    "Content-Type" : 'application/json'
                }
                return (body,status,headers)
            except NoListFoundError:
                response_data = {
                    "meta" : {},
                    "data" : {
                        "lists" : [{
                            "message" : "No List found of this user"
                        }]
                    }
                }
                body = json.dumps(response_data)
                status = 400
                headers = {
                    "Content-Type" : 'application/json'
                }
                return (body,status,headers)
            response_data = {
                "meta" : {},
                "data" : {
                    "lists" : [
                        list_item_info
                    ]
                }
            }
            status = 400
            body = json.dumps(response_data)
            headers = {
                "Content-Type" : 'application/json'
            }
            return (body,status,headers)

    else:
        try:
            dm.delete_item(user_id, list_id, item_id, db)
        except NoListFoundError:
            response_data = {
                "meta" : {},
                "data" : {
                    "lists" : [{
                        "message" : "No List found of this user"
                    }]
                }
            }
            body = json.dumps(response_data)
            status = 400
            headers = {
                "Content-Type" : 'application/json'
            }
            return (body,status,headers)
        except NoItemFoundError:
            response_data = {
                "meta" : {},
                "data" : {
                    "lists" : [{
                        "message" : "No Item found of this list"
                    }]
                }
            }
            body = json.dumps(response_data)
            status = 400
            headers = {
                "Content-Type" : 'application/json'
            }
            return (body,status,headers)
        response_data = {
            "meta" : {},
            "data" : {
                "lists" : [
                    {
                        "items": {
                            "message" : "Item deleted from the list"
                        }
                    }
                ]
            }
        }
        status = 400
        body = json.dumps(response_data)
        headers = {
            "Content-Type" : 'application/json'
        }
        return (body,status,headers)