def funfic_main_page(request, funfic_id): c = get_common_context(request) if request.method == 'POST': if request.POST.get('action') == 'rating': if request.user.is_authenticated(): try: mark = int(request.POST.get('mark')) mark_st = int(request.POST.get('mark_st')) mark_sz = int(request.POST.get('mark_sz')) mark_pr = int(request.POST.get('mark_pr')) Rating.add(request.user, Work.get_by_id(funfic_id), mark, mark_st, mark_sz, mark_pr) messages.success(request, u'Рейтинг успешно изменен.') except: messages.error(request, u'Введите корректные значения.') return HttpResponseRedirect('/funfic/%s/' % funfic_id) else: messages.error(request, u'Голосовать могут только авторизованные пользователи.') c['f'] = Work.get_by_id(funfic_id) c['funart'] = FunArt.get_by_work(c['f'])[:2] c['content'] = WorkPart.get_work_content(c['f']) c['f'].inc_vizits() c['its_mine'] = (c['f'].author == request.user) or (c['f'].is_collaborator(request.user)) if request.user.is_authenticated() else False c['recommend'] = Work.objects.all().order_by('-mark')[0:5] return render_to_response('funfic_main.html', c, context_instance=RequestContext(request))
def add_part_page(request, id): c = get_common_context(request) c['title'] = u'Добавить часть к фанфику' work = Work.get_by_id(id) if not ((work.author == request.user) or (work.is_collaborator(request.user))): return HttpResponseRedirect('/funfic/%s/' % id) c['parts'] = work.get_content() if request.method=='POST': form = AddPartForm(request.POST) if form.is_valid(): ff = form.save(commit=False) ff.work = work ff.number = int(request.POST['number']) + 1 ff.public_on = request.POST.get('public_on', False) ff.save() work.last_editing_time = datetime.datetime.now() messages.success(request, u'Часть успешно добавлена.') History.add_record(request.user, ff) return HttpResponseRedirect('/cabinet/edit_part/%s/%s/' % (id, ff.id)) else: messages.error(request, u'Часть не добавлен. Исправьте ошибки ниже.') else: form = AddPartForm() c['form'] = form c['is_adding'] = True return render_to_response('cabinet/add_part.html', c, context_instance=RequestContext(request))
def edit_page(request, id): c = get_common_context(request) f = Work.get_by_id(id) if not ((f.author == request.user) or (f.is_collaborator(request.user))): return HttpResponseRedirect('/funfic/%s/' % id) c['title'] = u'Редактировать фанфик' if request.method == 'POST': form = AddWorkForm(request.POST, instance=f) collaborator = None if request.POST.get('collaborator'): try: collaborator = UserProfile.objects.get(nick=request.POST.get('collaborator')).user except: messages.error(request, u'Необходимо ввести правильный ник соавтора.') if form.is_valid(): ff = form.save() if collaborator: ff.add_collaborator(collaborator) messages.success(request, u'Фанфик успешно отредактирован.') return HttpResponseRedirect('/cabinet/edit/%s/' % id) else: messages.error(request, u'Фанфик не отредактирован. Исправьте ошибки ниже.') print form.errors else: form = AddWorkForm(instance=f) c['form'] = form c['collaborator'] = f.get_collaborator() return render_to_response('cabinet/add.html', c, context_instance=RequestContext(request))
def my_funfic_page(request): c = get_common_context(request) c['title'] = u'Мои фанфики' funfics = Work.get_by_author(request.user) c['funfics_during'] = funfics.filter(status='during') c['funfics_frozen'] = funfics.filter(status='frozen') c['funfics_done'] = funfics.filter(status='done') return render_to_response('cabinet/my_funfic.html', c, context_instance=RequestContext(request))
def co_authorship_page(request): c = get_common_context(request) c['title'] = u'Фанфики в соавторстве' funfics = Work.get_by_collaborator(request.user) c['funfics_during'] = funfics.filter(status='during') c['funfics_frozen'] = funfics.filter(status='frozen') c['funfics_done'] = funfics.filter(status='done') return render_to_response('cabinet/my_funfic.html', c, context_instance=RequestContext(request))
def delete_page(request, id): c = get_common_context(request) f = Work.get_by_id(id) if not (f.author == request.user): return HttpResponseRedirect('/funfic/%s/' % id) else: f.delete() messages.success(request, u'Фанфик успешно Удалён.') return HttpResponseRedirect('/cabinet/my-funfic/')
def funfic_top_page(request, funfic_id): c = get_common_context(request) w = Work.get_by_id(funfic_id) if request.user.is_authenticated(): if PersonalTop.add(request.user, w): messages.success(request, u'Работа успешно добавлена в Ваш личный топ.') else: messages.error(request, u'Эта работа уже находится у Вас в личном топе.') else: messages.error(request, u'Для выполнения этого действия необходимо авторизироваться.') return HttpResponseRedirect('/funfic/%s/' % funfic_id)
def funfic_bookmark_page(request, funfic_id): c = get_common_context(request) w = Work.get_by_id(funfic_id) if request.user.is_authenticated(): if Bookmark.add(request.user, w): messages.success(request, u'Работа успешно добавлена в закладки.') else: messages.error(request, u'Эта работа уже находится у Вас в закладках.') else: messages.error(request, u'Для выполнения этого действия необходимо авторизироваться.') return HttpResponseRedirect('/funfic/%s/' % funfic_id)
def my_page_page(request): c = get_common_context(request) c['title'] = u'Личная страница' c['fun_count'] = Work.objects.filter(author=request.user).count() c['collab_count'] = request.user.collaborator.all().count() c['comm_count'] = Comment.objects.filter(user=request.user).count() funfics = Work.get_by_author(request.user) c['funfics_by_date'] = funfics.order_by('-last_editing_time')[:FUNCICS_COUNT_ON_PAGE] c['funfics_by_vizits'] = funfics.order_by('-vizits')[:FUNCICS_COUNT_ON_PAGE] c['funfics_by_comments'] = funfics.order_by('-commetns')[:FUNCICS_COUNT_ON_PAGE] c['funfics_by_rating'] = funfics.order_by('-mark')[:FUNCICS_COUNT_ON_PAGE] return render_to_response('cabinet/my_page.html', c, context_instance=RequestContext(request))
def funfic_review_add_page(request, funfic_id): c = get_common_context(request) c['f'] = Work.get_by_id(funfic_id) if request.method == 'POST': review = request.POST.get('text', None) if review: if request.user.is_authenticated(): Review(user=request.user, work=c['f'], name=request.POST.get('name', ''), text=request.POST.get('text', ''), ).save() messages.success(request, u'Ваша рецензия успешно добавлена.') else: messages.error(request, u'Для выполнения этого действия необходимо авторизироваться.') return HttpResponseRedirect('/funfic/%s/reviews/' % funfic_id) return render_to_response('funfic_review_add.html', c, context_instance=RequestContext(request))
def funfic_part_page(request, funfic_id, part_id): c = get_common_context(request) c['f'] = Work.get_by_id(funfic_id) c['f'].inc_vizits() if request.method == 'POST': comment = request.POST.get('comment', None) if comment: if request.user.is_authenticated(): Comment(user=request.user, work_part=WorkPart.get_by_id(part_id), text=comment).save() messages.success(request, u'Ваш комментарий успешно добавлен.') return HttpResponseRedirect('/funfic/%s/%s/' % (funfic_id, part_id)) else: messages.error(request, u'Для выполнения этого действия необходимо авторизироваться.') c['funart'] = FunArt.get_by_work(c['f'])[:2] c['content'] = WorkPart.get_work_content(c['f']) c['part'] = WorkPart.get_by_id(part_id) c['its_mine'] = (c['f'].author == request.user) or (c['f'].is_collaborator(request.user)) return render_to_response('funfic_part.html', c, context_instance=RequestContext(request))
def user_page(request, user_id): c = get_common_context(request) c['u'] = User.objects.get(id=user_id) c['u_profile'] = c['u'].get_profile if request.user.is_authenticated(): c['subscribed'] = Subscribe.has_subsribe(request.user, c['u']) c['is_me'] = (request.user == c['u']) c['fun_count'] = Work.objects.filter(author=request.user).count() c['collab_count'] = request.user.collaborator.all().count() c['comm_count'] = Comment.objects.filter(user=request.user).count() funfics = Work.get_by_author(c['u']) c['funfics_by_date'] = funfics.order_by('-last_editing_time')[:FUNCICS_COUNT_ON_PAGE] c['funfics_by_vizits'] = funfics.order_by('-vizits')[:FUNCICS_COUNT_ON_PAGE] c['funfics_by_comments'] = funfics.order_by('-commetns')[:FUNCICS_COUNT_ON_PAGE] c['funfics_by_rating'] = funfics.order_by('-mark')[:FUNCICS_COUNT_ON_PAGE] c['title'] = u'Автор - %s' % c['u'].get_profile().nick c['num_loop'] = ['#']+[i for i in range(10)] c['eng_loop'] = [chr(i) for i in xrange(ord('A'), ord('Z'))] c['rus_loop'] = [unichr(i) for i in xrange(ord(u'А'), ord(u'Я'))] return render_to_response('new_user.html', c, context_instance=RequestContext(request))
def edit_part_page(request, funfic_id, part_id): c = get_common_context(request) c['title'] = u'Редактирование части фанфика' work = Work.get_by_id(funfic_id) if not ((work.author == request.user) or (work.is_collaborator(request.user))): return HttpResponseRedirect('/funfic/%s/' % funfic_id) if request.method=='POST': form = AddPartForm(request.POST, instance=WorkPart.get_by_id(part_id)) if form.is_valid(): ff = form.save(commit=False) ff.public_on = request.POST.get('public_on', False) ff.save() work.last_editing_time = datetime.datetime.now() messages.success(request, u'Часть успешно изменена.') History.add_record(request.user, ff) return HttpResponseRedirect('/cabinet/edit_part/%s/%s/' % (funfic_id, part_id)) else: messages.error(request, u'Часть не добавлен. Исправьте ошибки ниже.') else: form = AddPartForm(instance=WorkPart.get_by_id(part_id)) c['form'] = form return render_to_response('cabinet/add_part.html', c, context_instance=RequestContext(request))
def statistics_page(request): import datetime c = get_common_context(request) c['title'] = u'Статистика аккаунта' if request.method == 'POST': try: date_from = datetime.datetime.strptime(request.POST.get('date_from', ''),'%d.%m.%Y') date_to = datetime.datetime.strptime(request.POST.get('date_to', ''),'%d.%m.%Y') except: messages.error(request, u'Необходимо выбрать даты') return HttpResponseRedirect('/cabinet/statistics/') if date_from > date_to: dd = date_from date_from = date_to date_to = dd funfics = Work.get_by_author(request.user) c['date_from'] = date_from.strftime('%d.%m.%Y') c['date_to'] = date_to.strftime('%d.%m.%Y') td = datetime.timedelta(days=1) result = [] curr = date_from while curr <= date_to: stat = [] for f in funfics: print curr, f.id, Review.get_count_by_work_date_range(f, curr, curr+td) curr_stat = { 'funfic': f, 'reviews': Review.get_count_by_work_date_range(f, curr, curr+td), 'comments': Comment.get_count_by_work_date_range(f, curr, curr+td), 'bookmarks': Bookmark.get_count_by_work_date_range(f, curr, curr+td), } curr_stat.update(Rating.get_count_by_work_date_range(f, curr, curr+td)) stat.append(curr_stat) result.append((curr.date(), stat)) curr = curr + td c['statistics'] = result return render_to_response('cabinet/statistics.html', c, context_instance=RequestContext(request))
def history_page(request): c = get_common_context(request) c['title'] = u'История изменений' c['my_works'] = Work.get_by_author(request.user) c['collab_works'] = request.user.collaborator.all() return render_to_response('cabinet/history.html', c, context_instance=RequestContext(request))
def funfic_reviews_page(request, funfic_id): c = get_common_context(request) c['f'] = Work.get_by_id(funfic_id) return render_to_response('funfic_reviews.html', c, context_instance=RequestContext(request))
def home_page(request): c = get_common_context(request) c['title'] = u'Главная' if request.method == "POST": c['registration_form'] = RegistrationForm(request.POST) if c['registration_form'].is_valid(): c['registration_form'].save() c['registration_form'].send_email(request) user_email = c['registration_form'].cleaned_data['email'] user_password = c['registration_form'].cleaned_data['password'] user = authenticate(username=user_email, password=user_password) login(request, user) messages.success(request, u'Вы успешно зарегистрировались.') return HttpResponseRedirect('/cabinet/settings/') else: c['open_registration'] = True #c['request_url'] = 'home' c['ratings'] = RATING c['sizes'] = SIZE c['statuses'] = STATUS c['fun_title'] = u'Новые Фанфики на сайте' c['active_page'] = 'home' if request.GET.get('act', '') == 's': category = request.GET.get('cat', None) fandom = request.GET.get('fan', None) size = request.GET.get('size', None) status = request.GET.get('status', None) rating = request.GET.get('rating', None) mark = request.GET.get('mark', None) try: genre_yes = [int(x) for x in request.GET.getlist('g_yes', [])] except: genre_yes = None try: genre_no = [int(x) for x in request.GET.getlist('g_no', [])] except: genre_no = None try: warning_yes = [int(x) for x in request.GET.getlist('w_yes', [])] except: warning_yes = None try: warning_no = [int(x) for x in request.GET.getlist('w_no', [])] except: warning_no = None funfics = Work.search(category, fandom, size, rating, status, mark, genre_yes, genre_no, warning_yes, warning_no) elif request.GET.get('act', '') == 'f': category_yes = [int(x) for x in request.GET.getlist('c_yes', [])] category_no = [int(x) for x in request.GET.getlist('c_no', [])] genre_yes = [int(x) for x in request.GET.getlist('g_yes', [])] genre_no = [int(x) for x in request.GET.getlist('g_no', [])] funfics = Work.get_by_category_and_genre(category_yes, category_no, genre_yes, genre_no) else: funfics = Work.objects.all() sort_par = request.GET.get('sort', 'time') if sort_par == 'vizits': funfics = funfics.order_by('-vizits') print 'v' elif sort_par == 'comments': funfics = funfics.order_by('-commetns') print 'c' elif sort_par == 'rating': funfics = funfics.order_by('-mark') print 'r' else: funfics = funfics.order_by('-last_editing_time') sort_par = 'time' c['sort'] = sort_par paginator = Paginator(funfics, FUNCICS_COUNT_ON_PAGE) page = int(request.GET.get('page', '1')) try: funfics = paginator.page(page) except PageNotAnInteger: page = 1 funfics = paginator.page(page) except EmptyPage: page = paginator.num_pages funfics = paginator.page(page) c['page'] = page c['page_range'] = paginator.page_range if len(c['page_range']) > 1: c['need_pagination'] = True c['old_args'] = request.META['QUERY_STRING'].replace('page', 'o').replace('sort', 'o') c['funfics'] = funfics return render_to_response('home.html', c, context_instance=RequestContext(request))