Exemplo n.º 1
0
def wishes(userid):
    if request.method == "GET":
        user = db.session.query(User).filter_by(id=userid).first()
        wishes = db.session.query(Wish).filter_by(userid=user.id).all()
        wishlist = []
        for wish in wishes:
            wishlist.append({
                'title': wish.name,
                'url': wish.url,
                'thumbnail': wish.thumbnail,
                'description': wish.description,
                'addon': timeinfo(wish.addon)
            })
        if (len(wishlist) > 0):
            response = jsonify({
                "error": "null",
                "data": {
                    "user": user.first_name + " " + user.last_name,
                    "wishes": wishlist
                },
                "message": "Success"
            })
        else:
            response = jsonify({
                "error": "1",
                "data": {},
                "message": "Unable to get wishes"
            })
        return response
    else:
        user = db.session.query(User).filter_by(id=userid).first()
        json_data = json.loads(request.data)
        wish = Wish(user.id, json_data.get('url'), json_data.get('thumbnail'),
                    json_data.get('title'), json_data.get('description'),
                    datetime.now())
        if wish:
            db.session.add(wish)
            db.session.commit()
            response = jsonify({
                "error": "null",
                "data": {
                    'userid': userid,
                    'url': json_data.get('url'),
                    'thumbnail': wish.thumbnail,
                    'title': json_data.get('title'),
                    'description': json_data.get('description')
                },
                "message": "Success"
            })
        else:
            response = jsonify({
                "error": "1",
                "data": {},
                'message': 'did not create wish'
            })
        return response
Exemplo n.º 2
0
def save_to_wish(isbn):
    # 判断书籍isbn是否有效,同时防止用户钻洞子自己送书给自己
    if current_user.check_before_save_to_list(isbn=isbn):
        with db.auto_commit():
            wish = Wish(recipient=current_user._get_current_object(), isbn=isbn)
            db.session.add(wish)
        flash("本书已添加至您的心愿清单中")
    else:
        flash("本书已存在于您的赠送清单或心愿清单中!")
    return redirect(url_for(".book_detail", isbn=isbn))
Exemplo n.º 3
0
def save_to_wish(isbn):
    if current_user.can_saveto_list(isbn):
        with db.auto_commit():
            wish = Wish()
            wish.isbn = isbn
            wish.uid = current_user.id
            db.session.add(wish)
    else:
        flash('这本书已存在于你的心愿单或者赠送清单,请勿重复添加')
    # 视图函数必须要返回值
    return redirect(url_for('web.book_detail', isbn=isbn))
    pass
Exemplo n.º 4
0
def save_to_wishes(book_id):
    note = current_user.can_save_list(book_id)

    if note['can_save']:
        with db.auto_commit():
            wish = Wish()
            wish.uid = current_user.id
            wish.book_id = note['book'].id
            db.session.add(wish)
    else:
        flash('这本书已添加至你的赠送清单或已存在于你的心愿清单,请不要重复添加')
    flash('成功加入心愿清单!')

    return redirect(url_for('web.book_detail', number=note['book'].number))
Exemplo n.º 5
0
def createWish(request):
    if request.method == 'POST':
        user = request.user
        response = json.loads(request.body)
        if 'WISH' in response:
            wish = Wish()
            wish.user = user
            wish.text = strip_tags(str(response['WISH']))
            wish.status = 0  # Not done; 1 Refused; 2 Accepted
            wish.save()
            data = errorCheckMessage(True, None)
        else:
            data = errorCheckMessage(False, "badFormat")
    else:
        data = errorCheckMessage(False, "badRequest")
    return JsonResponse(data)
Exemplo n.º 6
0
def wish_add_edit(id=None):

    form = AddOrEditWish()
    form.category.choices = get_unique_categories("wish")

    if id:
        wish = Wish.query.filter_by(id=int(id)).first()
        title = "Edit wish"
        edit = True
    else:
        wish = Wish()
        title = "Add wish"
        edit = False

    if request.method == 'POST' and form.validate_on_submit():

        wish.category = form.category.data
        if wish.category and form.add_category.data:
            wish.category = form.add_category.data

        wish.web_url = form.web_url.data
        wish.description = form.description.data
        wish.title = form.title.data
        wish.website = form.website.data
        wish.picture_url = "![picture](" + form.picture_url.data + ")"

        if id:
            db.session.commit()
            flash(f"Your changes on '{wish.title}' have been saved.")
        else:
            db.session.add(wish)
            db.session.commit()
            flash(f"The wish '{form.title.data}' was successfully added.")

        return redirect(url_for("wishes"))

    elif id and request.method == "GET":

        form.category.data = wish.category
        form.web_url.data = wish.web_url
        form.description.data = wish.description
        form.title.data = wish.title
        form.year.data = wish.year
        form.website.data = wish.website

    return render_template("wish.html", title=title, form=form, edit=edit)
Exemplo n.º 7
0
def createWish(request):
    if request.method == 'POST':
        user = request.user
        response = json.loads(request.body)
        if checkPermission(["WISH"], user):
            if 'WISH' in response:
                wish = Wish()
                wish.user = user
                wish.text = strip_tags(str(response['WISH']))
                wish.status = 0  # Not done; 1 Refused; 2 Accepted; 3 Read
                wish.save()
                data = errorCheckMessage(True, None, createWish)
            else:
                data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT,
                                         createWish)
        else:
            data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR,
                                     createWish, user)
    else:
        data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, createWish)
    return JsonResponse(data)
Exemplo n.º 8
0
def add_game_wish(game_id):
    db.session.add(Wish(user_id=current_user.id, game_id=game_id))
    db.session.commit()
    return redirect(request.referrer)
Exemplo n.º 9
0
def wishlist(uid):
    user = db.session.query(User).filter_by(uid=uid).first()

    if request.method == "POST":
        if not request.json:
            flash("Something went wrong on our side please give us a moment")
            abort(400)
        exp = ('name' not in request.json
               and 'thumbnail_url' not in request.json
               and 'description' not in request.json
               and 'url' not in request.json)
        if exp:
            flash("Something went wrong on our side please give us a moment")
            abort(400)
        while True:
            pid = randint(450000, 470000)
            if not db.session.query(
                    exists().where(Wish.pid == str(pid))).scalar():
                break
        name = request.json['name']
        description = request.json['description']
        url = request.json['url']
        thumbnail = request.json['thumbnaill']
        if user:
            wish = Wish(pid, name, description, url, thumbnail)
            db.session.add(wish)
            db.session.commit()
            err = None
            info = {
                'pid': wish.pid,
                'name': wish.name,
                'description': wish.description,
                'url': wish.url,
                'thumbnail': wish.thumbnail
            }
        else:
            flash("Something went wrong on our side please give us a moment")
            abort(404)
        return jsonify(error=None,
                       data={'info': info},
                       message="Everything is fine")
    elif request.method == "GET":
        if user:
            wishlst = []

            #FIGURE THIS OUT
            query = text(
                """SELECT wish.item_id, wish.title, wish.description, wish.url, wish.thumbnail FROM wish INNER JOIN users_wishes ON users_wishes.wish_id = wish.item_id WHERE users_wishes.user_id = :id"""
            )
            wishes = db.session.get_bind().execute(query, id=user.id)
            if wishes:
                for i in wishes:
                    wish = {
                        'pid': wish["pid"],
                        'name': wish["name"],
                        'description': wish["description"],
                        'url': wish["url"],
                        'thumbnail': wish["thumbnail"]
                    }
                    wishlst.append(wish)
                errors = None
                message = "Success"
                info = {"wishes": wishlst}
            else:
                errors = True
                message = "No wishes found"
                info = {"wishes": wishlst}
        else:
            flash("Something went wrong on our side please give us a moment")
            abort(404)
        return jsonify(error=errors, info=info, message=message)