This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathhelper.py
133 lines (116 loc) · 5.16 KB
/
helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#Copyright (c) 2011 - Santiago Zavala
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.
import random
import string
import prefetch
import keys
import indextank
from urlparse import urlparse
from models import User, Post, Comment, Vote
from django.utils.html import escape
from django.template.defaultfilters import slugify
from google.appengine.api import taskqueue
def sanitizeHtml(value):
return escape(value)
def is_json(value):
if value.find('.json') >= 0:
return True
else:
return False
def parse_post_id(value):
if is_json(value):
return value.split('.')[0]
else:
return value
def add_childs_to_comment(comment):
"""We need to add the childs of each post because we want to render them in the
same way we render the Post view. So we need to find all the "preprocessed_childs"
Now, we also want to hold a reference to them to be able to pre_fetch them
"""
comment.processed_child = []
total_childs = []
for child in comment.childs:
comment.processed_child.append(child)
total_childs.append(child)
total_childs.extend(add_childs_to_comment(child))
return total_childs
def filter_user_comments(all_comments, user):
""" This function removes comments that belong to a thread
which had a comment by the same user as a parent """
res_comments = []
for user_comment in all_comments: ### Cycle all the comments and find the ones we care
linked_comment = user_comment
while(True):
if Comment.father.get_value_for_datastore(linked_comment) is None:
if not [c for c in res_comments if c.key() == user_comment.key()]:
res_comments.append(user_comment) # we care about the ones that are topmost
break
if linked_comment.father.user.key() == user.key():
if not [c for c in res_comments if c.key() == linked_comment.father.key()]:
res_comments.append(linked_comment.father) # But we also want to append the "father" ones to avoid having pages with 0 comments
break
linked_comment = linked_comment.father
# Add Childs here
child_list = []
for comment in res_comments:
comment.is_top_most = True
child_list.extend(add_childs_to_comment(comment))
prefetch.prefetch_comment_list(res_comments + child_list) #Finally we prefetch everything, 1 super call to memcache
return res_comments
def get_comment_from_list(comment_key,comments):
return [comment for comment in comments if comment.key() == comment_key]
def order_comment_list_in_memory(comments):
# order childs for display
for comment in comments:
comment.processed_child = []
for comment in comments:
father_key = Comment.father.get_value_for_datastore(comment)
if father_key is not None:
father_comment = get_comment_from_list(father_key,comments)
if len(father_comment) == 1:
father_comment[0].processed_child.append(comment)
return comments
def base_url(self):
uri = urlparse(self.request.url)
return uri.scheme +'://'+ uri.netloc
def sluglify(text):
return slugify(text)
def indextank_document(base_url, post):
if not keys.indextank_private_key:
return
api = indextank.client.ApiClient(keys.indextank_private_key)
index = api.get_index(keys.indextank_name_key)
if base_url == keys.base_url or base_url == keys.base_url_custom_url:
index = api.get_index(keys.indextank_name_key_prod)
nhurl = base_url+ "/noticia/" + str(post.nice_url)
try:
index.add_document(nhurl, {'text': post.title + ' ' + (post.message or post.url) + ' ' + post.user.nickname,
'user':post.user.nickname, 'title':post.title, 'message':post.message, 'url': post.url, 'nhurl': nhurl})
except indextank.client.HttpException:
pass
def get_session_id(session):
if session.has_key('random_id'):
return session['random_id']
else:
char_set = string.ascii_uppercase + string.digits
str = ''.join(random.sample(char_set,20))
session['random_id'] = str
return str
def killmetrics(category,subcategory,verb,session,user,r):
user_agent = str(r.request.headers['User-Agent'])
sessionUID = get_session_id(session)
taskqueue.add(url='/tasks/send_to_killmetrics', params={'category':category,'subcategory':subcategory,'verb':verb,'userUID': user, 'sessionUID':sessionUID,'user-agent':user_agent})