Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding Friendly URLs

  • Loading branch information...
commit a25e3fa2fa70bda86e55fa305c9d076c63f3f4bf 1 parent 3417bb8
@Tmeister Tmeister authored
Showing with 42 additions and 9 deletions.
  1. +18 −0 helper.py
  2. +14 −8 main.py
  3. +3 −0  models.py
  4. +7 −1 templates/post_bit.html
View
18 helper.py
@@ -16,10 +16,13 @@
#THE SOFTWARE.
import prefetch
+import htmlentitydefs, re
+import random
from urlparse import urlparse
from models import User, Post, Comment, Vote
from django.utils.html import escape
+
def sanitizeHtml(value):
return escape(value)
@@ -90,3 +93,18 @@ def order_comment_list_in_memory(comments):
def base_url(self):
uri = urlparse(self.request.url)
return uri.scheme +'://'+ uri.netloc
+
+def sluglify(text, separator = "-"):
+ ret = ""
+ for c in text.lower():
+ try:
+ ret += htmlentitydefs.codepoint2name[ord(c)]
+ except:
+ ret += c
+
+ ret = re.sub("([a-zA-Z])(uml|acute|grave|circ|tilde|cedil)", r"\1", ret)
+ ret = re.sub("\W", " ", ret)
+ ret = re.sub(" +", separator, ret)
+ ret += str( random.randrange(1, 999) )
+
+ return ret.strip()
View
22 main.py
@@ -37,6 +37,7 @@
from libs import PyRSS2Gen
from models import User, Post, Comment, Vote, Notification, Ticket
+
#register the desdetiempo filter to print time since in spanish
template.register_template_library('CustomFilters')
@@ -262,8 +263,12 @@ def get(self,post_id):
session = get_current_session()
if session.has_key('user'):
user = session['user']
+
try:
- post = db.get(helper.parse_post_id(post_id))
+ post = Post.all().filter('niceurl =', helper.parse_post_id( post_id ) ).get()
+ if post == None:
+ post = db.get( helper.parse_post_id( post_id ) )
+
comments = Comment.all().filter("post =", post.key()).order("-karma").fetch(1000)
comments = helper.order_comment_list_in_memory(comments)
prefetch.prefetch_comment_list(comments)
@@ -435,7 +440,8 @@ def post(self):
url = self.request.get('url')
title = helper.sanitizeHtml(self.request.get('title'))
message = helper.sanitizeHtml(self.request.get('message'))
-
+ niceurl = helper.sluglify( title )
+
if session.has_key('user'):
if len(title) > 0:
user = session['user']
@@ -446,12 +452,12 @@ def post(self):
url_exists = q > 0
try:
if not url_exists:
- post = Post(url=url,title=title,message=message, user=user)
- post.put()
+ post = Post(url=url,title=title,message=message, user=user, niceurl=niceurl)
+ post.put()
vote = Vote(user=user, post=post, target_user=post.user)
vote.put()
Post.remove_cached_count_from_memcache()
- self.redirect('/noticia/' + str(post.key()));
+ self.redirect('/noticia/' + str(post.niceurl));
else:
session['post_error'] = "Este link ha sido entregado en los ultimo 7 dias"
self.redirect('/agregar')
@@ -459,14 +465,14 @@ def post(self):
session['post_error'] = "El formato del link no es valido"
self.redirect('/agregar')
else:
- post = Post(title=title,message=message, user=user)
+ post = Post(title=title,message=message, user=user, niceurl=niceurl)
post.put()
- post.url = helper.base_url(self) + "/noticia/" + str(post.key())
+ post.url = helper.base_url(self) + "/noticia/" + post.niceurl
post.put()
Post.remove_cached_count_from_memcache()
vote = Vote(user=user, post=post, target_user=post.user)
vote.put()
- self.redirect('/noticia/' + str(post.key()));
+ self.redirect('/noticia/' + post.niceurl);
else:
session['post_error'] = "Necesitas agregar un titulo"
self.redirect('/agregar')
View
3  models.py
@@ -27,6 +27,7 @@
from urlparse import urlparse
from datetime import datetime
+
# Models
class User(db.Model):
lowercase_nickname = db.StringProperty(required=True)
@@ -92,6 +93,7 @@ def remove_notifications_from_memcache(self):
class Post(db.Model):
title = db.StringProperty(required=True)
url = db.LinkProperty(required=False)
+ niceurl = db.StringProperty(required=False)
message = db.TextProperty()
user = db.ReferenceProperty(User, collection_name='posts')
created = db.DateTimeProperty(auto_now_add=True)
@@ -99,6 +101,7 @@ class Post(db.Model):
edited = db.BooleanProperty(default=False)
twittered = db.BooleanProperty(default=False)
+
def to_json(self):
return {
'id':str(self.key()),
View
8 templates/post_bit.html
@@ -18,7 +18,13 @@
<div class="post-info">
{{post.prefetched_sum_votes}} puntos, entregado por <a href="/perfil/{{post.user.nickname}}">{{post.user.nickname}}</a> hace {{post.created|timesince|hacetiempo}}
{% if post.edited %}*{% endif %}
- | <a href="/noticia/{{post.key}}">{{post.cached_comment_count}} comentarios</a>
+ |
+ {% if not post.niceurl %}
+ <a href="/noticia/{{post.key}}">{{post.cached_comment_count}} comentarios</a>
+ {% else %}
+ <a href="/noticia/{{post.niceurl}}">{{post.cached_comment_count}} comentarios</a>
+ {% endif %}
{% if post.can_edit %}| <a href="/editar-noticia/{{post.key}}">editar</a> {% endif %}
</div>
</div>
+
Please sign in to comment.
Something went wrong with that request. Please try again.