def message_add(request): is_anon = request.user.is_anonymous if is_anon: return HttpResponseRedirect('/login/') is_super = not is_anon and request.user.is_superuser previewed = False content = title = '' if is_super: cls = StickyDiaryForm else: cls = DiaryForm if request.POST: form = cls(request.POST) if form.is_valid(): content = form.cleaned_data['content'] previewed = True # do the save diary = models.DiaryEntry() diary.content = content diary.user = request.user diary.actor = request.user diary.title = form.cleaned_data['title'] if is_super: diary.sticky = form.cleaned_data.get('sticky', False) diary.created = datetime.datetime.utcnow() diary.save() desc, truncated = summarise(diary.content) log_event( type='new-thread', user=request.user, target=diary, title=diary.title, id=diary.id, description=desc, description_truncated=truncated, ) messages.success(request, 'Entry saved!') return HttpResponseRedirect(f'/d/{diary.id}/') else: form = cls() return render(request, 'message_add.html', { 'previewed': previewed, 'content': content, 'title': title, 'form': cls, })
def entry_add(request, challenge_id): challenge = get_object_or_404(models.Challenge, pk=challenge_id) if not challenge.isRegoOpen(): return HttpResponseRedirect(f"/{challenge_id}/") if challenge.isCompFinished(): messages.error(request, 'Entry registration closed') return HttpResponseRedirect(f"/{challenge_id}/") if request.method == 'POST': f = AddEntryForm(request.POST) f.current_user = request.user.username if f.is_valid(): entry = f.instance entry.challenge = challenge entry.user = request.user members = f.cleaned_data['users'] entry.save() entry.users.set(models.User.objects.filter(username__in=members)) short_description, truncated = summarise(entry.description) log_event( type='new-entry', challenge=entry.challenge.number, team=entry.title, members=list(members), name=entry.name, description=short_description, description_truncated=truncated, ) messages.success(request, 'Entry created!') return HttpResponseRedirect(f"/e/{entry.name}/") else: f = AddEntryForm(initial={'users': request.user.username}) return render(request, 'challenge/entry_add.html', { 'challenge': challenge, 'form': f, 'is_member': True, 'is_owner': True, } )
def entry_diary(request, entry_id): is_anon = request.user.is_anonymous if is_anon: return HttpResponseRedirect('/login/') entry = get_object_or_404(models.Entry, pk=entry_id) is_member = request.user in entry.users.all() if not is_member: messages.error(request, "You're not allowed to add diary entries!") return HttpResponseRedirect(f'/e/{entry_id}/') challenge = entry.challenge is_super = not is_anon and request.user.is_superuser previewed = False content = title = '' if is_super: cls = StickyDiaryForm else: cls = DiaryForm if request.POST: form = cls(request.POST) if form.is_valid(): content = form.cleaned_data['content'] previewed = True # do the save diary = models.DiaryEntry() diary.entry = entry diary.challenge = challenge diary.content = content diary.user = request.user diary.actor = request.user diary.title = form.cleaned_data['title'] diary.created = datetime.datetime.utcnow() if is_super: diary.sticky = form.cleaned_data.get('sticky', False) diary.save() desc, truncated = summarise(diary.content) log_event( type='new-diary', user=request.user, target=diary, title=diary.title, entry={ 'title': diary.entry.display_title, 'name': diary.entry.name, }, description=desc, description_truncated=truncated, ) messages.success(request, 'Entry saved!') return HttpResponseRedirect(f'/d/{diary.id}/') else: form = cls() return render( request, 'challenge/add_diary.html', { 'previewed': previewed, 'content': content, 'title': title, 'form': form, 'challenge': challenge, 'entry': entry, 'diary_entries': entry.diaryentry_set.all(), 'is_member': True, 'is_owner': entry.user == request.user, })