Beispiel #1
0
def unhide_ad(url):
    room = Room.query.filter_by(urlname=url).first()
    if not room:
        abort(404)
    if room.dead or get_days_ago(room.created_at) > OLD_DAYS.days:
        abort(404)
    occupied = Occupied.query.filter_by(space=room.occupieds).order_by("created_at").first()
    if occupied and get_days_ago(occupied.created_at) > OCCUPIED_DAYS.days:
        abort(404)
    form = ConfirmActionForm()
    if form.validate_on_submit():
        if "yes" in request.form:
            if room.user == g.user:
                room.occupieds = OccupiedSpace()
                db.session.commit()
                flash("Your ghosla has been un-marked as occupied", category="success")
            else:
                abort(403)
            return redirect(url_for("index"))
        else:
            return redirect(url_for("view_ad", url=url))
    if room.user == g.user:
        message = u"""Do you really wish to remove all occupied marks on your Ghosla '%s, %s'?
            Please do this, only if your Ghosla is really unoccupied."""
    return render_template(
        "confirm_action.html", form=form, title=u"Confirm un-occupied", message=message % (room.address, room.city)
    )
Beispiel #2
0
def hide_ad(url):
    room = Room.query.filter_by(urlname=url).first()
    if not room:
        abort(404)
    if room.dead or get_days_ago(room.created_at) > OLD_DAYS.days:
        abort(404)
    occupied = Occupied.query.filter_by(space=room.occupieds).order_by("created_at").first()
    if occupied and get_days_ago(occupied.created_at) > OCCUPIED_DAYS.days:
        abort(404)
    form = ConfirmActionForm()
    if form.validate_on_submit():
        if "yes" in request.form:
            if room.user == g.user:
                room.dead = True
                db.session.commit()
                flash("Your ghosla has been marked occupied", category="success")
            else:
                room.occupieds.occupied(g.user)
                db.session.commit()
                flash("The ghosla has been marked occupied", category="success")
            return redirect(url_for("index"))
        else:
            return redirect(url_for("view_ad", url=url))
    if room.user == g.user:
        message = u"""Do you really wish to mark your Ghosla '%s, %s' as occupied?
                This will remove all comments as well. This operation is permanent
                and cannot be undone."""
    else:
        message = u"""Are you sure you want to mark '%s, %s' as occupied? Please
               do so, only if you have contacted the ad poster and confirmed."""
    return render_template(
        "confirm_action.html", form=form, title=u"Confirm occupied", message=message % (room.address, room.city)
    )
Beispiel #3
0
def edit_ad(url):
    room = Room.query.filter_by(urlname=url).first()
    if not room and room.user == g.user:
        abort(404)
    if room.dead or get_days_ago(room.created_at) > OLD_DAYS.days:
        abort(404)
    occupied = Occupied.query.filter_by(space=room.occupieds).order_by("created_at").first()
    if occupied and get_days_ago(occupied.created_at) > OCCUPIED_DAYS.days:
        abort(404)
    if room.is_available:
        form = AvailableAdForm()
    else:
        form = WantedAdForm()
    form.starting.flags.is_date = True
    if request.method == "GET":
        form.process(obj=room)
        if room.is_available:
            form.room_pref.data = factorize(room.room_pref)
        else:
            form.room_type.data = factorize(room.room_type)
        form.email.data = g.user.email
    elif form.validate_on_submit():
        if room.is_available:
            form.room_pref.data = product(form.room_pref.data)
        else:
            form.room_type.data = product(form.room_type.data)
        form.populate_obj(room)
        db.session.commit()
        return redirect(url_for("view_ad", url=room.urlname))
    return render_template("autoform.html", form=form, title="Edit advertisement", submit="Save")
Beispiel #4
0
def view_ad(url):
    room = Room.query.filter_by(urlname=url).first()
    if not room:
        abort(404)
    if room.dead or get_days_ago(room.created_at) > OLD_DAYS.days:
        abort(404)
    occupied = Occupied.query.filter_by(space=room.occupieds).order_by("created_at").first()
    if occupied and get_days_ago(occupied.created_at) > OCCUPIED_DAYS.days:
        abort(404)
    # URL is okay. Show the ad.
    comments = Comment.query.filter_by(commentspace=room.comments, parent=None).order_by("created_at").all()
    commentform = CommentForm()
    delcommentform = DeleteCommentForm()
    if request.method == "POST":
        if request.form.get("form.id") == "newcomment" and commentform.validate():
            if commentform.edit_id.data:
                comment = Comment.query.get(int(commentform.edit_id.data))
                if comment:
                    if comment.user == g.user:
                        comment.message = commentform.message.data
                        flash("Your comment has been edited", category="info")
                    else:
                        flash("You can only edit your own comments", category="info")
                else:
                    flash("No such comment", category="error")
            else:
                comment = Comment(user=g.user, commentspace=room.comments, message=commentform.message.data)
                if commentform.parent_id.data:
                    parent = Comment.query.get(int(commentform.parent_id.data))
                    if parent and parent.commentspace == room.comments:
                        comment.parent = parent
                room.comments.count += 1
                db.session.add(comment)
                flash("Your comment has been posted", category="success")
            db.session.commit()
            # Redirect despite this being the same page because HTTP 303 is required to not break
            # the browser Back button
            return redirect(url_for("view_ad", url=room.urlname) + "#c" + str(comment.id), code=303)
        elif request.form.get("form.id") == "delcomment" and delcommentform.validate():
            comment = Comment.query.get(int(delcommentform.comment_id.data))
            if comment:
                if comment.user == g.user:
                    comment.delete()
                    room.comments.count -= 1
                    db.session.commit()
                    flash("Your comment was deleted.", category="success")
                else:
                    flash("You did not post that comment.", category="error")
            else:
                flash("No such comment.", category="error")
            return redirect(url_for("view_ad", url=room.urlname), code=303)
    return render_template(
        "room.html", room=room, comments=comments, commentform=commentform, delcommentform=delcommentform
    )