def test_setup_site_check() -> None: """ Verify that the bootstrap command must be run. Because the bootstrap command auto-runs on every test, we have to manually fake that it _hasn't_ run. """ Post.objects.all().delete() with pytest.raises(ImproperlyConfigured): get_additional_context({})
def test_bootstrap_check(settings: SettingsWrapper) -> None: """ Verify that the bootstrap command must be run. Because the bootstrap command auto-runs on every test, we have to manually fake that it _hasn't_ run. """ Post.objects.all().delete() settings.ENVIRONMENT = "prod" with pytest.raises(ImproperlyConfigured): get_additional_context({})
def get(self, request: Request, *args: object, **kwargs: object) -> HttpResponse: """Retrieve the rendered login form.""" form = LoginForm() context = get_additional_context({"form": form, "slim_form": True}) if next_path := request.GET.get("next", None): context.update({"next": next_path})
def get(self, request: HttpRequest, submission_id: int) -> HttpResponse: """Render the transcription view with data from an existing transcription.""" submission = get_object_or_404(Submission, id=submission_id) transcription = submission.transcription_set.filter(author=request.user).first() if not transcription: messages.error( request, f"Cannot find transcription for submission {submission.id} by you!", ) return redirect("choose_transcription") context = get_additional_context({"fullwidth_view": True}) context.update({"submission": submission}) context.update({"transcription_templates": get_and_format_templates()}) context.update({"edit_mode": True}) context = update_context_with_proxy_data(submission, context) if s_id := request.session.get("submission_id"): if s_id == submission_id: # it's for the submission we're currently working on. context = update_context_with_session_data(request, context) else: # it's old data. Nuke it. remove_old_session_data(request)
def get(self, request: HttpRequest, *args: object, **kwargs: object) -> HttpResponse: """ Build and render the page for adding a new post. This applies to both main site and the engineering blog. """ context = { "form": PostAddForm(), "header": "Add a new post!", "subheader": ('Remember to toggle "Published" if you want your post to appear!' ), # enable the WYSIWYG editor "enable_trumbowyg": True, # id of html element we want to attach the trumbowyg to "trumbowyg_target": "id_body", "fullwidth_view": True, } context = get_additional_context(context) return render(request, "website/generic_form.html", context)
def handler500(request: HttpRequest) -> HttpResponse: """View to handle 500 errors.""" context = get_additional_context({ "error_message": ("Something went wrong and the site broke. Try your action again?") }) return render(request, "website/error.html", context)
def handler404(request: HttpRequest, exception: Any) -> HttpResponse: """View to handle 404 errors.""" context = get_additional_context({ "error_message": ("Hm... that page doesn't seem to exist. Try a different link?") }) return render(request, "website/error.html", context)
def accept_coc(request: HttpRequest) -> HttpResponse: """Show the Code of Conduct and ask the volunteer to accept it.""" if request.method == "POST": request.user.accepted_coc = True request.user.save() return redirect(reverse("choose_transcription") + "?show_tutorial=1") else: return render(request, "app/accept_coc.html", get_additional_context())
def get_context_data(self, **kwargs: object) -> Dict: """Build the context dict with extra data needed for the templates.""" context = super().get_context_data(**kwargs) context.update({ "enable_trumbowyg": True, # id of html element we want to convert "trumbowyg_target": "id_body", "fullwidth_view": True, }) context = get_additional_context(context) return context
def index(request: HttpRequest) -> HttpResponse: """Render the homepage for the engineering posts.""" posts = Post.objects.filter( published=True, engineeringblogpost=True, standalone_section=False, show_in_news_view=True, ).order_by("-date") context = get_additional_context({"posts": posts}) return render(request, "website/index.html", context)
def view_previous_transcriptions(request: HttpRequest) -> HttpResponse: """Show the user their latest transcriptions so that they can edit them if needed.""" transcriptions = ( Transcription.objects.annotate(original_id_len=Length("original_id")) .filter( author=request.user, original_id_len__lt=14, submission__title__isnull=False ) .order_by("-create_time")[:25] ) context = get_additional_context( {"transcriptions": transcriptions, "fullwidth_view": True} ) return render(request, "app/view_transcriptions.html", context)
def get( # noqa: C901 self, request: HttpRequest, submission_id: int ) -> HttpResponse: """Provide the transcription view.""" drf_request = convert_to_drf_request( request, data={"username": request.user.username} ) viewset = SubmissionViewSet() # prepare the viewset for handling this request viewset.request = drf_request # I don't know why this isn't autopopulated, but if we don't set it here then # it explodes. viewset.format_kwarg = None response = viewset.claim(drf_request, submission_id) submission = get_object_or_404(Submission, id=submission_id) if response.status_code == status.HTTP_423_LOCKED: messages.error( request, "There is a problem with your account. Please contact the mods.", ) return redirect("logout") if response.status_code == status.HTTP_409_CONFLICT: if submission.claimed_by != request.user: # only actually error if it's not claimed by us messages.error( request, "Sorry, that submission was claimed by someone else. Try grabbing a" " different one!", ) return redirect("choose_transcription") if response.status_code == 460: messages.error( request, "Sorry -- you need to complete the submissions already assigned to you" " before you take on more.", ) return redirect("choose_transcription") flair_post(Submission.objects.get(id=submission_id), Flair.in_progress) context = get_additional_context({"fullwidth_view": True}) context.update({"submission": submission}) context.update({"edit_mode": False}) context.update({"transcription_templates": get_and_format_templates()}) context = update_context_with_proxy_data(submission, context) # if they posted a bad transcription and we want them to fix it, grab # the stored values and add them to the current context, then delete them # from the session so they don't get accidentally shown twice. if s_id := request.session.get("submission_id"): if s_id == submission_id: # it's for the submission we're currently working on. context = update_context_with_session_data(request, context) else: # it's old data. Nuke it. remove_old_session_data(request)
def choose_transcription(request: HttpRequest) -> HttpResponse: """Provide a user with transcriptions to choose from.""" time_delay = timezone.now() - timedelta( hours=settings.OVERRIDE_ARCHIVIST_DELAY_TIME if settings.OVERRIDE_ARCHIVIST_DELAY_TIME else settings.ARCHIVIST_DELAY_TIME ) submissions = Submission.objects.annotate( original_id_len=Length("original_id") ).filter( original_id_len__lt=10, completed_by=None, claimed_by=None, create_time__gte=time_delay, removed_from_queue=False, archived=False, ) options_list = [obj for obj in submissions if obj.is_image] if len(options_list) > 3: # we have more to choose from, so let's grab 3 temp = [] for _ in range(3): submission = random.choice(options_list) temp.append(options_list.pop(options_list.index(submission))) options = temp else: options = options_list for submission in options: # todo: add check for source. This will need to happen after we convert # sources to the individual subreddits if not submission.title: # we have a lot of submissions that don't have a title, but we # need it for this next page. Just grab the title and save it. post = request.user.reddit.submission(url=submission.url) submission.title = post.title submission.nsfw = post.over_18 submission.save(skip_extras=True) context = get_additional_context({"options": options, "fullwidth_view": True}) if len(options) == 0: completed_post_count = ( Submission.objects.annotate(original_id_len=Length("original_id")) .filter( original_id_len__lt=10, completed_by__isnull=False, create_time__gte=time_delay, ) .count() ) if completed_post_count == 0: # if it's not zero, then we cleared the queue and will show that page instead. context.update({"show_error_page": True}) claimed_submissions = Submission.objects.filter( claimed_by=request.user, archived=False, completed_by__isnull=True ) context.update({"claimed_submissions": claimed_submissions}) if request.user.ranked_up: ending = random.choice(EXCITEMENT) messages.success(request, f"You ranked up to {request.user.get_rank}! {ending}") context.update({"show_confetti": True}) return render(request, "app/choose_transcription.html", context)
def get_context_data(self, **kwargs: object) -> Dict: """Build the context dict with extra data needed for the templates.""" context = super().get_context_data(**kwargs) context = get_additional_context(context) return context
def get(self, request: HttpRequest, *args: object, **kwargs: object) -> HttpResponse: """Render the admin view.""" context = {"posts": Post.objects.all()} context = get_additional_context(context) return render(request, "website/admin.html", context)