def add_post(request): status_code = status.HTTP_200_OK if request.method == 'POST': form = PostCreationForm(request.POST) if form.is_valid(): post = form.save(commit=False) post.author = request.user post.save() form.save_m2m() messages.success(request, 'Post has been added successfully!') form = PostCreationForm() status_code = status.HTTP_201_CREATED else: status_code = status.HTTP_422_UNPROCESSABLE_ENTITY else: post_type = request.GET.get('post_type', None) try: validate_post_type(post_type) except ValidationError: return error(request, error_dict={ 'title': 'Bad Request', 'body': '' }) form = PostCreationForm(initial={'type': post_type}) return render(request, 'blog/add_post.html', context={'form': form}, status=status_code)
def login(request): if request.user.is_authenticated: return redirect('dashboard-home') if request.method == 'POST': login_form_filled = LoginForm(request.POST) if login_form_filled.is_valid(): success_redirect_url = request.GET.get('next', 'dashboard-home') if not is_safe_url(url=success_redirect_url, allowed_hosts=settings.ALLOWED_HOSTS ): # TODO what is this allowed_hosts? return error(request, error_dict={ 'title': 'Invalid Request', 'body': '' }) email = login_form_filled.cleaned_data["email"] password = login_form_filled.cleaned_data["password"] user: CustomUser = authenticate( request, email=email, password=password) # TODO duplicate in form validation if not user.verified: messages.error(request, "Please verify your email first") else: builtin_login(request, user) logger = logging.getLogger('app.accounts.login') logger.info(f'{user} has logged into the site') return redirect(success_redirect_url) return render(request, 'users/login.html', {'form': login_form_filled}) else: login_form_empty = LoginForm() return render(request, 'users/login.html', {'form': login_form_empty})
def view_post(request, slug): try: post = Post.objects.get(slug=slug) context = {'post': post} if not request.user.is_expert: if Submission.objects.filter( student_profile=request.user.student_profile, post=post).exists(): last_uploaded_url = Submission.objects.get( student_profile=request.user.student_profile, post=post).uploaded_file.url context['last_uploaded_url'] = last_uploaded_url return render(request, 'blog/post.html', context=context) except ObjectDoesNotExist: return error(request, error_dict={ 'title': "Requested post doesn't exist", 'body': "" })
def view_all_posts(request): post_type = request.GET.get('post_type', None) try: validate_post_type(post_type) except ValidationError: return error(request, error_dict={ 'title': 'Bad Request', 'body': '' }) # TODO replace with error 404 post_type = int(post_type) post_category = "" for allowed_posts in Post.POST_TYPE: if post_type == allowed_posts[0]: post_category = f'{allowed_posts[1]}s' break return render(request, 'blog/posts.html', context={ 'post_type': post_type, 'post_category': post_category })
def dummy(request): if settings.DEBUG: return render(request, 'dummy.html') else: return error(request, error_dict={'title': 'Bad Request', 'body': ''})