def add(request): post = {} if not request.session.get("user"): messages.info(request, "登录后创建主题!") return redirect("login") if request.method == "POST": post = request.POST.copy() form = TopicForm(post) if not form.is_valid(): for field in form.errors: messages.info(request, form[field].errors.as_text()) else: cd = form.cleaned_data if getattr(request.session["user"], "time", 0) < settings.TOPIC_CREATE: messages.info(request, "时间不足%s分钟!" % settings.TOPIC_CREATE) return redirect("/") bank = Bank.objects.get(id=1) bank.time += settings.TOPIC_CREATE bank.save() request.session["user"].time -= settings.TOPIC_CREATE request.session["user"].save() topic = Topic() topic.author_id = getattr(request.session["user"], "id", 0) topic.title = cd["title"] topic.text = cd["text"] topic.node_id = cd["nodename"] topic.date = int(time.time()) topic.save() bill = Bill( author=request.session["user"], time=settings.TOPIC_CREATE, type=2, date=int(time.time()), topic=topic, balance=getattr(request.session["user"], "time", 0), ) bill.save() messages.info(request, "主题创建成功!") return redirect("/") form = TopicForm( initial={ "title": request.POST.get("title", ""), "text": request.POST.get("text", ""), "nodename": request.POST.get("nodename", "0"), } ) return render_to_response("topic_add.html", locals(), context_instance=RequestContext(request))
def register(request, inviter = ''): #print inviter if request.method == 'POST': post = request.POST.copy() inviter = post.get('inviter', '') del post['inviter'] form = RegisterForm(post) if not form.is_valid(): for field in form.errors: messages.info(request, form[field].errors.as_text()) else: cd = form.cleaned_data #print set_password(cd['password1']) user_time = settings.USER_INVITER_TIME if inviter else settings.USER_DEFAULT_TIME user = User(name=cd['username'], usercenter=cd['username'], \ email=cd['email'], password=set_password(cd['password1']), time=user_time, date=int(time.time())) user.save() #init bank if Bank.objects.all().count() == 0: b = Bank(time=settings.BANK_INIT_TIME) b.save() bank = Bank.objects.get(id=1) gift_time = settings.USER_INVITER_TIME - settings.USER_DEFAULT_TIME giver = None try: giver = User.objects.get(name=inviter) except Exception, e: print e if giver: giver.time += gift_time giver.save() bank.time -= settings.USER_DEFAULT_TIME + gift_time userBill = Bill(author=user,time=settings.USER_INVITER_TIME,type=7,balance=user.time,date=int(time.time()),user_id=giver.id) giverBill = Bill(author=giver,time=gift_time,type=8,balance=giver.time,date=int(time.time()),user_id=user.id) giverBill.save() else: bank.time -= settings.USER_DEFAULT_TIME userBill = Bill(author=user,time=settings.USER_DEFAULT_TIME,type=1,balance=user.time,date=int(time.time())) userBill.save() bank.save() messages.info(request, 'You have successfully registered!') return redirect(login)
user = request.session.get('user') cd = form.cleaned_data reply = Reply(topic=topic, author=user, text=cd['content'], date=int(time.time())) reply.save() user.time -= settings.TOPIC_REPLY user.save() topic.author.time += settings.TOPIC_REPLY topic.author.save() topic.last_reply_date = int(time.time()) topic.save() #for reader in topic.readers: # topic.readers.remove(reader) t = int(time.time()) if user.id != topic.author.id: reply_bill = Bill(author=user,time=settings.TOPIC_REPLY,type=3,date=t,reply=reply,user_id=topic.author.id,balance=user.time) reply_bill.save() author_bill = Bill(author=topic.author,time=settings.TOPIC_REPLY,type=5,date=t,reply=reply,user_id=user.id,balance=topic.author.time) author_bill.save() author_notify = Notify(author=topic.author, topic=topic, reply=reply, type=1, date=t) author_notify.save() for username in mentions(cd['content']): if username: if username != topic.author.name: print 'username:'******'topicview', topic.id) def reply_del(request, reply_id):