def _rlrelase(self): try: self.rlock.release() except Exception as e: printlog.err(e) finally: pass
def add_article_views(request): if request.method == "GET": return render(request, 'addArticle.html') else: try: articleAuthor = request.POST['author'] articleTitle = request.POST['title'] articleComment = request.POST['content'] articleColumn = request.POST['column'] if articleAuthor and articleTitle and articleComment and articleColumn: article = Article(article_author=articleAuthor, article_title=articleTitle, article_column=articleColumn, article_comment=articleComment, article_views_count=0, article_update_time=datetime.now()) # article.switch_collection('article_more') # 切换集合,此用法需谨慎 article.save() # with switch_collection(Article,'article_more') as Article_More: # article = Article_More(name='Thomson', title='Phone Development', comment='python进阶教程')#切换集合 # article.save() sussOrFail = 200 return render(request, 'addArticle.html', status=sussOrFail) except Exception as e: sussOrFail = 404 printlog.err(e) return render(request, 'addArticle.html', status=sussOrFail)
def _rlacquire(self): try: self.rlock.acquire() except Exception as e: printlog.err(e) finally: pass
def database(): db = MYDatabase() try: if not db.connected: db._connect() yield db except Exception as e: printlog.err(e) db._close()
def filterIP(request): ''' :param request: :return: 1 -- 表示可以继续返回界面 -1 -- 表示不可以返回界面 0 -- 表示获取不到IP ''' # 主机名 domain = request.META.get('REMOTE_HOST') white_list = ['googlebot.com', 'crawl.baidu.com', 'sogou.com', 'bing.com'] # 白名单 for bot_domain in white_list: if domain and domain.find(bot_domain) > 0: return 1 # 获取IP user_ip = request.META.get('REMOTE_ADDR') if not user_ip: printlog.err('Get user_ip fail') return 0 try: DATAS = Robotkiller.objects.get(rbip=user_ip) except Robotkiller.DoesNotExist: Robotkiller.objects.create(rbip=user_ip, rbvisitcount=1, rbmintimecount=0) return 1 # 对访问时间间隔作捕获 now = datetime.now() passed_seconds = (now - DATAS.time).seconds # 转化为秒 # 具体逻辑可以是and/or if DATAS.rbvisitcount > visit_max_count or DATAS.rbmintimecount > visit_min_seconds_count: return -1 else: if passed_seconds < visit_min_seconds: DATAS.rbmintimecount += 1 DATAS.save() return 1 else: DATAS.rbvisitcount += 1 DATAS.time = now DATAS.save() return 1
def __update(self, key1, key2, action, value=None, upvalue=None): ''' 说明:调用接口为UPDATE,UPDATE函数内部调用__update eg:对匹配article_title的文档的article_views_count数值增加5 MGeng(Article).UPDATE('article_title', 'article_views_count', # 'incr', kvalue=artilcTitle, upvalue=5) :param key1: 用于查询文档的key 注意:若key1是id,则必须用 _id 形式,因数据库存储形式为 _id :param key2: 更新的key :param action: 更新操作 --incr 增加 --decr 减少 --set 设置新值 --unset 删除 :param value: 用于查询文档的key对应的值 :param upvalue: 更新字段的值 :return: ''' try: if action == 'incr': upaction = 'inc__' + key2 self.Object(__raw__={ key1: value }).update(**{upaction: upvalue}) elif action == 'decr': upaction = 'dec__' + key2 self.Object(__raw__={ key1: value }).update(**{upaction: upvalue}) elif action == 'set': upaction = 'set__' + key2 self.Object(__raw__={ key1: value }).update(**{upaction: upvalue}) elif action == 'unset': upaction = 'unset__' + key2 self.Object(__raw__={key1: value}).update(**{upaction: 1}) else: pass except Exception as e: printlog.err(e)
def update_article_views(request): if request.method == 'GET': return HttpResponseRedirect('/article/get_article') else: try: article = Article.objects.first() postBody = request.body postContent = json.load(postBody) postComment = postContent.comment article.update(comment=postComment, views=60) article.save() return HttpResponse('OK') except Exception as e: printlog.err(e) finally: INFO = 'update article' printlog.info(INFO)
def add_article_comment_views(request): if request.method == 'POST': try: commentBody = request.body.commennt commentName = request.body.NAME commentContent = request.body.content if commentBody: Article.objects(Q(article_name=commentName))\ .update_one(push__article_comment=commentContent) else: err = 'No comments gets' printlog.err(err) except Exception as e: printlog.err(e) finally: pass else: info = 'Cannot be method GET' printlog.info(info)
def __updateOne(self, key1, key2, action, value=None, upvalue=None): ''' 说明:此更新操作只更新符合匹配的第一个文档的值 ''' try: if action == 'incr': upaction = 'inc__' + key2 self.Object(__raw__={ key1: value }).update_one(**{upaction: upvalue}) elif action == 'decr': upaction = 'dec__' + key2 self.Object(__raw__={ key1: value }).update_one(**{upaction: upvalue}) elif action == 'set': upaction = 'set__' + key2 self.Object(__raw__={ key1: value }).update_one(**{upaction: upvalue}) elif action == 'unset': upaction = 'unset__' + key2 self.Object(__raw__={key1: value}).update_one(**{upaction: 1}) elif action == 'push': lupaction = 'push__' + key2 self.Object(__raw__={ key1: value }).update_one(**{lupaction: upvalue}) elif action == 'pushall': if isinstance(upvalue, list): lupaction = 'push_all__' + key2 self.Object(__raw__={ key1: value }).update_one(**{lupaction: upvalue}) else: return else: pass except Exception as e: printlog.err(e)
def __updateList(self, key1, key2, action, value=None, lupvalue=None): try: if action == 'push': lupaction = 'push__' + key2 self.Object(__raw__={ key1: value }).update(**{lupaction: lupvalue}) elif action == 'pushall': if isinstance(lupvalue, list): lupaction = 'push_all__' + key2 self.Object(__raw__={ key1: value }).update(**{lupaction: lupvalue}) else: printlog.err('[PUSH_ALL]lupvalue must be list') return elif action == 'pull': lupaction = 'pull__' + key2 self.Object(__raw__={ key1: value }).update(**{lupaction: lupvalue}) elif action == 'pullall': if isinstance(lupvalue, list): lupaction = 'pull_all__' + key2 self.Object(__raw__={ key1: value }).update(**{lupaction: lupvalue}) else: printlog.err('[PULL_ALL]lupvalue must be list') return else: pass except Exception as e: printlog.err(e)