Skip to content

Commit

Permalink
Add support for a bump button.
Browse files Browse the repository at this point in the history
  • Loading branch information
fatlotus committed Jan 26, 2016
1 parent 6e48977 commit d1c6dfe
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 43 deletions.
29 changes: 28 additions & 1 deletion caravel/controllers/listings.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def show_listing(listing):
@app.route("/<listing:listing>/publish", methods=["POST"])
def publish_listing(listing):
"""
Edits a listing.
Marks a listing as being published.
"""

if is_from_tor():
Expand All @@ -207,6 +207,33 @@ def publish_listing(listing):
return redirect(url_for("show_listing", listing=new_listing))


@app.route("/<listing:listing>/bump", methods=["POST"])
def bump_listing(listing):
"""
Moves a listing to the top of the home page.
"""

if is_from_tor():
abort(403)

requester = utils.Principal.from_request(request,
email=listing.principal.email)

if not requester.can_act_as(listing.principal) or not listing.can_bump:
abort(403)

new_listing = model.UnapprovedListing(id=listing.key.id())
new_listing.take_values_from(listing)
new_listing.principal = requester
new_listing.posted_at = datetime.datetime.now()
new_listing.put()

if not isinstance(new_listing, model.Listing):
flash("Your edit is awaiting moderation. "
"We'll email you when it is approved.")
return redirect(url_for("show_listing", listing=new_listing))


@app.route("/<listing:listing>/edit", methods=["GET", "POST"])
def edit_listing(listing):
"""
Expand Down
4 changes: 4 additions & 0 deletions caravel/model/listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class _Listing(CategoriesMixin, PhotosMixin, PrincipalMixin, TimeOrderMixin,
title = ndb.StringProperty()
body = ndb.TextProperty()

@property
def can_bump(self):
return self.age >= datetime.timedelta(days=7)


class Listing(SideEffectsMixin, _Listing):

Expand Down
7 changes: 5 additions & 2 deletions caravel/model/temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ class TimeOrderMixin(ndb.Model):

@property
def old(self):
horizon = datetime.datetime.now() - self.MARK_AS_OLD_AFTER
return (self.posted_at <= horizon)
return self.age >= self.MARK_AS_OLD_AFTER

@property
def age(self):
return datetime.datetime.now() - self.posted_at
4 changes: 4 additions & 0 deletions caravel/static/default.css
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,7 @@ form .thumbnail {
.help-page-shorter{
height:10em;
}

.inline-form {
display: inline;
}
20 changes: 20 additions & 0 deletions caravel/templates/listings/access_panel.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<form method="post" class="panel-body"
action="{{ url_for('publish_listing', listing=listing) }}">
<a class="btn btn-default"
href="{{ url_for('edit_listing', listing=listing) }}">
Edit</a>
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
<div class="btn-group">
{% if listing.sold %}
<button type="submit" class="btn btn-default"
name="sold" value="false">Available</button>
<button type="submit" class="btn btn-danger"
disabled>Sold</button>
{% else %}
<button type="submit" class="btn btn-success"
disabled>Available</button>
<button type="submit" class="btn btn-default"
name="sold" value="true">Sold</button>
{% endif %}
</div>
</form>
47 changes: 16 additions & 31 deletions caravel/templates/listings/fullpage.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,28 @@ <h2>{{ listing.title }}</h2>
Price: <strong>{{ '${:,.2f}'.format(listing.price) }}</strong>
</p>
{% if listing.sold %}
<p class="alert alert-warning">
<p class="alert alert-danger">
"<strong>{{ listing.title }}</strong>" has been sold,
preventing it from showing up in searches and blocking future inquiries.
</p>
{% elif listing.old %}
<p class="alert alert-warning">
<p class="alert alert-danger">
"<strong>{{ listing.title }}</strong>" was posted more than 30 days ago,
so it does not show up in searches, and will not receive inquiries.
It has likely already been sold.
</p>
{% elif listing.can_bump and
(not current_user or listing.principal.email == current_user.email())
%}
<form class="alert alert-warning" method="post"
action="{{ url_for('bump_listing', listing=listing) }}">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
<p>"<strong>{{ listing.title }}</strong>" was posted more than 7 days
ago, which means it may have fallen off the front page.</p>

<p><input type="submit" class="btn btn-warning"
value="Bump to top of front page"/></p>
</form>
{% endif %}
{% if is_admin %}
<p class="alert alert-info">
Expand All @@ -62,20 +74,7 @@ <h2>{{ listing.title }}</h2>
<div class="panel-heading">
<h3 class="panel-title">Manage Listing</h3>
</div>
<form class="panel-body" method="post"
action="{{ url_for('publish_listing', listing=listing) }}">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
<a class="btn btn-default"
href="{{ url_for('edit_listing', listing=listing) }}">
Edit</a>
{% if listing.sold %}
<button type="submit" class="btn btn-success"
name="sold" value="false">Mark as Unsold</button>
{% else %}
<button type="submit" class="btn btn-success"
name="sold" value="true">Mark as Sold</button>
{% endif %}
</form>
{% include "listings/access_panel.html" %}
</div>
{% elif listing.principal.auth_method == "GOOGLE_APPS" and not current_user %}
<div class="panel panel-default">
Expand All @@ -95,22 +94,8 @@ <h3 class="panel-title">
<h3 class="panel-title">
Suggest Changes</h3>
</div>
<form class="panel-body" method="post"
action="{{ url_for('publish_listing', listing=listing) }}">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
<a class="btn btn-default"
href="{{ url_for('edit_listing', listing=listing) }}">
Edit</a>
{% if listing.sold %}
<button type="submit" class="btn btn-success"
name="sold" value="false">Mark as Unsold</button>
{% else %}
<button type="submit" class="btn btn-success"
name="sold" value="true">Mark as Sold</button>
{% endif %}
</form>
{% include "listings/access_panel.html" %}
</div>

{% endif %}
{% if not listing.sold and not listing.old %}
<div class="panel panel-default">
Expand Down
18 changes: 18 additions & 0 deletions caravel/tests/test_listings.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,21 @@ def test_old_listing(self):
self.listing_a.put()

self.assertLongString(self.get("/listing_a").data)

def test_bump_listing(self):
# Test the can_bump property.
self.assertFalse(self.listing_a.can_bump)
self.listing_a.posted_at -= datetime.timedelta(days=10)
self.listing_a.put()
self.assertTrue(self.listing_a.can_bump)

# Try to bump a listing.
self.assertLongString(self.get("/listing_a").data)
with self.google_apps_user("seller-a@uchicago.edu"):
self.post("/listing_a/bump", data={
"csrf_token": self.csrf_token("/listing_a/edit"),
})

self.listing_a = self.listing_a.key.get()
self.assertFalse(self.listing_a.can_bump)
self.assertTrue(self.listing_a.age <= datetime.timedelta(seconds=60))
15 changes: 15 additions & 0 deletions caravel/tests/test_listings_bump_listing_expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
New Listing
Listing ☆A
cars
Posted 2w ago .
Price: $3.10
" Listing ☆A " was posted more than 7 days
ago, which means it may have fallen off the front page.
Body of ☆A
Manage Listing
Sign in with CNetID
to edit.
Contact Seller
From
Sign in with CNetID or
Message
3 changes: 2 additions & 1 deletion caravel/tests/test_listings_edit_listing_expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ None with
Body☆A
Manage Listing
Edit
Mark as Sold
Available
Sold
Contact Seller
From
seller-a@uchicago.edu ( Logout )
Expand Down
3 changes: 2 additions & 1 deletion caravel/tests/test_listings_new_listing_expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Price: $3.44
Body of ☆D
Suggest Changes
Edit
Mark as Sold
Available
Sold
Contact Seller
From
Sign in with CNetID or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ None with
Body of ☆D
Manage Listing
Edit
Mark as Sold
Available
Sold
Contact Seller
From
visitor@uchicago.edu ( Logout )
Expand Down
3 changes: 2 additions & 1 deletion caravel/tests/test_listings_post_inquiry_expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Price: $71.10
Body of ☆B
Suggest Changes
Edit
Mark as Sold
Available
Sold
Contact Seller
From
Sign in with CNetID or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ mozilla.
Body of ☆B
Suggest Changes
Edit
Mark as Sold
Available
Sold
Contact Seller
From
visitor@uchicago.edu ( Logout )
Expand Down
12 changes: 8 additions & 4 deletions caravel/tests/test_listings_publish_listing_expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Price: $71.10
Body of ☆B
Suggest Changes
Edit
Mark as Sold
Available
Sold
Contact Seller
From
Sign in with CNetID or
Expand All @@ -23,7 +24,8 @@ preventing it from showing up in searches and blocking future inquiries.
Body of ☆B
Suggest Changes
Edit
Mark as Unsold
Available
Sold
---
New Listing
Listing ☆A
Expand All @@ -48,7 +50,8 @@ preventing it from showing up in searches and blocking future inquiries.
Body of ☆B
Suggest Changes
Edit
Mark as Unsold
Available
Sold
---
New Listing
Listing ☆B
Expand All @@ -58,7 +61,8 @@ Price: $71.10
Body of ☆B
Suggest Changes
Edit
Mark as Sold
Available
Sold
Contact Seller
From
Sign in with CNetID or
Expand Down

0 comments on commit d1c6dfe

Please sign in to comment.