Exemple #1
0
 def post(self, request):
     user_id = request.POST.get('user_id')
     article_id = request.POST.get('article_id')
     content = request.POST.get('content')
     not_valid_str = DiscussView.base_str.format(
         '内容不能为空!', '/article/details/{}/'.format(article_id))
     defeat_str = DiscussView.base_str.format(
         '评论失败,稍后再试!', '/article/details/{}/'.format(article_id))
     # success_str = DiscussView.base_str.format('', '/article/details/{}/'.format(article_id))
     if not content:
         return HttpResponse(not_valid_str)
     try:
         # 写入数据库
         Discussion.objects.create(create_time=now(),
                                   content=content,
                                   article_id=article_id,
                                   user_id=user_id,
                                   is_delet=0)
         # F查询,帖子评论数加1
         Article.objects.filter(id=article_id).update(discuss=F('discuss') +
                                                      1)
     except:
         return HttpResponse(defeat_str)
     else:
         return HttpResponseRedirect(
             '/article/details/{}'.format(article_id))
Exemple #2
0
 def __doc_for_first_vist_mainpage(self, request, num):
     host = '0.0.0.0'
     try:
         # 获取主机地址,- 开头表示使用了ip代理
         if num == 1:
             if 'HTTP_X_FORWARDED_FOR' in request.META:
                 host = '-' + request.META['HTTP_X_FORWARDED_FOR']
             elif 'REMOTE_ADDR' in request.META:
                 host = request.META['REMOTE_ADDR']
         else:
             return
     except Exception as e:
         # print(e)
         return
     else:
         visit_time = getnow.now()
         is_delet = 0
     try:
         VisitDocument.objects.create(
             host=host,
             visit_time=visit_time,
             is_delet=is_delet,
         )
     except Exception as e2:
         # print(e2)
         return
Exemple #3
0
 def post(self, request):
     user_id = request.POST.get('user_id')
     file = request.FILES.get('upfile')
     # 文件内容判空
     if not file:
         return HttpResponse(FileUpload.file_is_empty)
     # 文件名长短判断
     if self._is_file_name_too_long(file.name):
         return HttpResponse(FileUpload.file_name_too_long)
     # 文件类型判断
     type_ = self.file_type(file.name)
     if type_ == 'unknow':
         return HttpResponse(FileUpload.file_type_not_allowed)
     else:
         type_id = FileType.objects.get(filetype=type_).id
         # print(type_id)
     # 准备存放位置
     user = self.get_user_info_by_id(user_id)
     user_dir, imgs_dir, docs_dir = self.init_dir_for_user_file(user)
     # 判断单个文件大小是否超标
     if self._is_file_too_large(file, user.account):
         return HttpResponse(FileUpload.file_too_large)
     # 判断用户内存是否用完
     if self._is_over_memory_size(user_dir, file):
         return HttpResponse(FileUpload.over_memory_size)
     # 写文件
     try:
         filepath = self.save_file(file, type_, imgs_dir, docs_dir)
     except:
         return HttpResponse(FileUpload.defeat_upload)
     # 数据库更新, 倘若数据库出错,则删除刚才的文件
     upload_time = now()
     content_type = file.content_type
     if File.objects.filter(name=file.name).count() > 0:
         return HttpResponse(FileUpload.file_name_repeat)
     try:
         File.objects.create(name=file.name,
                             path=filepath,
                             upload_time=upload_time,
                             download=0,
                             content_type=content_type,
                             is_delet=0,
                             filetype_id=type_id,
                             user_id=user.id)
     except:
         os.remove(filepath)
         return HttpResponse(FileUpload.defeat_upload)
     else:
         # 返回
         return HttpResponseRedirect('/file/filecenter')
Exemple #4
0
 def post(self, request):
     defeat = "<script>alert('出现错误,稍后再试!');window.location.href='/';</script>"
     user_id = request.POST.get('user_id', '')
     title = request.POST.get('title', '')
     cat = request.POST.get('my_category', '')
     tag = request.POST.get('my_tag', '')
     cont = request.POST.get('content', '')
     # 数据库写入,返回首页
     a_id = request.POST.get('a_id', None)
     if not a_id:
         try:
             Article.objects.create(title=title,
                                    create_time=now(),
                                    content=cont,
                                    read=0,
                                    discuss=0,
                                    is_delet=0,
                                    author_id=user_id,
                                    category_id=cat,
                                    tag_id=tag)
         except:
             return HttpResponse(defeat)
     else:
         try:
             Article.objects.filter(id=a_id).update(title=title,
                                                    create_time=now(),
                                                    content=cont,
                                                    read=0,
                                                    discuss=0,
                                                    is_delet=0,
                                                    author_id=user_id,
                                                    category_id=cat,
                                                    tag_id=tag)
         except:
             return HttpResponse(defeat)
     return HttpResponseRedirect('/')
Exemple #5
0
 def post(self, request):
     from vshare.settings import MAX_USER_SIZE
     # 会员数上限
     if models.User.objects.filter(is_delet=0).count() > MAX_USER_SIZE:
         return HttpResponse(Regist.str_user_over)
     account = request.POST.get('account', '')
     pwd = request.POST.get('pwd', '')
     sure_pwd = request.POST.get('sure_pwd', '')
     answer = request.POST.get('answer', '')
     question = request.POST.get('question', '')
     phone = request.POST.get('phone', '')
     tip = request.POST.get('tip', '')
     modified = now()
     successful_str = Regist.str_00.format(account, pwd)
     if not self._is_account_valid(account):
         # print(Regist.str_01)
         return HttpResponse(Regist.str_01)
     elif not self._is_pwd_valid(pwd):
         return HttpResponse(Regist.str_02)
     elif not self._is_sure_pwd(pwd, sure_pwd):
         return HttpResponse(Regist.pwd_is_not_sure_pwd)
     elif not self._is_question_valid(question):
         return HttpResponse(Regist.str_03)
     elif not self._is_answer_valid(answer):
         return HttpResponse(Regist.str_04)
     if phone:
         if not self._is_phone_valid(phone):
             return HttpResponse(Regist.str_05)
     if tip:
         if not self._is_tip_valid(tip):
             return HttpResponse(Regist.str_06)
     # 判断账号是否已经存在
     if self._is_account_exited(account):
         return HttpResponse(Regist.str_08)
     try:
         # 存入数据库
         models.User.objects.create(account=account,
                                    pwd=pwd,
                                    question=question,
                                    answer=answer,
                                    phone=phone,
                                    tip=tip,
                                    modified=modified,
                                    is_delet=0)
     except:
         return HttpResponse(Regist.str_07)
     else:
         return HttpResponse(successful_str)
Exemple #6
0
 def post(self, request):
     user_id = request.POST.get('user_id', '')
     new_question = request.POST.get('new_question', '')
     new_answer = request.POST.get('new_answer', '')
     sure_answer = request.POST.get('sure_answer', '')
     if not self.is_length_valid(new_question, new_answer):
         return HttpResponse(ModifyPwdSafe.field_not_valid)
     if new_answer != sure_answer:
         return HttpResponse(ModifyPwdSafe.not_sure_answer)
     # 修改数据库
     try:
         models.User.objects.filter(id=user_id).update(
             question=new_question, answer=new_answer, modified=now())
     except:
         return HttpResponse(ModifyPwdSafe.defeat)
     return HttpResponseRedirect('/')
Exemple #7
0
 def post(self, request):
     user_id = request.POST.get('user_id', '')
     pwd = request.POST.get('pwd', '')
     new_pwd = request.POST.get('new_pwd', '')
     sure_pwd = request.POST.get('sure_pwd', '')
     user = models.User.objects.get(id=user_id)
     if user.pwd != pwd:
         return HttpResponse(ModifyPwd.pwd_wrong)
     if new_pwd != sure_pwd:
         return HttpResponse(ModifyPwd.pwd_not_sure)
     if not self._is_pwd_valid(new_pwd):
         return HttpResponse(ModifyPwd.pwd_not_valid)
     try:
         models.User.objects.filter(id=user.id).update(pwd=new_pwd,
                                                       modified=now())
     except:
         pass
     # 修改成功,注销当前登录
     return HttpResponseRedirect('/user_/logout')
Exemple #8
0
 def post(self, request):
     host = request.META.get('REMOTE_ADDR', '0.0.0.0')
     # 删除验证码的session
     try:
         del request.session[host]
     except:
         pass
     base_str = '''
     <script>
     alert("{}");
     window.location.href = '/aboutsite/msgboard';
     </script>
     '''
     success_code = '<script>window.location.href="/aboutsite/msgboard";</script>'
     defeat_code = base_str.format('留言失败,可能是留言过长导致的,请稍后再试!')
     empty_content = base_str.format('留言内容不能为空!')
     # print(success_code, defeat_code)
     # 留言写入数据库
     nickname = request.POST.get('nickname', None)
     email = request.POST.get('email', None)
     content = request.POST.get('content', None)
     if content:
         pass
     else:
         return HttpResponse(empty_content)
     create_time = getnow.now()
     is_delet = 0
     try:
         MsgBoard.objects.create(nickname=nickname,
                                 create_time=create_time,
                                 content=content,
                                 email=email,
                                 is_delet=is_delet)
     except Exception as e:
         return JsonResponse({'result_info': defeat_code})
     else:
         return JsonResponse({'result_info': success_code})