def test_string(self): Counter.reset_counter() bk = Book('AAAA', 'BBBB', True) self.assertIn('1', str(bk)) # ID should be 1 self.assertIn('AAAA', str(bk)) self.assertIn('BBBB', str(bk)) self.assertIn('You have read', str(bk))
def test_add_book(self, mock_print, mock_input): main.main() # reset counter and make book that mimics the one expected to be created Counter.reset_counter() expected_book = Book('Title', 'Author', False) all_books = self.store.get_all_books() self.assertEqual(expected_book, all_books[0])
def wrapper(self, *args, **kwargs): counter = Counter.all().get() if counter is None: counter = Counter() counter.put() if not users.is_current_user_admin(): counter.count += 1 counter.put() return handler_method(self, *args, **kwargs)
def test_create_book_id_increases(self): Counter.reset_counter() bk = Book('Title', 'Author') self.assertEqual(1, bk.id) bk2 = Book('Title2', 'Author2') self.assertEqual(2, bk2.id) bk3 = Book('Title3', 'Author3') self.assertEqual(3, bk3.id)
def txn(key_name, amount): counter = Counter.get_by_key_name(key_name) if not counter: counter = Counter(key_name=key_name, count=0) counter.count += amount counter.put() return counter.count
def get(self): self.echo('home.html', { 'title': "首页", 'topic_objs': KeyStrValue.get_topic_objs('recent-topic-home'), 'site_counts': Counter.get_site_counts(), 'newest_node': Node.get_newest(), 'recent_node': Node.get_recent_node(), 'hot_node': Node.get_hot_node(), 'reply_topic_objs': KeyStrValue.get_topic_key_title('recent-reply-topic'), }, layout='_layout.html')
def votes_constructor(): votes = Votes(president=Counter(), gobernor=Counter(), diputado=Counter(), senador=Counter(), intendente=Counter(), general=Counter()) votes.save() return votes
def get_vars(self): # Round off the count so people don't expect it to change every time # they add a record. person_count = Counter.get_count(self.repo, 'person.all') if person_count < 100: num_people = 0 # No approximate count will be displayed. else: # 100, 200, 300, etc. num_people = int(round(person_count, -2)) return { 'num_people': num_people, 'seek_url': self.get_url('/query', role='seek'), 'provide_url': self.get_url('/query', role='provide'), 'facebook_locale': const.FACEBOOK_LOCALES.get(self.env.lang, 'en_US'), }
def post(self): req_user = self.request.user gu_obj = GoogleUser.get_or_insert(req_user.user_id()) if gu_obj.name: self.redirect('/') return errors = [] name = self.POST['name'].strip().lower() if name: if len(name)<20: if re.search('^[a-zA-Z0-9]+$', name): check_obj = Member.get_by_key_name(str(name)) if check_obj: errors.append('该用户名已被注册,请换一个吧') else: #get member id mid_obj = Counter.get_or_insert('member_auto_increment',name = 'member_auto_increment', value = 1) nuser_obj = Member(key_name=name, id = mid_obj.value, flag = 1, add = int(time())) nuser_obj.put() if nuser_obj.is_saved(): #set google user gu_obj.name = name db.run_in_transaction(obj_runput,gu_obj) #all member num +1 mid_obj.value += 1 db.run_in_transaction(obj_runput,mid_obj) self.redirect('/setavatar') return else: errors.append('服务器出现意外错误,请稍后再试') else: errors.append('用户名只能包含字母和数字') else: errors.append('用户名太长了') else: errors.append('用户名必填') self.echo('setname.html', { 'title': "设置名字", 'errors':errors, 'name':name, 'newest_node': Node.get_newest(), }, layout='_layout.html')
def load_votes_matrix(box): parties = [] parties_matrix = zip(PARTIES, VOTES_MATRIX) for party, flags in dict(parties_matrix).items(): kwargs = {} for i, flag in enumerate(flags): counter = Counter(enabled=bool(flag)) kwargs[CANDIDATES[i]] = counter _votes = Votes(**kwargs) _votes.save() _party = Party(name=party, votes=_votes) _party.save() parties.append(_party) box.parties = parties box.save() other_votes = OtherVotes(blank=votes_constructor(), nulled=votes_constructor(), recurrent=votes_constructor(), refuted=votes_constructor()).save() box.other_votes = other_votes box.save()
def post(self): if self.cur_user and self.cur_user.flag == 99: n_id = self.request.get('id') name = self.POST['name'] imgurl = self.POST['imgurl'] about = self.POST['about'] if name: if n_id: n_obj = Node.get_by_id(int(n_id)) else: n_obj = None if n_obj: n_obj.name = name n_obj.imgurl = imgurl n_obj.about = about n_obj.put() else: #get node id nid_obj = Counter.get_or_insert('node_auto_increment', name='node_auto_increment', value=1) n_obj = Node(key=db.Key.from_path('Node', nid_obj.value)) n_obj.name = name n_obj.imgurl = imgurl n_obj.about = about n_obj.put() if n_obj.is_saved(): n_id = nid_obj.value nid_obj.value += 1 db.run_in_transaction(obj_runput, nid_obj, ['newest_add_node']) self.redirect('/add-node?id=%s' % str(n_id)) else: self.error(403) self.write('403:forbidden')
def post(self): if self.cur_user and self.cur_user.flag==99: n_id = self.request.get('id') name = self.POST['name'] imgurl = self.POST['imgurl'] about = self.POST['about'] if name: if n_id: n_obj = Node.get_by_id(int(n_id)) else: n_obj = None if n_obj: n_obj.name = name n_obj.imgurl = imgurl n_obj.about = about n_obj.put() else: #get node id nid_obj = Counter.get_or_insert('node_auto_increment', name='node_auto_increment', value = 1) n_obj = Node(key=db.Key.from_path('Node', nid_obj.value)) n_obj.name = name n_obj.imgurl = imgurl n_obj.about = about n_obj.put() if n_obj.is_saved(): n_id = nid_obj.value nid_obj.value += 1 db.run_in_transaction(obj_runput,nid_obj,['newest_add_node']) self.redirect('/add-node?id=%s'%str(n_id)) else: self.error(403) self.write('403:forbidden')
def post(self, nodeid='1'): if self.cur_user and self.cur_user.flag > 1: n_obj = Node.get_by_id(int(nodeid)) if not n_obj: self.error(404) self.write('404: not found') return errors = [] author = self.cur_user.name title = self.POST['title'] content = self.POST['content'] if title and content: if len(title) <= TITLE_MAX_S and len(content) <= CONTENT_MAX_S: int_time = int(time()) #check spam mi_obj = MemberInfo.get_or_insert(author) if self.cur_user.flag < 99: if mi_obj.topict: t_list = mi_obj.topict.split(',') if len(t_list) == MEMBER_RECENT_TOPIC and ( int_time - int(t_list[-1])) < 3600: self.write( u'403:不要发帖太频繁了 <a href="/newpost/%s">请返回</a>' % nodeid) return #check repeat content = textilize(content) #content = safe_encode(content) con_md5 = md5(content.encode('utf-8')).hexdigest() if memcache.get('c_' + con_md5): self.write(u'403:请勿灌水 <a href="/newpost/%s">请返回</a>' % nodeid) return else: memcache.set('c_' + con_md5, '1', 36000) if n_obj.count: topic_id = n_obj.count + 1 else: topic_id = 1 topic_key = '%s-%s' % (nodeid, str(topic_id)) t_obj = Topic(key_name=topic_key) t_obj.title = title t_obj.author = author t_obj.add = int_time t_obj.con = content t_obj.put() if t_obj.is_saved(): #node count +1 n_obj.count += 1 db.run_in_transaction(obj_runput, n_obj) #memberinfo mi_obj.topicn += 1 if mi_obj.topick: t_list = mi_obj.topick.split(',') t_list.insert(0, topic_key) mi_obj.topick = ','.join( t_list[:MEMBER_RECENT_TOPIC]) else: mi_obj.topick = topic_key if mi_obj.topict: t_list = mi_obj.topict.split(',') t_list.insert(0, str(int_time)) mi_obj.topict = ','.join( t_list[:MEMBER_RECENT_TOPIC]) else: mi_obj.topict = str(int_time) db.run_in_transaction(obj_runput, mi_obj, ['topic_objs:' + author]) #recent in home +key hi_obj = KeyStrValue.get_or_insert('recent-topic-home') if hi_obj.value: t_list = hi_obj.value.split(',') t_list.insert(0, topic_key) hi_obj.value = ','.join(t_list[:RECENT_POST_NUM]) else: hi_obj.value = topic_key db.run_in_transaction(obj_runput, hi_obj, [ 'get_topic_objs:recent-topic-home', 'get_topic_key_title:recent-topic-home' ]) #all topic counter +1 at_obj = Counter.get_or_insert('all-topic-num', name='all-topic-num') at_obj.value += 1 db.run_in_transaction(obj_runput, at_obj) #notifications mentions = findall_mentions(t_obj.con, author) if mentions: deferred.defer(set_mentions, topic_key, ','.join(mentions), _countdown=8, _queue="default") self.redirect('/t-' + topic_key) return else: t_obj = Topic(title=title, con=content) errors.append(u"注意标题和内容的最大字数限制,当前字数:%s %d" % (len(title), len(content))) else: t_obj = Topic(title=title, con=content) errors.append("标题和内容必填") self.echo('newpost.html', { 'title': "发新帖子", 'errors': errors, 'n_obj': n_obj, 't_obj': t_obj, 'newest_node': Node.get_newest(), }, layout='_layout.html') else: self.error(403) self.write('403:forbidden')
def getCounter(): counter = Counter.all().get() if counter is None: return 0 return counter.count
def get(self): max_nid_obj = Counter.get_or_insert('node_auto_increment', name='node_auto_increment', value = 1) self.echo('robots.txt',{'ids':[x for x in range(1,max_nid_obj.value)]})
from flask import Flask from model import Counter # Init a flask app app = Flask() count = Counter()
def setUp(self): self.store = BookStore() self.store.delete_all_books() Counter.reset_counter()
def post(self, nodeid='1'): if self.cur_user and self.cur_user.flag>1: n_obj = Node.get_by_id(int(nodeid)) if not n_obj: self.error(404) self.write('404: not found') return errors = [] author = self.cur_user.name title = self.POST['title'] content = self.POST['content'] if title and content: if len(title)<=TITLE_MAX_S and len(content)<=CONTENT_MAX_S: int_time = int(time()) #check spam mi_obj = MemberInfo.get_or_insert(author) if mi_obj.topict: t_list = mi_obj.topict.split(',') if len(t_list) == MEMBER_RECENT_TOPIC and (int_time-int(t_list[-1])) < 3600: self.write(u'403:不要发帖太频繁了 <a href="/newpost/%s">请返回</a>' % nodeid) return #check repeat content = textilize(content) #content = safe_encode(content) con_md5 = md5(content.encode('utf-8')).hexdigest() if memcache.get('c_'+con_md5): self.write(u'403:请勿灌水 <a href="/newpost/%s">请返回</a>' % nodeid) return else: memcache.set('c_'+con_md5, '1', 36000) if n_obj.count: topic_id = n_obj.count + 1 else: topic_id = 1 topic_key = '%s-%s' % (nodeid, str(topic_id)) t_obj = Topic(key_name=topic_key) t_obj.title = title t_obj.author = author t_obj.add = int_time t_obj.con = textilize(content) #t_obj.con = safe_encode(content) t_obj.put() if t_obj.is_saved(): #node count +1 n_obj.count += 1 db.run_in_transaction(obj_runput,n_obj) #memberinfo mi_obj.topicn += 1 if mi_obj.topick: t_list = mi_obj.topick.split(',') t_list.insert(0, topic_key) mi_obj.topick = ','.join(t_list[:MEMBER_RECENT_TOPIC]) else: mi_obj.topick = topic_key if mi_obj.topict: t_list = mi_obj.topict.split(',') t_list.insert(0, str(int_time)) mi_obj.topict = ','.join(t_list[:MEMBER_RECENT_TOPIC]) else: mi_obj.topict = str(int_time) db.run_in_transaction(obj_runput,mi_obj,['topic_objs:'+author]) #recent in home +key hi_obj = KeyStrValue.get_or_insert('recent-topic-home') if hi_obj.value: t_list = hi_obj.value.split(',') t_list.insert(0, topic_key) hi_obj.value = ','.join(t_list[:RECENT_POST_NUM]) else: hi_obj.value = topic_key db.run_in_transaction(obj_runput,hi_obj,['get_topic_objs:recent-topic-home', 'get_topic_key_title:recent-topic-home']) #all topic counter +1 at_obj = Counter.get_or_insert('all-topic-num', name = 'all-topic-num') at_obj.value += 1 db.run_in_transaction(obj_runput,at_obj) #notifications mentions = findall_mentions(t_obj.con, author) if mentions: deferred.defer(set_mentions, topic_key, ','.join(mentions), _countdown=8, _queue="default") self.redirect('/t-'+topic_key) return else: t_obj = Topic(title = title, con = content) errors.append(u"注意标题和内容的最大字数限制,当前字数:%s %d" % (len(title), len(content))) else: t_obj = Topic(title = title, con = content) errors.append("标题和内容必填") self.echo('newpost.html', { 'title': "发新帖子", 'errors':errors, 'n_obj': n_obj, 't_obj': t_obj, 'newest_node': Node.get_newest(), }, layout='_layout.html') else: self.error(403) self.write('403:forbidden')
def post(self): req_user = self.request.user gu_obj = GoogleUser.get_or_insert(req_user.email()) if gu_obj.name: self.redirect('/') return errors = [] name = self.POST['name'].strip().lower() if name: if len(name) < 20: if re.search('^[a-zA-Z0-9]+$', name): check_obj = Member.get_by_key_name(str(name)) if check_obj: errors.append('该用户名已被注册,请换一个吧') else: #get member id mid_obj = Counter.get_or_insert( 'member_auto_increment', name='member_auto_increment', value=1) nuser_obj = Member(key_name=name, id=mid_obj.value, flag=1, add=int(time())) nuser_obj.put() if nuser_obj.is_saved(): #set google user gu_obj.name = name db.run_in_transaction(obj_runput, gu_obj) #all member num +1 mid_obj.value += 1 db.run_in_transaction(obj_runput, mid_obj) #try to Fetch Google Plus Profile Picture try: img_url = 'https://profiles.google.com/s2/photos/profile/' + req_user.nickname( ) img_data = read_img(img_url) #self.header['Content-Type'] = "image/jpg" if img_data: pass else: img_url = 'https://www.gravatar.com/avatar/%s?s=73&d=monsterid' % md5( req_user.email()).hexdigest() img_data = read_img(img_url) if img_data: imgobj = images.Image(img_data) if imgobj.width <= 73 and imgobj.height <= 73: img_large = img_data else: imgobj.resize(73, 73) imgobj.im_feeling_lucky() img_large = imgobj.execute_transforms( output_encoding=images.JPEG, quality=95) u = UpYun(DOMAIN_NAME_AVATAR, UPYUN_USER, UPYUN_PW) file_path_name = '/avatar/%s.jpg' % self.cur_user.name avatar = u.writeFile( file_path_name, img_large, True) if avatar: m_obj = self.cur_user #Member.get_by_key_name(self.cur_user.name) if m_obj.flag == 1: if m_obj.id == 1: m_obj.flag = 99 else: m_obj.flag = 2 db.run_in_transaction( obj_runput, m_obj, [ 'cur_user:'******'Fetch Google Plus Profile Picture Failure: ' + req_user.email()) # self.redirect('/setavatar') return else: errors.append('服务器出现意外错误,请稍后再试') else: errors.append('用户名只能包含字母和数字') else: errors.append('用户名太长了') else: errors.append('用户名必填') self.echo('setname.html', { 'title': "设置名字", 'errors': errors, 'name': name, 'newest_node': Node.get_newest(), }, layout='_layout.html')
def post(self, nodeid, topicid): if self.cur_user and self.cur_user.flag>1: author = self.cur_user.name content = self.POST['content'] if content and len(content)<=COMMENT_MAX_S: int_time = int(time()) #check spam mi_obj = MemberInfo.get_or_insert(author) if mi_obj.replyt: t_list = mi_obj.replyt.split(',') if len(t_list) == MEMBER_RECENT_REPLY and (int_time-int(t_list[-1])) < 3600: self.write(u'403:不要回复太频繁了 <a href="/t-%s-%s">请返回</a>' % (nodeid, topicid)) return #check repeat content = textilize(content) #content = safe_encode(content) con_md5 = md5(content.encode('utf-8')).hexdigest() if memcache.get('c_'+con_md5): self.write(u'403:请勿灌水 <a href="/t-%s-%s">请返回</a>' % (nodeid, topicid)) return else: memcache.set('c_'+con_md5, '1', 36000) topic_key = '%s-%s' % (nodeid, topicid) t_obj = Topic.get_by_key_name(topic_key) if not t_obj: self.error(404) self.write('404: not found') return if t_obj.cnum: id_num = t_obj.cnum + 1 else: id_num = 1 c_key = '%s-%d' % (topic_key, id_num) c_obj = Comment(key_name=c_key) c_obj.author = author c_obj.add = int_time c_obj.con = content c_obj.put() if c_obj.is_saved(): #topic commont count +1 t_obj.cnum = id_num t_obj.reply = author t_obj.edit = int_time db.run_in_transaction(obj_runput,t_obj) #memberinfo mi_obj.replyn += 1 if mi_obj.replyk: t_list = mi_obj.replyk.split(',') if topic_key in t_list: t_list.remove(topic_key) t_list.insert(0, topic_key) mi_obj.replyk = ','.join(t_list[:MEMBER_RECENT_REPLY]) else: mi_obj.replyk = topic_key if mi_obj.replyt: t_list = mi_obj.replyt.split(',') t_list.insert(0, str(int_time)) mi_obj.replyt = ','.join(t_list[:MEMBER_RECENT_REPLY]) else: mi_obj.replyt = str(int_time) db.run_in_transaction(obj_runput,mi_obj,['reply_objs:'+author]) #recent reply +key hi_obj = KeyStrValue.get_or_insert('recent-reply-topic') if hi_obj.value: t_list = hi_obj.value.split(',') if topic_key in t_list: t_list.remove(topic_key) t_list.insert(0, topic_key) hi_obj.value = ','.join(t_list[:RECENT_COMMENT_NUM]) db.run_in_transaction(obj_runput,hi_obj,['get_topic_key_title:recent-reply-topic']) else: hi_obj.value = topic_key db.run_in_transaction(obj_runput,hi_obj,['get_topic_key_title:recent-reply-topic']) #all reply counter +1 at_obj = Counter.get_or_insert('all-reply-num', name = 'all-reply-num') at_obj.value += 1 db.run_in_transaction(obj_runput,at_obj) #notifications if t_obj.author != author: mentions = findall_mentions(c_obj.con+' @%s '%t_obj.author, author) else: mentions = findall_mentions(c_obj.con, author) if mentions: deferred.defer(set_mentions, topic_key, ','.join(mentions), _countdown=8, _queue="default") #del cache cache_keys = [] hi_obj = KeyStrValue.get_or_insert('recent-topic-home') if hi_obj.value: if topic_key in hi_obj.value.split(','): cache_keys.append('get_topic_objs:recent-topic-home') if id_num<EACH_PAGE_COMMENT_NUM: cache_keys.append('get_comments:%s:1' % topic_key) else: cache_keys.append('get_comments:%s:%d' % (topic_key, [i for i in range(1,id_num,EACH_PAGE_COMMENT_NUM)][-1])) if cache_keys: memcache.delete_multi(cache_keys) self.redirect('/t-%s#reply%d'%(topic_key,id_num)) return else: self.write('错误: 没有内容 或 内容太长了,请后退返回修改!') else: self.error(403) self.write('403:forbidden')
def get(self): max_nid_obj = Counter.get_or_insert('node_auto_increment', name='node_auto_increment', value=1) self.echo('robots.txt', {'ids': [x for x in range(1, max_nid_obj.value)]})
def post(self, nodeid, topicid): if self.cur_user and self.cur_user.flag > 1: author = self.cur_user.name content = self.POST['content'] if content and len(content) <= COMMENT_MAX_S: int_time = int(time()) #check spam mi_obj = MemberInfo.get_or_insert(author) if self.cur_user.flag < 99: if mi_obj.replyt: t_list = mi_obj.replyt.split(',') if len(t_list) == MEMBER_RECENT_REPLY and ( int_time - int(t_list[-1])) < 3600: self.write( u'403:不要回复太频繁了 <a href="/t-%s-%s">请返回</a>' % (nodeid, topicid)) return #check repeat content = textilize(content) #safe_encode(content) con_md5 = md5(content.encode('utf-8')).hexdigest() if memcache.get('c_' + con_md5): self.write(u'403:请勿灌水 <a href="/t-%s-%s">请返回</a>' % (nodeid, topicid)) return else: memcache.set('c_' + con_md5, '1', 36000) topic_key = '%s-%s' % (nodeid, topicid) t_obj = Topic.get_by_key_name(topic_key) if not t_obj: self.error(404) self.write('404: not found') return if t_obj.cnum: id_num = t_obj.cnum + 1 else: id_num = 1 c_key = '%s-%d' % (topic_key, id_num) c_obj = Comment(key_name=c_key) c_obj.author = author c_obj.add = int_time c_obj.con = content c_obj.put() if c_obj.is_saved(): #topic commont count +1 t_obj.cnum = id_num t_obj.reply = author t_obj.edit = int_time db.run_in_transaction(obj_runput, t_obj) #memberinfo mi_obj.replyn += 1 if mi_obj.replyk: t_list = mi_obj.replyk.split(',') if topic_key in t_list: t_list.remove(topic_key) t_list.insert(0, topic_key) mi_obj.replyk = ','.join(t_list[:MEMBER_RECENT_REPLY]) else: mi_obj.replyk = topic_key if mi_obj.replyt: t_list = mi_obj.replyt.split(',') t_list.insert(0, str(int_time)) mi_obj.replyt = ','.join(t_list[:MEMBER_RECENT_REPLY]) else: mi_obj.replyt = str(int_time) db.run_in_transaction(obj_runput, mi_obj, ['reply_objs:' + author]) #recent reply +key hi_obj = KeyStrValue.get_or_insert('recent-reply-topic') if hi_obj.value: t_list = hi_obj.value.split(',') if topic_key in t_list: t_list.remove(topic_key) t_list.insert(0, topic_key) hi_obj.value = ','.join(t_list[:RECENT_COMMENT_NUM]) db.run_in_transaction( obj_runput, hi_obj, ['get_topic_key_title:recent-reply-topic']) else: hi_obj.value = topic_key db.run_in_transaction( obj_runput, hi_obj, ['get_topic_key_title:recent-reply-topic']) #all reply counter +1 at_obj = Counter.get_or_insert('all-reply-num', name='all-reply-num') at_obj.value += 1 db.run_in_transaction(obj_runput, at_obj) #notifications if t_obj.author != author: mentions = findall_mentions( c_obj.con + ' @%s ' % t_obj.author, author) else: mentions = findall_mentions(c_obj.con, author) if mentions: deferred.defer(set_mentions, topic_key, ','.join(mentions), _countdown=8, _queue="default") #del cache cache_keys = [] hi_obj = KeyStrValue.get_or_insert('recent-topic-home') if hi_obj.value: if topic_key in hi_obj.value.split(','): cache_keys.append( 'get_topic_objs:recent-topic-home') if id_num < EACH_PAGE_COMMENT_NUM: cache_keys.append('get_comments:%s:1' % topic_key) else: cache_keys.append('get_comments:%s:%d' % (topic_key, [ i for i in range(1, id_num, EACH_PAGE_COMMENT_NUM) ][-1])) if cache_keys: memcache.delete_multi(cache_keys) self.redirect('/t-%s#reply%d' % (topic_key, id_num)) return else: self.write('错误: 没有内容 或 内容太长了,请后退返回修改!') else: self.error(403) self.write('403:forbidden')
def test_counter(self): Counter.reset_counter() self.assertEqual(1, Counter.get_counter()) self.assertEqual(2, Counter.get_counter())