def single(request, username, year, month, day, comment_form=None): """View a diary page of the user with username. Finds a diary entry by using the year month and day of it. If those fields are not specified, uses the current date. """ user = get_object_or_404(User, username=username) if user != request.user and not request.user.is_active: next_url = urlparams(reverse('users.inactive'), next=request.path) return HttpResponseRedirect(next_url) get_users_and_visits(user, request.user) c_year, c_month = clean_diary_date(year, month) c_day = smart_int(day) entries = user.diaries.get_for_month(visitor=request.user, creator=user, year=c_year, month=c_month) calendar_data = get_diary_calendar(entries, c_year, c_month) if c_day not in calendar_data['days_with_entries']: url = reverse('diary.list_all', args=[user.username]) return HttpResponseRedirect(url) entry = calendar_data['days_with_entries'][c_day] if not comment_form: comment_form = CommentForm(creator=request.user, diary=entry) data = {'entry': entry, 'calendar_data': calendar_data, 'comments': entry.comments.order_by('created'), 'diary': DiaryForm(creator=entry.creator, instance=entry), 'comment_form': comment_form, 'creator': user} return jingo.render(request, 'diary/single.html', data)
def list_personal(request, username, year=None, month=None): """Lists the diaries belonging to username. If the username is the name of the logged in user, it will display drafts and private. If it is another user's page it only displays non-draft, non-private entries. """ if username == 'me': return HttpResponseRedirect( reverse('diary.list_all', args=[request.user.username])) user = get_object_or_404(User, username=username) if user != request.user and not request.user.is_active: next_url = urlparams(reverse('users.inactive'), next=request.path) return HttpResponseRedirect(next_url) get_users_and_visits(user, request.user) c_year, c_month = clean_diary_date(year, month) today = datetime.datetime.today() current_year_month = True if (c_month == today.month and c_year == today.year) else False if year and month: entries = user.diaries.get_for_month(visitor=request.user, creator=user, year=c_year, month=c_month) else: entries = user.diaries.get_all(visitor=request.user, creator=user) entry_today_exists = diary_today_exists(entries) data = {'entries': entries, 'creator': user, 'calendar_data': get_diary_calendar(entries, c_year, c_month), 'entry_today_exists': entry_today_exists} # Do we display a new diary entry form or draft form? if (request.user == user and not entry_today_exists and current_year_month): if draft_today_exists(entries): form = DiaryForm(creator=entries[0].creator, instance=entries[0]) data['draft_exists'] = True else: form = DiaryForm(creator=request.user) data['diary_form'] = form return jingo.render(request, 'diary/list.html', data)
def edit(request, diary_id): """Edit an existing diary entry. Checks and adds errors for setting a non-draft diary to a draft diary. """ entry = get_object_or_404(DiaryEntry, pk=diary_id) if entry.creator != request.user: raise PermissionDenied was_draft = entry.is_draft if request.method == 'POST': data = request.POST if request.is_ajax(): data = data.copy() data['is_draft'] = True form = DiaryForm(creator=entry.creator, data=data, instance=entry) if form.is_valid(): entry = form.save() u = request.user entries = list(u.diaries.get_all(visitor=u, creator=u)[:10]) u.diaries.invalidate(*entries) # Publishing a diary entry. Notify favorites. if was_draft and not entry.is_draft: process_diary(entry) u.profile.update( coins=u.profile.coins + ucs.ACTIONS_COINS['diary']['daily']) update_rewards(request.user, 'diary') process_mojo_action(request.user, 'diary_entry') if request.is_ajax(): data = {'is_valid': True, 'timestamp': entry.updated.strftime("%s")} return JSONResponse(data) return HttpResponseRedirect(reverse( 'diary.single', kwargs=get_kwargs_for_diary_id(entry.pk))) else: form = DiaryForm(creator=entry.creator, instance=entry) c_year, c_month = clean_diary_date(entry.created_day.year, entry.created_day.month) entries = request.user.diaries.get_for_month(visitor=request.user, creator=request.user, year=c_year, month=c_month) calendar_data = get_diary_calendar(entries, c_year, c_month) data = {'form': form, 'creator': request.user, 'entry': entry, 'calendar_data': calendar_data} return jingo.render(request, 'diary/edit.html', data)
def new(request): """Create a new diary entry for today.""" diaries = request.user.diaries.filter(created_day=datetime.date.today()) # If they already have a diary for today, redirect them to edit if diaries: return HttpResponseRedirect(reverse('diary.edit', args=[diaries[0].pk])) if request.method == 'POST': data = request.POST if request.is_ajax(): data = data.copy() data['is_draft'] = True form = DiaryForm(creator=request.user, data=data) if form.is_valid(): entry = form.save() u = request.user entries = list(u.diaries.get_all(visitor=u, creator=u)[:10]) u.diaries.invalidate(*entries) # Register user to receive email when a comment is posted. NewCommentEvent.notify(request.user, entry) # Send news feed item to creator's favorites. if not entry.is_draft: process_diary(entry) u.profile.update( coins=u.profile.coins + ucs.ACTIONS_COINS['diary']['daily']) update_rewards(request.user, 'diary') process_mojo_action(request.user, 'diary_entry') if request.is_ajax(): data = {'is_valid': True, 'is_new': True, 'url': reverse('diary.edit', args=[entry.pk]), 'timestamp': entry.updated.strftime("%s")} return JSONResponse(data) return HttpResponseRedirect(reverse('diary.list_all', args=[request.user.username])) else: # request.method == 'GET' form = DiaryForm(creator=request.user) c_year, c_month = clean_diary_date() entries = request.user.diaries.get_for_month(visitor=request.user, creator=request.user, year=c_year, month=c_month) calendar_data = get_diary_calendar(entries, c_year, c_month) data = {'form': form, 'creator': request.user, 'calendar_data': calendar_data} return jingo.render(request, 'diary/new.html', data)