def RemoveItem(ItemId, UserId, Misc): tItem = tarsusaItem.get_by_id(int(ItemId)) if tItem is not None: if tItem.usermodel.key().id() == int(UserId): ## Check User Permission to undone this Item if tItem.public != 'none': memcache.event('deletepublicitem', int(UserId)) if tItem.routine == 'none': ## if this item is not a routine item. tItem.delete() memcache.event('deleteitem', int(UserId)) else: ## Del a RoutineLog item! ## All its doneRoutineLogWillBeDeleted! ## wether there will be another log for this? :-) for record nerd? ## This is a daily routine, and we are going to delete it. ## For DailyRoutine, now I just count the matter of deleting today's record. ## the code for handling the whole deleting routine( delete all concerning routine log ) will be added in future # GAE can not make dateProperty as query now! There is a BUG for GAE! # http://blog.csdn.net/kernelspirit/archive/2008/07/17/2668223.aspx tarsusaRoutineLogItemCollection_ToBeDeleted = db.GqlQuery("SELECT * FROM tarsusaRoutineLogItem WHERE routineid = :1", int(ItemId)) for result in tarsusaRoutineLogItemCollection_ToBeDeleted: result.delete() tItem.delete() memcache.event('deleteroutineitem_daily', int(UserId)) #ShardingCounter shardingcounter.increment("tarsusaItem", "minus") return 0 else: #Authentication Failed. return 1 else: #Authentication Failed. return 1
def get(self): if users.get_current_user() != None: CurrentUser = self.get_user_db() if CurrentUser == None: # Create a User CurrentUser = tarsusaUser(user=users.get_current_user(), urlname=cgi.escape(users.get_current_user().nickname())) CurrentUser.put() ## Added userid property. CurrentUser.userid = CurrentUser.key().id() CurrentUser.dispname = users.get_current_user().nickname() CurrentUser.put() logging.info("New User, id:" + str(CurrentUser.key().id()) + " name:" + CurrentUser.dispname) #ShardingCounter import shardingcounter shardingcounter.increment("tarsusaUser") else: ## DB Model Patch ## These code for registered user whose information are not fitted into the new model setting. #Run DB Model Patch when User Logged in. #DBPatcher.chk_dbmodel_update(CurrentUser) #Run this at every ViewItem event ## Added userid here. if CurrentUser.userid == None: CurrentUser.userid = CurrentUser.key().id() CurrentUser.put() ## Check usedtags as the evaluation for Tags Model ## TEMP CODE! UserTags = '<a href=/tag/>未分类项目</a> '.decode('utf-8') if CurrentUser.usedtags: CheckUsedTags = [] for each_cate in CurrentUser.usedtags: ## After adding code with avoiding add duplicated tag model, it runs error on live since there are some items are depending on the duplicated ones. try: ## Caution! Massive CPU consumption. ## Due to former BUG, there might be duplicated tags in usertags. ## TO Solve this. DuplicatedTags = False for each_cate_vaild in CheckUsedTags: if each_cate.name == each_cate_vaild: DuplicatedTags = True CheckUsedTags.append(each_cate.name) if DuplicatedTags != True: try: ## Since I have deleted some tags in CheckNerds manually, ## so there will be raise such kind of errors, in which the tag will not be found. each_tag = db.get(each_cate) UserTags += '<a href="/tag/' + cgi.escape(each_tag.name) + '">' + cgi.escape(each_tag.name) + '</a> ' except: ## Tag model can not be found. pass except: pass UserTags += 'Error, On MainPage Tags Section.' template_values = { 'UserLoggedIn': 'Logged In', 'UserNickName': cgi.escape(CurrentUser.dispname), 'UserID': CurrentUser.key().id(), 'htmltag_today': datetime.datetime.date(datetime.datetime.now()), 'UserTags': UserTags, } #Manupilating Templates path = os.path.join(os.path.dirname(__file__), 'pages/index.html') self.response.out.write(template.render(path, template_values)) else: #WelcomePage for Non-registered Users. IsCachedWelcomePage = memcache.get_item('strCachedWelcomePage', 'global') if IsCachedWelcomePage: strCachedWelcomePage = IsCachedWelcomePage else: TotalUserCount = tarsusaCore.get_count_tarsusaUser() TotaltarsusaItem = tarsusaCore.get_count_tarsusaItem() ## Homepage for Non-Registered Users. template_values = { 'UserNickName': "访客", 'AnonymousVisitor': "Yes", 'htmltag_TotalUser': TotalUserCount, 'htmltag_TotaltarsusaItem': TotaltarsusaItem, } #Manupilating Templates path = os.path.join(os.path.dirname(__file__), 'pages/welcome.html') strCachedWelcomePage = template.render(path, template_values) memcache.set_item("strCachedWelcomePage", strCachedWelcomePage, 'global') self.response.out.write(strCachedWelcomePage)
def AddItem(UserId, rawName, rawComment='', rawRoutine='', rawPublic='private', rawInputDate='', rawTags=None): CurrentUser = tarsusaUser.get_by_id(int(UserId)) #Check if comment property's length is exceed 500 try: if len(rawComment)>500: item_comment = rawComment[:500] else: item_comment = rawComment except: item_comment = '' try: # The following code works on GAE platform. # it is weird that under GAE, it should be without .decode, but on localhost, it should add them! item2beadd_name = cgi.escape(rawName) try: item2beadd_comment = cgi.escape(item_comment) except: item2beadd_comment = '' try: tarsusaItem_Tags = cgi.escape(rawTags).split(",") except: tarsusaItem_Tags = None #routine is a must provided in template, by type=hidden item2beadd_routine = cgi.escape(rawRoutine) first_tarsusa_item = tarsusaItem(user=CurrentUser.user, name=item2beadd_name, comment=item2beadd_comment, routine=rawRoutine) first_tarsusa_item.public = rawPublic first_tarsusa_item.done = False # DATETIME CONVERTION TRICKS from http://hi.baidu.com/huazai_net/blog/item/8acb142a13bf879f023bf613.html # The easiest way to convert this to a datetime seems to be; #datetime.date(*time.strptime("8/8/2008", "%d/%m/%Y")[:3]) # the '*' operator unpacks the tuple, producing the argument list. # also learned sth from: http://bytes.com/forum/thread603681.html # Logic: If the expectdate is the same day as today, It is none. try: expectdatetime = None expectdate = datetime.date(*time.strptime(rawInputDate,"%Y-%m-%d")[:3]) if expectdate == datetime.datetime.date(datetime.datetime.today()): expectdatetime == None else: currenttime = datetime.datetime.time(datetime.datetime.now()) expectdatetime = datetime.datetime(expectdate.year, expectdate.month, expectdate.day, currenttime.hour, currenttime.minute, currenttime.second, currenttime.microsecond) except: expectdatetime = None first_tarsusa_item.expectdate = expectdatetime ## the creation date will be added automatically by GAE datastore first_tarsusa_item.usermodel = CurrentUser #first_tarsusa_item.put() try: tarsusaItem_Tags = cgi.escape(rawTags).split(",") except: tarsusaItem_Tags = None except: #Something is wrong when adding the item. self.write("sth is wrong.") #memcache related. Clear ajax_DailyroutineTodayCache after add a daily routine item if item2beadd_routine == 'daily': memcache.event('addroutineitem_daily', CurrentUser.key().id()) else: memcache.event('additem', CurrentUser.key().id()) if cgi.escape(rawPublic) != 'private': memcache.event('addpublicitem', CurrentUser.key().id()) if tarsusaItem_Tags != None: for each_tag_in_tarsusaitem in tarsusaItem_Tags: ## It seems that these code above will create duplicated tag model. ## TODO: I am a little bit worried when the global tags are exceed 1000 items. catlist = db.GqlQuery("SELECT * FROM Tag WHERE name = :1 LIMIT 1", each_tag_in_tarsusaitem) try: each_cat = catlist[0] except: try: #added this line for Localhost GAE runtime... each_cat = Tag(name=each_tag_in_tarsusaitem.decode('utf-8')) each_cat.put() except: each_cat = Tag(name=each_tag_in_tarsusaitem) each_cat.put() first_tarsusa_item.tags.append(each_cat.key()) # To Check whether this user is using this tag before. tag_AlreadyUsed = False for check_whether_used_tag in CurrentUser.usedtags: item_check_whether_used_tag = db.get(check_whether_used_tag) if item_check_whether_used_tag != None: if each_cat.key() == check_whether_used_tag or each_cat.name == item_check_whether_used_tag.name: tag_AlreadyUsed = True else: if each_cat.key() == check_whether_used_tag: tag_AlreadyUsed = True if tag_AlreadyUsed == False: CurrentUser.usedtags.append(each_cat.key()) first_tarsusa_item.put() CurrentUser.put() #ShardingCounter shardingcounter.increment("tarsusaItem") return first_tarsusa_item.key().id()