示例#1
0
def collection():
    args = request.json

    user_id = args['user_id']
    collection_op_type = args['op_type']
    collection_name = args['collection_name']

    if collection_op_type == 'new':
        Collection.create(name=collection_name, user_id=user_id)
    elif collection_op_type == 'edit':
        Collection.get(args['collection_id']).update({'name': collection_name})

    return ''
示例#2
0
def signup():
    form = SignupForm()
    if request.method == "POST" and form.validate():
        username = form.username.data
        password = form.password.data
        email = form.email.data

        if User.find_by_username(username):
            flash('Sorry, but this username is already taken.', 'warning')
        else:
            user = User.get(User.new(username, password, email))
            collection_id = Collection.create(name=form.collection_name.data
                                              or 'My collection',
                                              user_id=user.get_id())
            user.update({'selected_collection': collection_id})
            remember = form.remember_me.data
            if login_user(user, remember=remember):
                flash("Logged in!", 'success')
                return redirect(url_for("index"))
            else:
                flash("Sorry, but you could not log in.", 'danger')
    else:
        for field_name, error_messages in form.errors.items():
            for err in error_messages:
                flash(
                    'error with {}: {}'.format(field_name.replace('_', ' '),
                                               err), 'danger')

    return render_template("signup.html", form=form)
示例#3
0
def collections():
    form = CollectionAddForm()
    if form.validate_on_submit():
        with db.atomic() as txn:
            try:
                Collection.create(
                    name=form.name.data,
                    user=current_user.get_id(),
                )
            except IntegrityError:
                flash('A collection with that name already exists.')
        return redirect(url_for('collections'))
    else:
        collections = (Collection.select(
            Collection,
            fn.COUNT(QuoteCollection.id).alias('quote_count'),
        ).join(QuoteCollection, JOIN.LEFT_OUTER).group_by(Collection).where(
            Collection.user == current_user.get_id(), ))
        return render_template(
            'collections.html',
            form=form,
            collections=collections,
        )
示例#4
0
 def post():
     return Collection.create(**get_request_json())