def user_login(request): v = LoginValidator() f, result = v.validate(request) if not f: return ajax.ajax_fail(result) username = result['username'] password = result['password'] user = authenticate(username=username, password=password) if user is None: message = {'_':_('Authentication is failed!')} if '@' in username: # Mistakenly entered e-mail address instead of username? Look it up. try: user = User.objects.get(email=username) except User.DoesNotExist: message['username'] = _("Usernames cannot contain the '@' character.") else: message['username'] = _("Your e-mail address is not your username. Try '%s' instead.") % user.username return ajax.ajax_fail(message) # The user data is correct; log in the user in and continue. else: if user.is_staff: login(request, user) user.last_login = datetime.datetime.now() user.save() return ajax.ajax_ok(True, next=request.POST.get('next', setting('URLROOT') + '/'))
def user_book_edit(request, object_id, book_id=None): user = request.user v = BookValidator(request, book_id) flag , obj = v.validate_and_save(request) if flag: return ajax.ajax_ok(message=_('Success!')) return ajax.ajax_fail(obj,message=_('Fail!'))
def user_book_edit(request, object_id, book_id=None): user = request.user v = BookValidator(request, book_id) flag, obj = v.validate_and_save(request) if flag: return ajax.ajax_ok(message=_('Success!')) return ajax.ajax_fail(obj, message=_('Fail!'))
def user_chapter(request, object_id, book_id, chapter_id=None): if request.GET: o = Chapter.objects.get(pk=int(chapter_id)) result = { 'id': o.id, 'content': o.content, 'abstract': o.abstract, 'title': o.title, 'date': o.modifydate.strftime("%b %d,%Y %I:%m %p") } return ajax.ajax_ok(result) else: v = ChapterValidator(request, book_id, chapter_id) if request.POST: flag, obj = v.validate_and_save(request) if flag: return ajax.ajax_ok(message=_("Success!")) return ajax.ajax_fail(obj, message=_("There are some errors!")) else: user = User.objects.get(pk=int(object_id)) book = Book.objects.get(pk=int(book_id)) chapter = Chapter.objects.get(pk=int(chapter_id)) return render_template(request, 'users/user_chapter.html', { 'person': user, 'book': book, 'chapter': chapter })
def add(request, book_id=None): if book_id: try: book = Book.objects.get(pk=int(book_id)) except Book.DoesNotExist: return ajax_fail(message="此书在数据库中不存在!") url = book.url else: url = request.POST.get('url', '') if url: args = list(settings.PARAMS) + ['get', url] try: os.spawnl(*args) except: traceback.print_exc() return ajax_ok(message="任务已经提交!请稍候下载。") return ajax_fail(message="请输入想下载书的目录URL")
def add_comment(request, book_id, chapter_num): m = AddCommentValidator(request, book_id, chapter_num) f, obj = m.validate_and_save(request) if f: response = ajax.ajax_ok(message='ok') set_cookie(response, 'username', request.REQUEST.get('username', '')) set_cookie(response, 'email', request.REQUEST.get('email', '')) set_cookie(response, 'website', request.REQUEST.get('website', '')) return response return ajax.ajax_fail(obj, message='error')
def refresh(request, book_id): try: book = Book.objects.get(pk=int(book_id)) except Book.DoesNotExist: return ajax_fail(message="此书在数据库中不存在!") args = list(settings.PARAMS) + ['reget', book.url] try: os.spawnl(*args) except: traceback.print_exc() return ajax_ok(message="任务已经提交!请稍候下载。")
def user_edit(request, object_id): if request.POST: v = ChangeValidator(request, object_id) f, obj = v.validate_and_save(request) if f: return ajax.ajax_ok(message=_('User infomation updated successful!')) return ajax.ajax_fail(obj) else: user = User.objects.get(pk=int(object_id)) d = {'person':user} return render_template(request, 'users/user_edit.html', d)
def user_edit(request, object_id): if request.POST: v = ChangeValidator(request, object_id) f, obj = v.validate_and_save(request) if f: return ajax.ajax_ok( message=_('User infomation updated successful!')) return ajax.ajax_fail(obj) else: user = User.objects.get(pk=int(object_id)) d = {'person': user} return render_template(request, 'users/user_edit.html', d)
def user_register(request): v = RegisterValidator(request) if request.POST: f, obj = v.validate_and_save(request) if f: user = authenticate(username=obj.username, password=obj.password) if user and user.is_staff: login(request, user) user.last_login = datetime.datetime.now() user.save() return ajax.ajax_ok(True, next=setting('URLROOT') + '/') return ajax.ajax_fail(obj) else: return render_template(request, 'users/register.html')
def user_chapter(request, object_id, book_id, chapter_id=None): if request.GET: o = Chapter.objects.get(pk=int(chapter_id)) result = {'id':o.id, 'content':o.content, 'abstract':o.abstract, 'title':o.title, 'date':o.modifydate.strftime("%b %d,%Y %I:%m %p")} return ajax.ajax_ok(result) else: v = ChapterValidator(request, book_id, chapter_id) if request.POST: flag, obj = v.validate_and_save(request) if flag: return ajax.ajax_ok(message=_("Success!")) return ajax.ajax_fail(obj, message=_("There are some errors!")) else: user = User.objects.get(pk=int(object_id)) book = Book.objects.get(pk=int(book_id)) chapter = Chapter.objects.get(pk=int(chapter_id)) return render_template(request, 'users/user_chapter.html', {'person':user, 'book':book, 'chapter':chapter})
def user_book_addauthor(request, object_id, book_id): v = AddUserValidator(book_id) flag, obj = v.validate_and_save(request) if flag: return ajax.ajax_ok() return ajax.ajax_fail(obj)