def get(self, topic_num): site = GetSite() browser = detect(self.request) template_values = {} template_values['site'] = site template_values['system_version'] = SYSTEM_VERSION errors = 0 template_values['errors'] = errors member = CheckAuth(self) l10n = GetMessages(self, member, site) template_values['l10n'] = l10n if (member): if (member.num == 1): template_values['member'] = member topic = False q = db.GqlQuery("SELECT * FROM Topic WHERE num = :1", int(topic_num)) if (q.count() == 1): topic = q[0] try: topic.hits = topic.hits + 1 topic.put() except: topic.hits = topic.hits - 1 template_values['topic'] = topic if (topic): template_values['topic_title'] = topic.title template_values['topic_content'] = topic.content node = False section = False if topic: # q2 = db.GqlQuery("SELECT * FROM Node WHERE num = :1", topic.node_num) q2 = Node.all().filter('name =', topic.node_name) node = q2[0] # q3 = db.GqlQuery("SELECT * FROM Section WHERE num = :1", node.section_num) q3 = Node.all().filter('name =', q2[0].category) section = q3[0] template_values['node'] = node template_values['section'] = section q4 = db.GqlQuery( "SELECT * FROM Reply WHERE topic_num = :1 ORDER BY created ASC", topic.num) template_values['replies'] = q4 if browser['ios']: path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'edit_topic.html') else: path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'edit_topic.html') else: path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'topic_not_found.html') output = template.render(path, template_values) self.response.out.write(output) else: self.redirect('/t/' + str(topic_num)) else: self.redirect('/signin')
def get(self, node_name): site = GetSite() browser = detect(self.request) template_values = {} template_values['site'] = site template_values['system_version'] = SYSTEM_VERSION member = CheckAuth(self) l10n = GetMessages(self, member, site) template_values['l10n'] = l10n template_values[ 'page_title'] = site.title + u' › ' + l10n.create_new_topic.decode( 'utf-8') if (member): template_values['member'] = member node = GetKindByName('Node', node_name) if node is False: path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'node_not_found.html') output = template.render(path, template_values) return self.response.out.write(output) template_values['node'] = node # section = GetKindByNum('Section', node.section_num) section = Node.all().filter('name =', node.category) template_values['section'] = section if site.use_topic_types: types = site.topic_types.split("\n") options = '<option value="0"> </option>' i = 0 for a_type in types: i = i + 1 detail = a_type.split(':') options = options + '<option value="' + str( i) + '">' + detail[0] + '</option>' tt = '<div class="sep5"></div><table cellpadding="5" cellspacing="0" border="0" width="100%"><tr><td width="60" align="right">Topic Type</td><td width="auto" align="left"><select name="type">' + options + '</select></td></tr></table>' template_values['tt'] = tt else: template_values['tt'] = '' if browser['ios']: if node: path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'new_topic.html') else: path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'node_not_found.html') else: if node: path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'new_topic.html') else: path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'node_not_found.html') output = template.render(path, template_values) self.response.out.write(output) else: self.redirect('/signin')
def get(self, node_name): site = GetSite() browser = detect(self.request) self.session = Session() template_values = {} template_values['site'] = site template_values['rnd'] = random.randrange(1, 100) template_values['system_version'] = SYSTEM_VERSION member = CheckAuth(self) if member: template_values['member'] = member l10n = GetMessages(self, member, site) template_values['l10n'] = l10n node = GetKindByName('Node', node_name) template_values['node'] = node pagination = False pages = 1 page = 1 page_size = 15 start = 0 has_more = False more = 1 has_previous = False previous = 1 if node: template_values['canonical'] = 'http://' + site.domain + '/go/' + node.name if member: favorited = member.hasFavorited(node) template_values['favorited'] = favorited recent_nodes = memcache.get('member::' + str(member.num) + '::recent_nodes') recent_nodes_ids = memcache.get('member::' + str(member.num) + '::recent_nodes_ids') if recent_nodes and recent_nodes_ids: if (node.num in recent_nodes_ids) is not True: recent_nodes.insert(0, node) recent_nodes_ids.insert(0, node.num) memcache.set('member::' + str(member.num) + '::recent_nodes', recent_nodes, 7200) memcache.set('member::' + str(member.num) + '::recent_nodes_ids', recent_nodes_ids, 7200) else: recent_nodes = [] recent_nodes.append(node) recent_nodes_ids = [] recent_nodes_ids.append(node.num) memcache.set('member::' + str(member.num) + '::recent_nodes', recent_nodes, 7200) memcache.set('member::' + str(member.num) + '::recent_nodes_ids', recent_nodes_ids, 7200) template_values['recent_nodes'] = recent_nodes template_values['page_title'] = site.title + u' › ' + node.title # Pagination if node.topics > page_size: pagination = True else: pagination = False if pagination: if node.topics % page_size == 0: pages = int(node.topics / page_size) else: pages = int(node.topics / page_size) + 1 page = self.request.get('p') if (page == '') or (page is None): page = 1 else: page = int(page) if page > pages: page = pages else: if page < 1: page = 1 if page < pages: has_more = True more = page + 1 if page > 1: has_previous = True previous = page - 1 start = (page - 1) * page_size template_values['canonical'] = 'http://' + site.domain + '/go/' + node.name + '?p=' + str(page) else: template_values['page_title'] = site.title + u' › 节点未找到' template_values['pagination'] = pagination template_values['pages'] = pages template_values['page'] = page template_values['page_size'] = page_size template_values['has_more'] = has_more template_values['more'] = more template_values['has_previous'] = has_previous template_values['previous'] = previous section = False if node: # section = GetKindByNum('Section', node.section_num) section = Node.all().filter('name =',node.category) template_values['section'] = section topics = False if node: q3 = db.GqlQuery("SELECT * FROM Topic WHERE node_num = :1 ORDER BY last_touched DESC LIMIT " + str(start) + ", " + str(page_size), node.num) topics = q3 template_values['topics'] = topics if browser['ios']: if (node): path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'node.html') else: path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'node_not_found.html') else: if (node): path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'node.html') else: path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'node_not_found.html') output = template.render(path, template_values) self.response.out.write(output)
def get(self): host = self.request.headers['Host'] site = GetSite() browser = detect(self.request) template_values = {} template_values['site'] = GetSite() template_values['canonical'] = 'http://' + site.domain + '/' template_values['rnd'] = random.randrange(1, 100) template_values['page_title'] = site.title template_values['system_version'] = SYSTEM_VERSION member = CheckAuth(self) l10n = GetMessages(self, member, site) template_values['l10n'] = l10n if member: self.response.headers['Set-Cookie'] = 'auth=' + member.auth + '; expires=' + (datetime.datetime.now() + datetime.timedelta(days=365)).strftime("%a, %d-%b-%Y %H:%M:%S GMT") + '; path=/' template_values['member'] = member try: blocked = pickle.loads(member.blocked.encode('utf-8')) except: blocked = [] if (len(blocked) > 0): template_values['blocked'] = ','.join(map(str, blocked)) if member: recent_nodes = memcache.get('member::' + str(member.num) + '::recent_nodes') if recent_nodes: template_values['recent_nodes'] = recent_nodes nodes_new = [] nodes_new = memcache.get('home_nodes_new') if nodes_new is None: nodes_new = [] qnew = db.GqlQuery("SELECT * FROM Node ORDER BY created DESC LIMIT 10") if (qnew.count() > 0): i = 0 for node in qnew: nodes_new.append(node) i = i + 1 memcache.set('home_nodes_new', nodes_new, 3600) template_values['nodes_new'] = nodes_new if browser['ios']: s = '' s = memcache.get('home_sections_neue') if (s == None): s = '' # q = db.GqlQuery("SELECT * FROM Section ORDER BY created ASC") q = Node.all().filter('caterogy =','') if (q.count() > 0): for section in q: q2 = db.GqlQuery("SELECT * FROM Node WHERE section_num = :1 ORDER BY created ASC", section.num) n = '' if (q2.count() > 0): nodes = [] i = 0 for node in q2: nodes.append(node) i = i + 1 random.shuffle(nodes) for node in nodes: fs = random.randrange(12, 16) n = n + '<a href="/go/' + node.name + '" style="font-size: ' + str(fs) + 'px;">' + node.title + '</a> ' s = s + '<div class="section">' + section.title + '</div><div class="cell">' + n + '</div>' memcache.set('home_sections_neue', s, 600) template_values['s'] = s if browser['ios']: home_rendered = memcache.get('home_rendered_mobile') if home_rendered is None: latest = memcache.get('q_latest_16') if (latest): template_values['latest'] = latest else: q2 = db.GqlQuery("SELECT * FROM Topic ORDER BY last_touched DESC LIMIT 16") memcache.set('q_latest_16', q2, 600) latest = q2 template_values['latest'] = latest path = os.path.join(os.path.dirname(__file__), 'tpl', 'portion', 'home_mobile.html') home_rendered = template.render(path, template_values) memcache.set('home_rendered_mobile', home_rendered, 600) template_values['home'] = home_rendered else: home_rendered = memcache.get('home_rendered') if home_rendered is None: latest = memcache.get('q_latest_16') if (latest): template_values['latest'] = latest else: q2 = db.GqlQuery("SELECT * FROM Topic ORDER BY last_touched DESC LIMIT 16") memcache.set('q_latest_16', q2, 600) latest = q2 template_values['latest'] = latest path = os.path.join(os.path.dirname(__file__), 'tpl', 'portion', 'home.html') home_rendered = template.render(path, template_values) memcache.set('home_rendered', home_rendered, 600) template_values['home'] = home_rendered member_total = memcache.get('member_total') if member_total is None: q3 = db.GqlQuery("SELECT * FROM Counter WHERE name = 'member.total'") if (q3.count() > 0): member_total = q3[0].value else: member_total = 0 memcache.set('member_total', member_total, 600) template_values['member_total'] = member_total topic_total = memcache.get('topic_total') if topic_total is None: q4 = db.GqlQuery("SELECT * FROM Counter WHERE name = 'topic.total'") if (q4.count() > 0): topic_total = q4[0].value else: topic_total = 0 memcache.set('topic_total', topic_total, 600) template_values['topic_total'] = topic_total reply_total = memcache.get('reply_total') if reply_total is None: q5 = db.GqlQuery("SELECT * FROM Counter WHERE name = 'reply.total'") if (q5.count() > 0): reply_total = q5[0].value else: reply_total = 0 memcache.set('reply_total', reply_total, 600) template_values['reply_total'] = reply_total if (browser['ios']): path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'index.html') else: hottest = memcache.get('index_hottest_sidebar') if hottest is None: qhot = db.GqlQuery("SELECT * FROM Node ORDER BY topics DESC LIMIT 25") hottest = u'' for node in qhot: hottest = hottest + '<a href="/go/' + node.name + '" class="item_node">' + node.title + '</a>' memcache.set('index_hottest_sidebar', hottest, 5000) template_values['index_hottest_sidebar'] = hottest c = memcache.get('index_categories') if c is None: c = '' i = 0 categoriesdb = db.GqlQuery("SELECT * FROM Node where category = ''") for cate in categoriesdb: category = cate.name.strip() i = i + 1 if i == categoriesdb.count(): css_class = 'inner' else: css_class = 'cell' c = c + '<div class="' + css_class + '"><table cellpadding="0" cellspacing="0" border="0"><tr><td align="right" width="80"><span class="fade">' + category + '</span></td><td style="line-height: 200%; padding-left: 15px;">' qx = db.GqlQuery("SELECT * FROM Node WHERE category = :1 ORDER BY topics DESC", category) for node in qx: c = c + '<a href="/go/' + node.name + '" style="font-size: 14px;">' + node.title + '</a> ' c = c + '</td></tr></table></div>' memcache.set('index_categories', c, 3600) template_values['c'] = c path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'index.html') output = template.render(path, template_values) self.response.out.write(output)
def post(self, topic_num): site = GetSite() template_values = {} template_values['site'] = site browser = detect(self.request) template_values['system_version'] = SYSTEM_VERSION member = CheckAuth(self) l10n = GetMessages(self, member, site) template_values['l10n'] = l10n if (member): if (member.num == 1): template_values['member'] = member topic = False q = db.GqlQuery("SELECT * FROM Topic WHERE num = :1", int(topic_num)) if (q.count() == 1): topic = q[0] template_values['topic'] = topic if (topic): q2 = db.GqlQuery("SELECT * FROM Node WHERE num = :1", topic.node_num) node = False if (q2.count() == 1): node = q2[0] template_values['node'] = node section = False if node: # q3 = db.GqlQuery("SELECT * FROM Section WHERE num = :1", node.section_num) q3 = Node.all().filter('name =', q2[0].caterogy) if (q3.count() == 1): section = q3[0] template_values['section'] = section errors = 0 # Verification: title topic_title_error = 0 topic_title_error_messages = [ '', u'请输入主题标题', u'主题标题长度不能超过 120 个字符' ] topic_title = self.request.get('title').strip() if (len(topic_title) == 0): errors = errors + 1 topic_title_error = 1 else: if (len(topic_title) > 120): errors = errors + 1 topic_title_error = 2 template_values['topic_title'] = topic_title template_values['topic_title_error'] = topic_title_error template_values[ 'topic_title_error_message'] = topic_title_error_messages[ topic_title_error] # Verification: content topic_content_error = 0 topic_content_error_messages = [ '', u'请输入主题内容', u'主题内容长度不能超过 5000 个字符' ] topic_content = self.request.get('content').strip() if (len(topic_content) == 0): errors = errors + 1 topic_content_error = 1 else: if (len(topic_content) > 5000): errors = errors + 1 topic_content_error = 2 template_values['topic_content'] = topic_content template_values[ 'topic_content_error'] = topic_content_error template_values[ 'topic_content_error_message'] = topic_content_error_messages[ topic_content_error] template_values['errors'] = errors if (errors == 0): topic.title = topic_title topic.content = topic_content path = os.path.join(os.path.dirname(__file__), 'tpl', 'portion', 'topic_content.html') output = template.render(path, {'topic': topic}) topic.content_rendered = output.decode('utf-8') topic.last_touched = datetime.datetime.now() topic.put() memcache.delete('Topic_' + str(topic.num)) self.redirect('/t/' + str(topic.num)) else: if browser['ios']: path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'edit_topic.html') else: path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'edit_topic.html') output = template.render(path, template_values) self.response.out.write(output) else: path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'topic_not_found.html') output = template.render(path, template_values) self.response.out.write(output) else: self.redirect('/t/' + str(topic_num)) else: self.redirect('/signin')
def post(self, topic_num): site = GetSite() browser = detect(self.request) template_values = {} template_values['site'] = site template_values['system_version'] = SYSTEM_VERSION member = CheckAuth(self) template_values['member'] = member l10n = GetMessages(self, member, site) template_values['l10n'] = l10n topic_num_str = str(topic_num) if len(topic_num_str) > 8: if browser['ios']: path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'topic_not_found.html') else: path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'topic_not_found.html') output = template.render(path, template_values) self.response.out.write(output) return if (member): topic = False q = db.GqlQuery("SELECT * FROM Topic WHERE num = :1", int(topic_num)) if (q.count() == 1): topic = q[0] try: topic.hits = topic.hits + 1 topic.put() except: topic.hits = topic.hits - 1 template_values['topic'] = topic errors = 0 # Verification: content reply_content_error = 0 reply_content_error_messages = [ '', u'请输入回复内容', u'回复内容长度不能超过 2000 个字符' ] reply_content = self.request.get('content').strip() if (len(reply_content) == 0): errors = errors + 1 reply_content_error = 1 else: if (len(reply_content) > 2000): errors = errors + 1 reply_content_error = 2 template_values['reply_content'] = reply_content template_values['reply_content_error'] = reply_content_error template_values[ 'reply_content_error_message'] = reply_content_error_messages[ reply_content_error] template_values['errors'] = errors if (topic and (errors == 0)): reply = Reply(parent=topic) q = db.GqlQuery('SELECT * FROM Counter WHERE name = :1', 'reply.max') if (q.count() == 1): counter = q[0] counter.value = counter.value + 1 else: counter = Counter() counter.name = 'reply.max' counter.value = 1 q2 = db.GqlQuery('SELECT * FROM Counter WHERE name = :1', 'reply.total') if (q2.count() == 1): counter2 = q2[0] counter2.value = counter2.value + 1 else: counter2 = Counter() counter2.name = 'reply.total' counter2.value = 1 node = False section = False if topic: q3 = db.GqlQuery("SELECT * FROM Node WHERE num = :1", topic.node_num) node = q3[0] # q4 = db.GqlQuery("SELECT * FROM Section WHERE num = :1", node.section_num) q4 = Node.all().filter('name =', q3[0].caterogy) section = q4[0] reply.num = counter.value reply.content = reply_content reply.topic = topic reply.topic_num = topic.num reply.member = member reply.member_num = member.num reply.created_by = member.username topic.replies = topic.replies + 1 topic.node_name = node.name topic.node_title = node.title topic.last_reply_by = member.username topic.last_touched = datetime.datetime.now() ua = self.request.headers['User-Agent'] if (re.findall('Mozilla\/5.0 \(iPhone', ua)): reply.source = 'iPhone' if (re.findall('Mozilla\/5.0 \(iPod', ua)): reply.source = 'iPod' if (re.findall('Mozilla\/5.0 \(iPad', ua)): reply.source = 'iPad' if (re.findall('Android', ua)): reply.source = 'Android' if (re.findall('Mozilla\/5.0 \(PLAYSTATION 3;', ua)): reply.source = 'PS3' reply.put() topic.put() counter.put() counter2.put() memcache.set('Topic_' + str(topic.num), topic, 86400) memcache.delete('topic_' + str(topic.num) + '_replies_desc_compressed') memcache.delete('topic_' + str(topic.num) + '_replies_asc_compressed') memcache.delete('topic_' + str(topic.num) + '_replies_filtered_compressed') memcache.delete('topic_' + str(topic.num) + '_replies_desc_rendered') memcache.delete('topic_' + str(topic.num) + '_replies_asc_rendered') memcache.delete('topic_' + str(topic.num) + '_replies_filtered_rendered') memcache.delete('topic_' + str(topic.num) + '_replies_desc_rendered_mobile') memcache.delete('topic_' + str(topic.num) + '_replies_asc_rendered_mobile') memcache.delete('topic_' + str(topic.num) + '_replies_filtered_rendered_mobile') memcache.delete('member::' + str(member.num) + '::participated') memcache.delete('home_rendered') memcache.delete('home_rendered_mobile') taskqueue.add(url='/index/topic/' + str(topic.num)) # Twitter Sync if member.twitter_oauth == 1 and member.twitter_sync == 1: access_token = OAuthToken.from_string( member.twitter_oauth_string) twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, access_token) link = 'http://' + self.request.headers[ 'Host'] + '/t/' + str(topic.num) + '#r' + str( reply.num) link_length = len(link) reply_content_length = len(reply.content) available = 140 - link_length - 1 if available > reply_content_length: status = reply.content + ' ' + link else: status = reply.content[0:(available - 4)] + '... ' + link self.response.out.write('Status: ' + status) logging.error('Status: ' + status) try: twitter.PostUpdate(status.encode('utf-8')) except: logging.error("Failed to sync to Twitter for Reply #" + str(reply.num)) self.redirect('/t/' + str(topic.num) + '#reply' + str(topic.replies)) else: node = False section = False if topic: q2 = db.GqlQuery("SELECT * FROM Node WHERE num = :1", topic.node_num) node = q2[0] # q3 = db.GqlQuery("SELECT * FROM Section WHERE num = :1", node.section_num) q3 = Node.all().filter('name =', q2[0].caterogy) section = q3[0] template_values['node'] = node template_values['section'] = section if browser['ios']: path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'topic.html') else: path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'topic.html') output = template.render(path, template_values) self.response.out.write(output) else: self.redirect('/signin')
def get(self, topic_num): site = GetSite() browser = detect(self.request) template_values = {} template_values['site'] = site template_values['rnd'] = random.randrange(1, 100) reply_reversed = self.request.get('r') if reply_reversed == '1': reply_reversed = True else: reply_reversed = False filter_mode = self.request.get('f') if filter_mode == '1': filter_mode = True else: filter_mode = False template_values['reply_reversed'] = reply_reversed template_values['filter_mode'] = filter_mode template_values['system_version'] = SYSTEM_VERSION errors = 0 template_values['errors'] = errors member = CheckAuth(self) template_values['member'] = member l10n = GetMessages(self, member, site) template_values['l10n'] = l10n if member is not False: try: blocked = pickle.loads(member.blocked.encode('utf-8')) except: blocked = [] if (len(blocked) > 0): template_values['blocked'] = ','.join(map(str, blocked)) if member.num == 1: template_values['is_admin'] = 1 else: template_values['is_admin'] = 0 topic_num_str = str(topic_num) if len(topic_num_str) > 8: if browser['ios']: path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'topic_not_found.html') else: path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'topic_not_found.html') output = template.render(path, template_values) self.response.out.write(output) return topic = False topic = memcache.get('Topic_' + str(topic_num)) if topic is None: q = db.GqlQuery("SELECT * FROM Topic WHERE num = :1", int(topic_num)) if (q.count() == 1): topic = q[0] memcache.set('Topic_' + str(topic_num), topic, 86400) if topic: taskqueue.add(url='/hit/topic/' + str(topic.key())) template_values['page_title'] = site.title + u' › ' + topic.title template_values[ 'canonical'] = 'http://' + site.domain + '/t/' + str(topic.num) if topic.content_rendered is None: path = os.path.join(os.path.dirname(__file__), 'tpl', 'portion', 'topic_content.html') output = template.render(path, {'topic': topic}) topic = db.get(topic.key()) topic.content_rendered = output.decode('utf-8') memcache.delete('Topic_' + str(topic.num)) topic.put() else: template_values['page_title'] = site.title + u' › 主题未找到' template_values['topic'] = topic if member: if member.num == 1: template_values['can_edit'] = True else: template_values['can_edit'] = False if (topic): node = False section = False node = GetKindByNum('Node', topic.node_num) if (node): # section = GetKindByNum('Section', node.section_num) section = Node.all().filter('name =', node.category) template_values['node'] = node template_values['section'] = section replies = False if browser['ios']: path = os.path.join(os.path.dirname(__file__), 'tpl', 'portion', 'topic_replies_mobile.html') else: path = os.path.join(os.path.dirname(__file__), 'tpl', 'portion', 'topic_replies.html') if filter_mode: if browser['ios']: r_tag = 'topic_' + str( topic.num) + '_replies_filtered_rendered' else: r_tag = 'topic_' + str( topic.num) + '_replies_filtered_rendered_mobile' r = memcache.get(r_tag) if r is None: replies = memcache.get('topic_' + str(topic.num) + '_replies_filtered_compressed') if replies is None: q5 = db.GqlQuery( "SELECT * FROM Reply WHERE topic_num = :1 AND member_num = :2 ORDER BY created ASC", topic.num, topic.member.num) replies = q5 memcache.set( 'topic_' + str(topic.num) + '_replies_filtered_compressed', GetPacked(replies), 7200) else: replies = GetUnpacked(replies) template_values['replies'] = replies r = template.render(path, template_values) memcache.set(r_tag, r, 86400) else: if reply_reversed: if browser['ios']: r_tag = 'topic_' + str( topic.num) + '_replies_desc_rendered' else: r_tag = 'topic_' + str( topic.num) + '_replies_desc_rendered_mobile' r = memcache.get(r_tag) if r is None: replies = memcache.get('topic_' + str(topic.num) + '_replies_desc_compressed') if replies is None: q4 = db.GqlQuery( "SELECT * FROM Reply WHERE topic_num = :1 ORDER BY created DESC", topic.num) replies = q4 memcache.set( 'topic_' + str(topic.num) + '_replies_desc_compressed', GetPacked(q4), 86400) else: replies = GetUnpacked(replies) template_values['replies'] = replies r = template.render(path, template_values) memcache.set(r_tag, r, 86400) else: if browser['ios']: r_tag = 'topic_' + str( topic.num) + '_replies_asc_rendered' else: r_tag = 'topic_' + str( topic.num) + '_replies_asc_rendered_mobile' r = memcache.get(r_tag) if r is None: replies = memcache.get('topic_' + str(topic.num) + '_replies_asc_compressed') if replies is None: q4 = db.GqlQuery( "SELECT * FROM Reply WHERE topic_num = :1 ORDER BY created ASC", topic.num) replies = q4 memcache.set( 'topic_' + str(topic.num) + '_replies_asc_compressed', GetPacked(q4), 86400) else: replies = GetUnpacked(replies) template_values['replies'] = replies r = template.render(path, template_values) memcache.set(r_tag, r, 86400) template_values['r'] = r if topic and member: if member.hasFavorited(topic): template_values['favorited'] = True else: template_values['favorited'] = False if browser['ios']: path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'topic.html') else: path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'topic.html') else: if browser['ios']: path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'topic_not_found.html') else: path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'topic_not_found.html') output = template.render(path, template_values) self.response.out.write(output)
def post(self, node_name): site = GetSite() browser = detect(self.request) template_values = {} template_values['site'] = site template_values['system_version'] = SYSTEM_VERSION member = CheckAuth(self) l10n = GetMessages(self, member, site) template_values['l10n'] = l10n template_values[ 'page_title'] = site.title + u' › ' + l10n.create_new_topic.decode( 'utf-8') if (member): template_values['member'] = member q = db.GqlQuery("SELECT * FROM Node WHERE name = :1", node_name) node = False if (q.count() == 1): node = q[0] template_values['node'] = node section = False if node: # q2 = db.GqlQuery("SELECT * FROM Section WHERE num = :1", node.section_num) q2 = Node.all().filter('name =', node.category) if (q2.count() == 1): section = q2[0] template_values['section'] = section errors = 0 # Verification: title topic_title_error = 0 topic_title_error_messages = [ '', u'请输入主题标题', u'主题标题长度不能超过 120 个字符' ] topic_title = self.request.get('title').strip() if (len(topic_title) == 0): errors = errors + 1 topic_title_error = 1 else: if (len(topic_title) > 120): errors = errors + 1 topic_title_error = 2 template_values['topic_title'] = topic_title template_values['topic_title_error'] = topic_title_error template_values[ 'topic_title_error_message'] = topic_title_error_messages[ topic_title_error] # Verification: content topic_content_error = 0 topic_content_error_messages = [ '', u'请输入主题内容', u'主题内容长度不能超过 9999 个字符' ] topic_content = self.request.get('content').strip() if (len(topic_content) == 0): errors = errors + 1 topic_content_error = 1 else: if (len(topic_content) > 9999): errors = errors + 1 topic_content_error = 2 template_values['topic_content'] = topic_content template_values['topic_content_error'] = topic_content_error template_values[ 'topic_content_error_message'] = topic_content_error_messages[ topic_content_error] # Verification: type if site.use_topic_types: types = site.topic_types.split("\n") if len(types) > 0: topic_type = self.request.get('type').strip() try: topic_type = int(topic_type) if topic_type < 0: topic_type = 0 if topic_type > len(types): topic_type = 0 if topic_type > 0: detail = types[topic_type - 1].split(':') topic_type_label = detail[0] topic_type_color = detail[1] except: topic_type = 0 else: topic_type = 0 options = '<option value="0"> </option>' i = 0 for a_type in types: i = i + 1 detail = a_type.split(':') if topic_type == i: options = options + '<option value="' + str( i ) + '" selected="selected">' + detail[0] + '</option>' else: options = options + '<option value="' + str( i) + '">' + detail[0] + '</option>' tt = '<div class="sep5"></div><table cellpadding="5" cellspacing="0" border="0" width="100%"><tr><td width="60" align="right">Topic Type</td><td width="auto" align="left"><select name="type">' + options + '</select></td></tr></table>' template_values['tt'] = tt else: template_values['tt'] = '' template_values['errors'] = errors if (errors == 0): topic = Topic(parent=node) q = db.GqlQuery('SELECT * FROM Counter WHERE name = :1', 'topic.max') if (q.count() == 1): counter = q[0] counter.value = counter.value + 1 else: counter = Counter() counter.name = 'topic.max' counter.value = 1 q2 = db.GqlQuery('SELECT * FROM Counter WHERE name = :1', 'topic.total') if (q2.count() == 1): counter2 = q2[0] counter2.value = counter2.value + 1 else: counter2 = Counter() counter2.name = 'topic.total' counter2.value = 1 topic.num = counter.value topic.title = topic_title topic.content = topic_content path = os.path.join(os.path.dirname(__file__), 'tpl', 'portion', 'topic_content.html') output = template.render(path, {'topic': topic}) topic.content_rendered = output.decode('utf-8') topic.node = node topic.node_num = node.num topic.node_name = node.name topic.node_title = node.title topic.created_by = member.username topic.member = member topic.member_num = member.num topic.last_touched = datetime.datetime.now() ua = self.request.headers['User-Agent'] if (re.findall('Mozilla\/5.0 \(iPhone;', ua)): topic.source = 'iPhone' if (re.findall('Mozilla\/5.0 \(iPod;', ua)): topic.source = 'iPod' if (re.findall('Mozilla\/5.0 \(iPad;', ua)): topic.source = 'iPad' if (re.findall('Android', ua)): topic.source = 'Android' if (re.findall('Mozilla\/5.0 \(PLAYSTATION 3;', ua)): topic.source = 'PS3' if site.use_topic_types: if topic_type > 0: topic.type = topic_type_label topic.type_color = topic_type_color node.topics = node.topics + 1 node.put() topic.put() counter.put() counter2.put() memcache.delete('feed_index') memcache.delete('Node_' + str(topic.node_num)) memcache.delete('Node::' + str(node.name)) memcache.delete('home_rendered') memcache.delete('home_rendered_mobile') taskqueue.add(url='/index/topic/' + str(topic.num)) # Twitter Sync if member.twitter_oauth == 1 and member.twitter_sync == 1: access_token = OAuthToken.from_string( member.twitter_oauth_string) twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, access_token) status = topic.title + ' #' + topic.node.name + ' http://' + self.request.headers[ 'Host'] + '/t/' + str(topic.num) try: twitter.PostUpdate(status.encode('utf-8')) except: logging.error("Failed to sync to Twitter for Topic #" + str(topic.num)) self.redirect('/t/' + str(topic.num) + '#reply0') else: if browser['ios']: path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'new_topic.html') else: path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'new_topic.html') output = template.render(path, template_values) self.response.out.write(output) else: self.redirect('/signin')