def up_down_share_post(req): action_type = req.GET.get('type') post_id = req.GET.get('post_id') user_id = req.user.id if action_type in ('up', 'down'): if not req.user.is_authenticated(): json_str = {'status': 0, 'msg': u'登录之后方可操作'} return response_json(json_str) if blog_models.ThumbUpDown.objects.filter( post_id=post_id, user_id=user_id, thumb_type=action_type).exists(): json_str = {'status': 0, 'msg': u'不可重复操作'} else: blog_models.ThumbUpDown.objects.create(user_id=user_id, thumb_type=action_type, post_id=post_id) count = blog_models.ThumbUpDown.objects.filter( post_id=post_id, thumb_type=action_type).count() json_str = {'status': 1, 'count': count} elif action_type in ('weibo', 'qq', 'weixin', 'tencent', 'douban', 'qzone', 'linkedin', 'diandian', 'facebook', 'twitter', 'google'): blog_models.PostShare.objects.create(post_id=post_id, destination=action_type) count = blog_models.PostShare.objects.filter(post_id=post_id).count() json_str = {'status': 1, 'count': count} else: json_str = {'status': 0, 'msg': u'未知类型的操作'} return response_json(json_str)
def register_account(req): email = req.POST.get('email') uname = req.POST.get('uname') pwd = req.POST.get('pwd') confirm_pwd = req.POST.get('confirm_pwd') if pwd != confirm_pwd: json_str = {'status': 0, 'msg': u'两次密码输入不一致,请重新输入'} return response_json(json_str) if not re.match('[a-zA-Z]\w{5,9}', pwd): json_str = {'status': 0, 'msg': u'密码长度6-10位,以字母开头,只能输入字母、数字、下划线'} return response_json(json_str) if User.objects.filter(username=uname).exists(): json_str = {'status': 0, 'msg': u'该用户名已被注册,请更换'} return response_json(json_str) if User.objects.filter(email=email).exists(): json_str = {'status': 0, 'msg': u'该邮箱已被注册,请更换'} return response_json(json_str) json_str = {'status': 0, 'msg': u'系统异常,请稍后重试'} with transaction.atomic(): user = User.objects.create_user(uname, email, pwd) user.is_active = 1 user.userextend.portrait_id = set_avatar_rendom() user.save() # send_email('register', user) # json_str = {'status': 1, 'msg': u'激活链接已发送到邮箱,请激活'} json_str = {'status': 1, 'msg': u'注册成功,请登录'} return response_json(json_str)
def set_nickname(req): user = req.user nickname = req.GET.get('nickname') if not nickname.strip(): json_str = {'status': 0, 'msg': u'昵称不允许为空'} return response_json(json_str) blog_models.UserExtend.objects.filter(user=user).update(nickname=nickname) json_str = {'status': 1, 'msg': u'昵称设置成功'} return response_json(json_str)
def get_label_list(req): try: cats = blog_models.Category.objects.filter(level=2) labelList = [{ 'href': '/category/%s/%s' % (blog_models.Category.objects.get(id=cat.parent_level).name, cat.name), 'name': cat.name } for cat in cats] return response_json({'labelList': labelList}) except Exception, exc: logger.error(exc, exc_info=True) return response_json({'labelList': []})
def get_user_post(req): user = req.user page = req.GET.get('page', 1) limit = settings.PAGE_SIZE # 每页显示的记录数 posts = blog_models.Post.objects.filter(author__user__id=user.id, is_valid=1).order_by('-id') paginator = Paginator(posts, limit) # 实例化一个分页对象 try: postObj = paginator.page(page) # 获取某页对应的记录 except PageNotAnInteger: # 如果页码不是个整数 postObj = paginator.page(1) # 取第一页的记录 except EmptyPage: # 如果页码太大,没有相应的记录 postObj = [] post_list = [{ 'time': str(post.post_time)[0:19], 'summary': '%s...' % post.summary[0:50] if len(post.summary) > 50 else post.summary, 'post_detail_path': post.post_detail_path() } for post in postObj] json_str = { 'post_list': post_list, 'next': 'yes' if postObj.has_next() else 'no', 'previous': 'yes' if postObj and postObj.has_previous() else 'no', 'current_page': page } return response_json(json_str)
def make_leave_comment_submit(req): body = json.loads(req.body) user = req.user msg = body.get('msg', '') level = body.get('level', 1) parent_id = body.get('parent_id') try: if not msg.strip(): json_str = {'status': 0, 'msg': u'留言不允许为空'} return response_json(json_str) # 保存留言 blog_models.MessageLeave.objects.create(leaver_id=user.id, message=msg, level=level, parent_id=parent_id) msgObj = { 'leaver_name': user.userextend.nickname, 'leaver_portrait': "/static/images/%s/%s" % (user.userextend.portrait.img_category.name, user.userextend.portrait.src), 'leave_msg': msg, 'leave_time': str(datetime.now())[0:19] } msg = u'留言成功,博主会尽快给您答复!' if str(level) == '1' else u'回复成功!' json_str = {'status': 1, 'msg': msg, 'obj': msgObj} except Exception, exc: logger.error(exc, exc_info=True) json_str = {'status': 0, 'msg': u'留言异常,请稍后重试!'}
def change_other(req): user = req.user type = req.POST.get('type') value = req.POST.get('value') try: if type == 'name': # 检查用户名是否被其他用户占用 if blog_models.User.objects.exclude(id=user.id).filter( username=value).exists(): json_str = {'status': 0, 'msg': u'该用户名已被占用,请更换!'} return response_json(json_str) else: user.username = value user.save() json_str = {'status': 1, 'msg': u'您已成功修改用户名'} return response_json(json_str) elif type == 'nickName': # 检查用户昵称是否被其他用户占用 if blog_models.UserExtend.objects.exclude(user=user).filter( nickname=value).exists(): json_str = {'status': 0, 'msg': u'该昵称已被占用,请更换!'} return response_json(json_str) else: user.userextend.nickname = value user.userextend.save() json_str = {'status': 1, 'msg': u'您已成功修改昵称'} return response_json(json_str) elif type == 'email': if '@' not in value: json_str = {'status': 0, 'msg': u'请输入有效邮箱'} return response_json(json_str) elif value == user.email: json_str = {'status': 0, 'msg': u'修改邮箱与原有邮箱一致'} return response_json(json_str) else: # 给新邮箱发确认邮件 # send_email('update', user, email=value) # json_str = {'status': 1, 'msg': u'邮件已发送,请检查邮箱'} user.email = value user.save() json_str = {'status': 1, 'msg': u'修改成功'} return response_json(json_str) else: json_str = {'status': 0, 'msg': u'类型错误,请稍后重试'} return response_json(json_str) except Exception, exc: logger.error(exc, exc_info=True) json_str = {'status': 0, 'msg': u'服务器异常,请稍后重试'} return response_json(json_str)
def change_pwd(req): old_pwd = req.POST.get('old_pwd') new_pwd = req.POST.get('new_pwd') confirm_pwd = req.POST.get('confirm_pwd') # 检查原密码是否正确 if not req.user.check_password(old_pwd): json_str = {'status': 0, 'msg': u'原密码错误,请检查!'} return response_json(json_str) # 检查新密码是否符合规则 if new_pwd != confirm_pwd: json_str = {'status': 0, 'msg': u'两次密码输入不一致,请重新输入'} return response_json(json_str) if not re.match('[a-zA-Z]\w{5,9}', new_pwd): json_str = {'status': 0, 'msg': u'密码长度6-10位,以字母开头,只能输入字母、数字、下划线'} return response_json(json_str) # 保存新密码 req.user.set_password(new_pwd) req.user.save() json_str = {'status': 1, 'msg': u'新密码设置成功'} return response_json(json_str)
def upload_avatar(req): user = req.user avatar = req.FILES.get('avatar') try: # 检查用户是否选择文件 if not avatar: json_str = {'status': 0, 'msg': u'请选择需要上传的图片'} return response_json(json_str) # 检查文件大小及格式 name = avatar.name size = avatar.size foramt = avatar.content_type.split('/')[1] if foramt not in ('jpg', 'png', 'jpeg'): json_str = {'status': 0, 'msg': u'请选择JPG/PNG/JPEG格式的图片'} return response_json(json_str) if size > 2 * 1024 * 1024: json_str = {'status': 0, 'msg': u'请上传2M以内的图片'} return response_json(json_str) # 图片插入记录及修改用户头像走原子操作 with transaction.atomic(): name = '%s-%s' % (user.id, name) path = os.path.join(IMG_SAVE_ABSOLUTE_PATH, name) save = open(path, 'wb') save.write(avatar.read()) img = blog_models.Images.objects.create( src=name, img_category_id=blog_models.ImagesCategory.objects.get( name='other').id) blog_models.UserExtend.objects.filter(user=user).update( portrait_id=img.id) json_str = { 'status': 1, 'msg': u'头像上传成功', 'img': '/static/images/other/%s' % user.userextend.portrait.src } return response_json(json_str) except Exception, exc: logger.error(exc, exc_info=True) json_str = {'status': 0, 'msg': u'图片上传异常,请检查图片格式'} return response_json(json_str)
def get_more_message(req): page = req.GET.get('page', 1) is_login = 1 if req.user.is_authenticated() else 0 limit = settings.PAGE_SIZE # 每页显示的记录数 msgs = blog_models.MessageLeave.objects.filter(level=1).order_by('-id') paginator = Paginator(msgs, limit) # 实例化一个分页对象 def _get_childrens(parentId): childrens = blog_models.MessageLeave.objects.filter( parent_id=parentId).order_by('-id') return [{ 'leaver_name': child.leaver.user.userextend.nickname, 'leaver_portrait': "/static/images/%s/%s" % (child.leaver.portrait.img_category.name, child.leaver.portrait.src), 'leave_msg': child.message, 'leave_time': str(child.leave_time)[0:19], } for child in childrens] try: msgObj = paginator.page(page) # 获取某页对应的记录 except PageNotAnInteger: # 如果页码不是个整数 msgObj = paginator.page(1) # 取第一页的记录 except EmptyPage: # 如果页码太大,没有相应的记录 # msgObj = paginator.page(paginator.num_pages) # 取最后一页的记录 msgObj = [] msg_list = [{ 'leaver_name': msg.leaver.user.userextend.nickname, 'leaver_portrait': "/static/images/%s/%s" % (msg.leaver.portrait.img_category.name, msg.leaver.portrait.src), 'leave_msg': msg.message, 'leave_time': str(msg.leave_time)[0:19], 'id': msg.id, 'childrens': _get_childrens(msg.id) } for msg in msgObj] json_str = { 'msg_list': msg_list, 'next': 'yes' if msgObj.has_next() else 'no', 'previous': 'yes' if msgObj and msgObj.has_previous() else 'no', 'current_page': page, 'is_login': is_login } return response_json(json_str)
def get_top_three_post(req): page = req.GET.get('page', 1) posts = blog_models.Post.objects.all().order_by('?')[(int(page) - 1) * 9:int(page) * 9] post_list = [{ 'img': "/static/images/%s/%s" % (post.img.img_category.name, post.img.src), 'href': post.post_detail_path(), 'summary': post.summary } for post in posts] data = {'post_list': post_list} return response_json(data)
def user_login(req): username = req.POST.get('username') password = req.POST.get('password') remember = req.POST.get('remember') if req.user.is_authenticated(): json_str = {'status': 0, 'msg': u'您已登录,不可重复登录!'} return response_json(json_str) user = authenticate(username=username, password=password) if user is not None: if not user.is_active: json_str = {'status': 0, 'msg': u'您的账号处于未激活状态,请激活!'} else: login(req, user) ip = get_user_ip(req) blog_models.UserExtend.objects.filter(user=user).update( login_ip=ip) json_str = {'status': 1, 'msg': u'您已成功登陆!'} # 一周之内保持登录状态 if str(remember) == '1': req.session.set_expiry(60 * 60 * 24 * 7) else: json_str = {'status': 0, 'msg': u'用户名与密码不匹配,请检查!'} return response_json(json_str)
def update_forget_pwd(req): uname = req.POST.get('uname') email = req.POST.get('email') pwd = req.POST.get('pwd') pwd_again = req.POST.get('pwd_again') try: user = User.objects.get(username=uname, email=email) # 检查新密码是否符合规则 if pwd != pwd_again: json_str = {'status': 0, 'msg': u'两次密码输入不一致,请重新输入'} return response_json(json_str) if not re.match('[a-zA-Z]\w{5,9}', pwd): json_str = {'status': 0, 'msg': u'密码长度6-10位,以字母开头,只能输入字母、数字、下划线'} return response_json(json_str) # 保存新密码 user.set_password(pwd) user.save() json_str = {'status': 1, 'msg': u'新密码设置成功,请登录'} return response_json(json_str) except Exception, exc: logger.error(exc, exc_info=True) json_str = {'status': 0, 'msg': u'用户名与邮箱不匹配'} return response_json(json_str)
def get_user_list(req): page = req.GET.get('page', 1) limit = settings.PAGE_SIZE # 每页显示的记录数 users = blog_models.User.objects.all().exclude( id=req.user.id).order_by('id') paginator = Paginator(users, limit) # 实例化一个分页对象 try: userObj = paginator.page(page) # 获取某页对应的记录 except PageNotAnInteger: # 如果页码不是个整数 userObj = paginator.page(1) # 取第一页的记录 except EmptyPage: # 如果页码太大,没有相应的记录 userObj = [] email_switch = lambda uid: blog_models.UserAttention.objects.get(guan_zhu=req.user.id, bei_guan_zhu=uid).email_notice or 0 \ if blog_models.UserAttention.objects.filter(guan_zhu=req.user.id, bei_guan_zhu=uid).exists() else 0 user_list = [{ 'portrait': '/static/images/%s/%s' % (user.userextend.portrait.img_category.name, user.userextend.portrait.src), 'nickname': user.userextend.nickname or u'暂无昵称', 'date_joined': str(user.date_joined)[0:10], 'level': get_user_level(user.id), 'fans': blog_models.UserAttention.objects.filter(bei_guan_zhu=user.id).count(), 'posts': blog_models.Post.objects.filter(author_id=user.id).count(), 'attention': 'yes' if blog_models.UserAttention.objects.filter( guan_zhu=req.user.id, bei_guan_zhu=user.id).exists() else 'no', 'id': user.id, 'email_notice': email_switch(user.id), 'username': user.username } for user in userObj] json_str = { 'user_list': user_list, 'next': int(page) + 1 if userObj and userObj.has_next() else 0, 'current': page } return response_json(json_str)
def makePostSummit(req): user = req.user content = req.POST.get('content') category = req.POST.get('category') title = req.POST.get('title') summary = req.POST.get('summary') if not all( [content.strip(), category.strip(), title.strip(), summary.strip()]): json_str = {'status': 0, 'msg': u'标题、概要、内容为必填项!'} return response_json(json_str) try: blog_models.Post.objects.create(content=content, author_id=user.id, img_id=set_post_img_random(), category_id=category, title=title, summary=summary) json_str = {'status': 1, 'msg': u'提交成功'} except Exception, exc: logger.error(exc, exc_info=True) json_str = {'status': 0, 'msg': u'提交异常,请给作者留言,谢谢!'}
title.strip(), summary.strip()]): json_str = {'status': 0, 'msg': u'标题、概要、内容为必填项!'} return response_json(json_str) try: blog_models.Post.objects.create(content=content, author_id=user.id, img_id=set_post_img_random(), category_id=category, title=title, summary=summary) json_str = {'status': 1, 'msg': u'提交成功'} except Exception, exc: logger.error(exc, exc_info=True) json_str = {'status': 0, 'msg': u'提交异常,请给作者留言,谢谢!'} return response_json(json_str) def up_down_share_post(req): action_type = req.GET.get('type') post_id = req.GET.get('post_id') user_id = req.user.id if action_type in ('up', 'down'): if not req.user.is_authenticated(): json_str = {'status': 0, 'msg': u'登录之后方可操作'} return response_json(json_str) if blog_models.ThumbUpDown.objects.filter( post_id=post_id, user_id=user_id, thumb_type=action_type).exists(): json_str = {'status': 0, 'msg': u'不可重复操作'} else: