Esempio n. 1
0
 def get(self):
     output = memcache.get('feed_read_output')
     if output is None:
         self.values['site_domain'] = self.site.domain
         self.values['site_name'] = self.site.title
         self.values['site_slogan'] = self.site.slogan
         self.values['feed_url'] = 'http://' + self.values[
             'site_domain'] + '/read.xml'
         self.values['site_updated'] = datetime.datetime.now(
             pytz.timezone('Asia/Shanghai'))
         topics = memcache.get('feed_home')
         if topics is None:
             #q = db.GqlQuery("SELECT * FROM Topic ORDER BY created DESC LIMIT 10")
             q = Topic.select(orderBy='-created').limit(10)
             topics = []
             IGNORED = [
                 'newbie', 'in', 'flamewar', 'pointless', 'tuan', '528491',
                 'chamber', 'autistic', 'blog', 'love', 'flood'
             ]
             for topic in q:
                 if topic.node.name not in IGNORED:
                     topics.append(topic)
             memcache.set('feed_home', topics, 3600)
         self.values['topics'] = topics
         self.values['feed_title'] = self.site.title
         path = os.path.join(os.path.dirname(__file__), 'tpl', 'feed')
         t = self.get_template(path, 'read.xml')
         output = t.render(self.values)
         memcache.set('feed_read_output', output, 3600)
     self.set_header('Content-type', 'application/xml;charset=UTF-8')
     self.write(output)
Esempio n. 2
0
 def get(self, topic_key):
     topic = Topic.get(topic_key)
     if topic:
         topic.stars = topic.stars - 1
         topic.sync()
         store.commit()  #jon add
         memcache.set('Topic_' + str(topic.num), topic, 86400)
Esempio n. 3
0
 def get(self, topic_key):
     topic = Topic.get(topic_key)
     if topic:
         topic.stars = topic.stars - 1
         topic.sync()
         store.commit()  #jon add
         memcache.set('Topic_' + str(topic.num), topic, 86400)
Esempio n. 4
0
 def get(self, node_name):
     node_name = node_name.lower()
     site = GetSite()
     node = GetKindByName('Node', node_name)
     if node is False:
         return self.write('node not found')
     output = memcache.get('feed_node_' + node_name)
     if output is None:
         template_values = {}
         template_values['site'] = site
         template_values['site_domain'] = site.domain
         template_values['site_name'] = site.title
         template_values['site_slogan'] = site.slogan
         template_values['feed_url'] = 'http://' + template_values[
             'site_domain'] + '/index.xml'
         template_values['site_updated'] = datetime.datetime.now(
             pytz.timezone('Asia/Shanghai'))
         #q = db.GqlQuery("SELECT * FROM Topic WHERE node = :1 ORDER BY created DESC LIMIT 10", node)
         q = Topic.selectBy(node=node).orderBy('-created').limit(10)
         topics = []
         for topic in q:
             topics.append(topic)
         template_values['topics'] = topics
         template_values['feed_title'] = site.title + u' › ' + node.title
         path = os.path.join(os.path.dirname(__file__), 'tpl', 'feed')
         t = self.get_template(template_values)
         output = t.render(self.values)
         memcache.set('feed_node_' + node.name, output, 7200)
     self.set_header('Content-type', 'application/xml;charset=UTF-8')
     self.write(output)
Esempio n. 5
0
 def get(self):
     output = memcache.get('feed_read_output')
     if output is None:
         self.values['site_domain'] = self.site.domain
         self.values['site_name'] = self.site.title
         self.values['site_slogan'] = self.site.slogan
         self.values['feed_url'] = 'http://' + self.values['site_domain'] + '/read.xml'
         self.values['site_updated'] = datetime.datetime.now(pytz.timezone('Asia/Shanghai'))
         topics = memcache.get('feed_home')
         if topics is None:
             #q = db.GqlQuery("SELECT * FROM Topic ORDER BY created DESC LIMIT 10")
             q = Topic.select(orderBy='-created').limit(10)
             topics = []
             IGNORED = ['newbie', 'in', 'flamewar', 'pointless', 'tuan', '528491', 'chamber', 'autistic', 'blog', 'love', 'flood']
             for topic in q:
                 if topic.node.name not in IGNORED:
                     topics.append(topic)
             memcache.set('feed_home', topics, 3600)
         self.values['topics'] = topics
         self.values['feed_title'] = self.site.title
         path = os.path.join(os.path.dirname(__file__), 'tpl', 'feed')
         t=self.get_template(path, 'read.xml')
         output = t.render(self.values)
         memcache.set('feed_read_output', output, 3600)
     self.set_header('Content-type', 'application/xml;charset=UTF-8')
     self.write(output)
Esempio n. 6
0
 def get(self):
     member = CheckAuth(self)
     if member:
         site = GetSite()
         l10n = GetMessages(self, member, site)
         template_values = {}
         template_values['site'] = site
         template_values['member'] = member
         template_values['l10n'] = l10n
         template_values['page_title'] = site.title + u' › 我的特别关注'
         template_values['rnd'] = random.randrange(1, 100)
         if member.favorited_members > 0:
             template_values['has_following'] = True
             #q = db.GqlQuery("SELECT * FROM MemberBookmark WHERE member_num = :1 ORDER BY created DESC", member.num)
             q = MemberBookmark.selectBy(member_num=member.num).orderBy('-created')
             template_values['following'] = q
             following = []
             for bookmark in q:
                 following.append(bookmark.one.num)
             #q2 = db.GqlQuery("SELECT * FROM Topic WHERE member_num IN :1 ORDER BY created DESC LIMIT 20", following)
             q2 = Topic.selectBy(member_num=following).orderBy('-created').limit(20)
             template_values['latest'] = q2
         else:
             template_values['has_following'] = False
         path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop')
         t=self.get_template(path,'my_following.html')
         self.finish(t.render(template_values))
     else:
         self.redirect('/')
Esempio n. 7
0
 def get(self, node_name):
     node_name = node_name.lower()
     site = GetSite()
     node = GetKindByName('Node', node_name)
     if node is False:
         return self.write('node not found')
     output = memcache.get('feed_node_' + node_name)
     if output is None:
         template_values = {}
         template_values['site'] = site
         template_values['site_domain'] = site.domain
         template_values['site_name'] = site.title
         template_values['site_slogan'] = site.slogan
         template_values['feed_url'] = 'http://' + template_values['site_domain'] + '/index.xml'
         template_values['site_updated'] = datetime.datetime.now(pytz.timezone('Asia/Shanghai'))
         #q = db.GqlQuery("SELECT * FROM Topic WHERE node = :1 ORDER BY created DESC LIMIT 10", node)
         q = Topic.selectBy(node=node).orderBy('-created').limit(10)
         topics = []
         for topic in q:
             topics.append(topic)
         template_values['topics'] = topics
         template_values['feed_title'] = site.title + u' › ' + node.title
         path = os.path.join(os.path.dirname(__file__), 'tpl', 'feed')
         t=self.get_template(template_values)
         output = t.render(self.values)
         memcache.set('feed_node_' + node.name, output, 7200)
     self.set_header('Content-type', 'application/xml;charset=UTF-8')
     self.write(output)
Esempio n. 8
0
    def get(self, topic_key):
        topic = Topic.get(topic_key)
        combined = topic.title + " " + topic.content
        ms = re.findall('(@[a-zA-Z0-9\_]+\.?)\s?', combined)
        unique = []
        for m in ms:
            if m.lower() not in unique:
                unique.append(m.lower())
        keys = []
        if (len(unique) > 0):
            for m in unique:
                m_id = re.findall('@([a-zA-Z0-9\_]+\.?)', m)
                if (len(m_id) > 0):
                    if (m_id[0].endswith('.') != True):
                        member_username = m_id[0]
                        member = GetMemberByUsername(member_username)
                        if member:
                            if member.id != topic.member.id and member.id not in keys:
                                #q = db.GqlQuery('SELECT * FROM Counter WHERE name = :1', 'notification.max')
                                q = Counter.selectBy(name='notification.max')
                                if (q.count() == 1):
                                    counter = q[0]
                                    counter.value = counter.value + 1
                                else:
                                    counter = Counter()
                                    counter.name = 'notification.max'
                                    counter.value = 1
                                #q2 = db.GqlQuery('SELECT * FROM Counter WHERE name = :1', 'notification.total')
                                q2 = Counter.selectBy(
                                    name='notification.total')
                                if (q2.count() == 1):
                                    counter2 = q2[0]
                                    counter2.value = counter2.value + 1
                                else:
                                    counter2 = Counter()
                                    counter2.name = 'notification.total'
                                    counter2.value = 1

                                notification = Notification(member=member)
                                notification.num = counter.value
                                notification.type = 'mention_topic'
                                notification.payload = topic.content
                                notification.label1 = topic.title
                                notification.link1 = '/t/' + str(
                                    topic.num) + '#reply' + str(topic.replies)
                                notification.member = topic.member
                                notification.for_member_num = member.num

                                keys.append(str(member.id))

                                counter.sync()
                                counter2.sync()
                                notification.sync()
                                store.commit()  #jon add
        for k in keys:
            taskqueue.add(url='/notifications/check/' + k)
Esempio n. 9
0
    def get(self, topic_key):
        topic = Topic.get(topic_key)
        combined = topic.title + " " + topic.content
        ms = re.findall('(@[a-zA-Z0-9\_]+\.?)\s?', combined)
        unique = []
        for m in ms:
            if m.lower() not in unique:
                unique.append(m.lower())
        keys = []
        if (len(unique) > 0):
            for m in unique:
                m_id = re.findall('@([a-zA-Z0-9\_]+\.?)', m)
                if (len(m_id) > 0):
                    if (m_id[0].endswith('.') != True):
                        member_username = m_id[0]
                        member = GetMemberByUsername(member_username)
                        if member:
                            if member.id != topic.member.id and member.id not in keys:
                                #q = db.GqlQuery('SELECT * FROM Counter WHERE name = :1', 'notification.max')
                                q = Counter.selectBy(name='notification.max')
                                if (q.count() == 1):
                                    counter = q[0]
                                    counter.value = counter.value + 1
                                else:
                                    counter = Counter()
                                    counter.name = 'notification.max'
                                    counter.value = 1
                                #q2 = db.GqlQuery('SELECT * FROM Counter WHERE name = :1', 'notification.total')
                                q2 = Counter.selectBy(name='notification.total')
                                if (q2.count() == 1):
                                    counter2 = q2[0]
                                    counter2.value = counter2.value + 1
                                else:
                                    counter2 = Counter()
                                    counter2.name = 'notification.total'
                                    counter2.value = 1

                                notification = Notification(member=member)
                                notification.num = counter.value
                                notification.type = 'mention_topic'
                                notification.payload = topic.content
                                notification.label1 = topic.title
                                notification.link1 = '/t/' + str(topic.num) + '#reply' + str(topic.replies)
                                notification.member = topic.member
                                notification.for_member_num = member.num

                                keys.append(str(member.id))

                                counter.sync()
                                counter2.sync()
                                notification.sync()
                                store.commit()  #jon add
        for k in keys:
            taskqueue.add(url='/notifications/check/' + k)
Esempio n. 10
0
 def get(self):
     site = GetSite()
     template_values = {}
     template_values['site'] = site
     topics = memcache.get('api_topics_latest')
     if topics is None:
         #topics = db.GqlQuery("SELECT * FROM Topic ORDER BY created DESC LIMIT 20")
         topics = Topic.selectBy(orderBy='-created').limit(20)
         memcache.set('api_topics_latest', topics, 120)
     template_values['topics'] = topics
     template_values['topics_count'] = topics.count()
     path = os.path.join(os.path.dirname(__file__), 'tpl', 'api')
     t = self.get_template(path, 'topics_latest.json')
     self.finish(t.render(template_values))
Esempio n. 11
0
File: topic.py Progetto: shykai/v2ex
 def post(self, node_name):
     site = GetSite()
     browser = detect(self.request)
     template_values = {}
     template_values['site'] = site
     template_values['page_title'] = site.title + u' › 创建新主题'
     template_values['system_version'] = SYSTEM_VERSION
     member = CheckAuth(self)
     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)
             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'主题内容长度不能超过 2000 个字符'
         ]
         topic_content = self.request.get('content').strip()
         if (len(topic_content) == 0):
             errors = errors + 1
             topic_content_error = 1
         else:
             if (len(topic_content) > 2000):
                 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 = 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'            
             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 + ' 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')
Esempio n. 12
0
 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">&nbsp;&nbsp;&nbsp;&nbsp;</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')
Esempio n. 13
0
 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)
             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"主题内容长度不能超过 2000 个字符"]
         topic_content = self.request.get("content").strip()
         if len(topic_content) == 0:
             errors = errors + 1
             topic_content_error = 1
         else:
             if len(topic_content) > 2000:
                 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">&nbsp;&nbsp;&nbsp;&nbsp;</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")
Esempio n. 14
0
 def get(self):
     site = GetSite()
     template_values = {}
     template_values['site'] = site
     method_determined = False
     parameter_id = self.request.arguments['id'][0]
     parameter_username = False
     parameter_node_id = False
     parameter_node_name = False
     if parameter_id:
         method_determined = True
     if method_determined is False:
         parameter_username = self.request.arguments['username'][0]
         if parameter_username:
             method_determined = True
     if method_determined is False:
         parameter_node_id = self.request.arguments['node_id'][0]
         if parameter_node_id:
             method_determined = True
     if method_determined is False:
         parameter_node_name = self.request.arguments['node_name'][0]
         if parameter_node_name:
             method_determined = True
     if method_determined is False:
         template_values[
             'message'] = "Required parameter id, username, node_id or node_name is missing"
         path = os.path.join(os.path.dirname(__file__), 'tpl', 'api')
         self.set_status(400, 'Bad Request')
         t = self.get_template(path, 'error.json')
         self.finish(t.render(template_values))
     else:
         topics = False
         topic = False
         if parameter_id:
             try:
                 topic = GetKindByNum('Topic', int(parameter_id))
                 if topic is not False:
                     topics = []
                     topics.append(topic)
                     template_values['topic'] = topic
             except:
                 topics = False
         if topics is False:
             if parameter_username:
                 one = GetMemberByUsername(parameter_username)
                 if one is not False:
                     #topics = db.GqlQuery("SELECT * FROM Topic WHERE member_num = :1 ORDER BY created DESC LIMIT 20", one.num)
                     topics = Topic.selectBy(
                         member_num=one.num).orderBy('-created').limit(20)
                     template_values['topics'] = topics
         if topics is False:
             try:
                 if parameter_node_id:
                     node = GetKindByNum('Node', int(parameter_node_id))
                     if node is not False:
                         #topics = db.GqlQuery("SELECT * FROM Topic WHERE node_num = :1 ORDER BY last_touched DESC LIMIT 20", node.num)
                         topics = Topic.selectBy(node_num=one.num).orderBy(
                             '-last_touched').limit(20)
                         template_values['topics'] = topics
             except:
                 topics = False
         if topics is False:
             if parameter_node_name:
                 node = GetKindByName('Node', str(parameter_node_name))
                 if node is not False:
                     #topics = db.GqlQuery("SELECT * FROM Topic WHERE node_num = :1 ORDER BY last_touched DESC LIMIT 20", node.num)
                     topics = Topic.selectBy(node_num=node.num).orderBy(
                         '-last_touched').limit(20)
                     template_values['topics'] = topics
         if topic or topics:
             path = os.path.join(os.path.dirname(__file__), 'tpl', 'api')
             t = self.get_template(path, 'topics_show.json')
             self.finish(t.render(template_values))
         else:
             template_values['message'] = "Failed to get topics"
             path = os.path.join(os.path.dirname(__file__), 'tpl', 'api')
             self.set_status(400, 'Bad Request')
             t = self.get_template(path, 'error.json')
             self.finish(t.render(template_values))
Esempio n. 15
0
File: topic.py Progetto: gostop/v2ex
 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
     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)
             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'主题内容长度不能超过 2000 个字符'
         ]
         topic_content = self.request.get('content').strip()
         if (len(topic_content) == 0):
             errors = errors + 1
             topic_content_error = 1
         else:
             if (len(topic_content) > 2000):
                 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">&nbsp;&nbsp;&nbsp;&nbsp;</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 + ' 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')