def new(req): #new user msg = '' if req.method == 'GET': avatar_form = new_avatar_form() userAddForm = UserCreationForm() elif req.method == 'POST': userAddForm = UserCreationForm(req.POST) avatar_form = new_avatar_form(req.POST) if userAddForm.is_valid(): _a = avatar() _a.desc = req.POST['desc'] if req.FILES.has_key('avatar'): # 如果有上传图像 _img = req.FILES['avatar'] if _img.size < 50*1024: # 图片大小合适 userAddForm.save() _a.avatar = upload_and_replace('avatar',_img,str(userAddForm.instance.id)) else: # 图片过大 msg = "图片大小要小于50kb!" return rr('user/new.htm', {'userAddForm':userAddForm, 'avatar_form':avatar_form,'msg':msg, }, context_instance = RequestContext(req)) else: #没有上传图像,保存新建用户的请求,保存avatar。 userAddForm.save() _a.user = userAddForm.instance _a.save() return rd('/') return rr('user/new.htm', {'userAddForm':userAddForm, 'avatar_form':avatar_form,'msg':msg, }, context_instance = RequestContext(req))
def delete_reply(req,cid, tid, rid): if req.user.groups.all()[0].name == "admin": reply.objects.filter(id=rid).delete() else: pass return rd('/bbs/'+str(cid)+'/'+str(tid)+'/')
def change_avatar(req): msg = '' if req.method == 'GET': avatar_form = new_avatar_form() try: avatar_form.instance = req.user.get_profile() except: pass elif req.method == 'POST': avatar_form = new_avatar_form(req.POST) if avatar.objects.filter(user=req.user): _a = avatar.objects.filter(user=req.user)[0] else : _a = avatar() _a.user = req.user if req.FILES.has_key('avatar'): _img = req.FILES['avatar'] if _img.size < 50*1024: _a.avatar = upload_and_replace('avatar',_img,str(_a.user.id)) else: msg = "图片大小要小于50kb!" return rr('user/change_avatar.htm', {'avatar_form':avatar_form,'msg':msg, }, context_instance = RequestContext(req)) _a.desc = req.POST['desc'] _a.save() return rd('/') return rr('user/change_avatar.htm', {'avatar_form':avatar_form,'msg':msg, }, context_instance = RequestContext(req))
def untop_thread(req, cid, tid): if req.user.groups.all()[0].name == "admin": _thread = thread.objects.get(id=tid) _thread.is_top = False; _thread.save() else: pass return rd('/bbs/'+str(cid)+'/'+str(tid)+'/')
def open_thread(req, cid, tid): if req.user.groups.all()[0].name == "admin": _thread = thread.objects.get(id=tid) _thread.is_commentable = True; _thread.save() else: pass return rd('/bbs/'+str(cid)+'/'+str(tid)+'/')
def create(req): if req.method == 'GET': form = new_article_form() elif req.method == 'POST': form = new_article_form(req.POST) if form.is_valid(): form.instance.author = req.user form.save() return rd("/news/") return rr('news/create.html', {'form':form, }, context_instance = RequestContext(req))
def edit_thread(req, cid, tid): _t = thread.objects.get(id=tid) if _t.user.id == req.user.id or req.user.groups.all()[0].name == "admin": if req.method=='GET': return rr('bbs/edit_thread.html', {'thread':_t,'catid':cid,}, context_instance = RequestContext(req)) elif req.method =='POST': _t.title = req.POST['threadname'] _t.text = req.POST['threadtext'] _t.save() else: pass return rd('/bbs/'+str(cid)+'/'+str(tid)+'/')
def log_in(req): if req.method == 'GET': af = AuthenticationForm() elif req.method == 'POST': #form = AuthenticationForm(data=req.POST, request = request) if cookie_test else AuthenticationForm(data=request.POST) af = AuthenticationForm(data=req.POST) if af.is_valid(): user = User.objects.get(username = req.POST['username']) user.backend = 'django.contrib.auth.backends.ModelBackend' login(req, user) if req.POST.has_key('next'): return rd(req.POST['next']) return rd('/') if req.GET.has_key('next'): return rr('login.html', {'next':req.GET['next'], 'form':af, }, context_instance = RequestContext(req)) else: return rr('login.html', {'form':af, }, context_instance = RequestContext(req))
def edit(req, aid): if req.method == 'GET': _article = article.objects.get(id=aid) form = new_article_form(instance = _article) elif req.method == 'POST': form = new_article_form(req.POST) if form.is_valid(): _article = article.objects.get(id=aid) _article.title = req.POST['title'] _article.text = req.POST['text'] _article.save() return rd('/news/' + str(_article.id) + '/') return rr('news/edit.html', {'form':form, }, context_instance = RequestContext(req))
def articlecomment(req , aid): if req.method=='POST': _a = article.objects.get(id=aid) _c = comment() _c.text = req.POST['comment_text'] _c.nick = req.POST['nick_name'] _c.email = req.POST['email'] _c.save() _ac = article_comment() _ac._a = _a; _ac._c = _c; _ac.save() return rd('/news/' + str(aid) + '/')
def new(req, cid): cat = category.objects.get(id=cid) if req.method == 'GET': return rr('bbs/newthread.html', {'cat':cat,}, context_instance = RequestContext(req)) elif req.method == 'POST': _t = thread() _t.title = req.POST['threadname'] _t.text = req.POST['threadtext'] _t.user = req.user _t.posts = 0 _t.save() _ct = category_thread() _ct._t = _t _ct._c = cat _ct.save() return rd('/bbs/' + str(cid) + '/')
def reply_to_thread(req, cid, tid): if req.method == 'POST': _t = thread.objects.get(id=tid) _r = reply() _r.text = req.POST['replytext'] _r.user = req.user _r.floor = _t.posts + 1 _r.save() _t.posts = _t.posts + 1 _t.last_reply_time = _r.time _t.save() _tr = thread_reply() _tr._t = _t; _tr._r = _r; _tr.save() return rd('/bbs/'+str(cid)+'/'+str(tid)+'/')
def delete_thread(req, cid, tid): if req.user.groups.all()[0].name == "admin": thread.objects.filter(id=tid).delete() else: pass return rd('/bbs/'+str(cid)+'/')
def log_out(req): logout(req) return rd('/')
def delete(req, aid): article.objects.get(id=aid).delete() return rd("/news/")