From: Michael Tremer Date: Tue, 7 Apr 2015 16:00:04 +0000 (+0200) Subject: planet: Slight face-lift and add hottest posts X-Git-Url: http://git.ipfire.org/?p=ipfire.org.git;a=commitdiff_plain;h=d88b8f41fa5fa24eb01422894a5c2f2d864e6cef planet: Slight face-lift and add hottest posts --- diff --git a/templates/modules/menu.html b/templates/modules/menu.html index f4a77861..66df0d2f 100644 --- a/templates/modules/menu.html +++ b/templates/modules/menu.html @@ -154,6 +154,10 @@
  • {{ _("Statistics") }}
  • + {% elif hostname == "planet.ipfire.org" %} +
  • + {{ _("Hottest posts") }} +
  • {% elif hostname == "talk.ipfire.org" and current_user %}
  • {{ _("Phonebook") }} diff --git a/templates/modules/planet-entry.html b/templates/modules/planet-entry.html index 0b125ab3..62841b9c 100644 --- a/templates/modules/planet-entry.html +++ b/templates/modules/planet-entry.html @@ -6,13 +6,15 @@

    {{ entry.title }} + +
    + + + {{ _("by") }} {{ entry.author.name }}, + {{ locale.format_date(entry.published, shorter=True) }} +

    {% raw entry.text %} -

    - {{ _("Posted by") }} {{ entry.author.name }} - {{ _("on") }} {{ locale.format_date(entry.published, shorter=True) }} -

    -
    diff --git a/templates/planet/hottest.html b/templates/planet/hottest.html new file mode 100644 index 00000000..7f1a0448 --- /dev/null +++ b/templates/planet/hottest.html @@ -0,0 +1,23 @@ +{% extends "base.html" %} + +{% block title %}{{ _("Hottest posts") }}{% end block %} + +{% block body %} +
    +
    +

    {{ _("The hottest posts of the last month") }}

    + +
    + {% for entry in entries %} +
    {{ entry.title }}
    +
    + {{ _("by") }} {{ entry.author.name }} + {{ locale.format_date(entry.published, relative=True, shorter=True) }} +
    + +
    + {% end %} +
    +
    +
    +{% end block %} diff --git a/templates/planet/modules/author-box.html b/templates/planet/modules/author-box.html new file mode 100644 index 00000000..9a27605d --- /dev/null +++ b/templates/planet/modules/author-box.html @@ -0,0 +1,26 @@ +
    + {{ author.name }} + +

    + {{ author.name }} +

    + +
    + +
      +
    • + + + +
    • + + {% if author.is_talk_enabled() %} +
    • + + + +
    • + {% end %} +
    +
    diff --git a/templates/planet/posting.html b/templates/planet/posting.html index 5fa44845..9be8c74a 100644 --- a/templates/planet/posting.html +++ b/templates/planet/posting.html @@ -1,11 +1,18 @@ -{% extends "user.html" %} +{% extends "../base.html" %} -{% block title %}{{ _("IPFire Planet") }} - {{ entry.title }}{% end block %} +{% block title %}{{ entry.title }}{% end block %} -{% block bodyA %} +{% block body %} @@ -16,10 +23,7 @@ {% end %} - {% raw entry.text %} + {% module PlanetAuthorBox(entry.author) %} -

    - {{ _("Posted by") }} {{ entry.author.name }} - {{ _("on") }} {{ locale.format_date(entry.published, shorter=True) }} -

    + {% raw entry.text %} {% end block %} diff --git a/templates/planet/user.html b/templates/planet/user.html index 6ec4cf2e..ce57a41a 100644 --- a/templates/planet/user.html +++ b/templates/planet/user.html @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "../base.html" %} {% block title %}{{ _("IPFire Planet") }} - {{ author.name }}{% end block %} @@ -36,23 +36,7 @@
    - + {% module PlanetAuthorBox(entry.author) %}
    {% end block %} diff --git a/webapp/__init__.py b/webapp/__init__.py index 6cab6c0d..de574b91 100644 --- a/webapp/__init__.py +++ b/webapp/__init__.py @@ -50,6 +50,7 @@ class Application(tornado.web.Application): "NewsLine" : NewsLineModule, "NewsTable" : NewsTableModule, "NewsYearNavigation" : NewsYearNavigationModule, + "PlanetAuthorBox" : PlanetAuthorBoxModule, "PlanetEntry" : PlanetEntryModule, "PlanetSearchBox" : PlanetSearchBoxModule, "ProgressBar" : ProgressBarModule, @@ -145,6 +146,7 @@ class Application(tornado.web.Application): # planet.ipfire.org self.add_handlers(r"planet(\.dev)?\.ipfire\.org", [ (r"/", PlanetMainHandler), + (r"/hottest", PlanetHotEntriesHandler), (r"/post/([A-Za-z0-9_-]+)", PlanetPostingHandler), (r"/user/([a-z0-9_-]+)", PlanetUserHandler), (r"/search", PlanetSearchHandler), diff --git a/webapp/backend/planet.py b/webapp/backend/planet.py index c4f43207..d116ced3 100644 --- a/webapp/backend/planet.py +++ b/webapp/backend/planet.py @@ -115,8 +115,9 @@ class PlanetEntry(Object): def is_published(self): return self.status == "published" - def increase_view_counter(self): - self.db.execute("UPDATE planet SET views = views + 1 WHERE id = %s", self.id) + def count_view(self, referer=None, location=None): + self.db.execute("INSERT INTO planet_views(post_id, referer, location) \ + VALUES(%s, %s, %s)", self.id, referer, location) class Planet(Object): @@ -196,6 +197,17 @@ class Planet(Object): return [PlanetEntry(self.backend, e) for e in entries] + def get_hot_entries(self, days=30, limit=8): + entries = self.db.query("WITH hottest AS (SELECT post_id, COUNT(post_id) AS count \ + FROM planet_views WHERE \"when\" >= NOW() - INTERVAL '%s days' \ + GROUP BY post_id ORDER BY count DESC) SELECT * FROM planet \ + LEFT JOIN hottest ON planet.id = hottest.post_id \ + WHERE hottest.count IS NOT NULL \ + ORDER BY hottest.count DESC LIMIT %s", + days, limit) + + return [PlanetEntry(self.backend, e) for e in entries] + def render(self, text, limit=0): if limit and len(text) >= limit: text = text[:limit] + "..." diff --git a/webapp/handlers_planet.py b/webapp/handlers_planet.py index cd8cf348..1a3ba765 100644 --- a/webapp/handlers_planet.py +++ b/webapp/handlers_planet.py @@ -20,6 +20,19 @@ class PlanetMainHandler(PlanetBaseHandler): self.render("planet/index.html", entries=entries, offset=offset + limit, limit=limit) +class PlanetHotEntriesHandler(PlanetBaseHandler): + def get(self): + days = self.get_argument("days", None) + try: + days = int(days) + except (TypeError, ValueError): + days = 30 + + entries = self.planet.get_hot_entries(days, limit=25) + + self.render("planet/hottest.html", entries=entries) + + class PlanetUserHandler(PlanetBaseHandler): def get(self, author): author = self.accounts.get_by_uid(author) @@ -38,16 +51,24 @@ class PlanetUserHandler(PlanetBaseHandler): class PlanetPostingHandler(PlanetBaseHandler): def get(self, slug): - entry = self.planet.get_entry_by_slug(slug) + self.entry = self.planet.get_entry_by_slug(slug) - if not entry: + if not self.entry: raise tornado.web.HTTPError(404) - # Update the view counter - entry.increase_view_counter() - self.render("planet/posting.html", - author=entry.author, entry=entry) + author=self.entry.author, entry=self.entry) + + def on_finish(self): + assert self.entry + + # Get the referer and location for statistical purposes + referer = self.request.headers.get("Referer", None) + location = self.get_remote_location() + if location: + location = location.country + + self.entry.count_view(referer=referer, location=location) class PlanetSearchHandler(PlanetBaseHandler): diff --git a/webapp/ui_modules.py b/webapp/ui_modules.py index b85fac47..454557cf 100644 --- a/webapp/ui_modules.py +++ b/webapp/ui_modules.py @@ -303,6 +303,11 @@ class DownloadButtonModule(UIModule): release=release, image=best_image) +class PlanetAuthorBoxModule(UIModule): + def render(self, author): + return self.render_string("planet/modules/author-box.html", author=author) + + class PlanetEntryModule(UIModule): def render(self, entry, show_avatar=True): return self.render_string("modules/planet-entry.html",