Repository navigation
feat(profiles): setter CVs, shareable solve cards, embeddable problem widgets [wave F items 70-72] #353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(profiles): setter CVs, shareable solve cards, embeddable problem widgets [wave F items 70-72] #353
Changes from all commits
4e0ca8c
744cd32
1c01d2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # Generated by Django 5.2.17 on 2026-11-18 09:15 | ||
|
|
||
| import django.db.models.deletion | ||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("users", "0009_user_bio"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="SetterProfile", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.BigAutoField( | ||
| auto_created=True, | ||
| primary_key=True, | ||
| serialize=False, | ||
| verbose_name="ID", | ||
| ), | ||
| ), | ||
| ( | ||
| "blurb", | ||
| models.TextField(blank=True, max_length=600, verbose_name="blurb"), | ||
| ), | ||
| ( | ||
| "links", | ||
| models.JSONField( | ||
| blank=True, | ||
| default=list, | ||
| help_text="Up to 5 external links (strings).", | ||
| verbose_name="links", | ||
| ), | ||
| ), | ||
| ("updated_at", models.DateTimeField(auto_now=True)), | ||
| ( | ||
| "user", | ||
| models.OneToOneField( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="setter_profile", | ||
| to=settings.AUTH_USER_MODEL, | ||
| verbose_name="user", | ||
| ), | ||
| ), | ||
| ], | ||
| options={ | ||
| "verbose_name": "setter profile", | ||
| "verbose_name_plural": "setter profiles", | ||
| }, | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| """Setter CVs, solve cards and embeddable widgets (wave F items 70-72). | ||
|
|
||
| Endpoints (all public where noted): | ||
|
|
||
| - ``GET /users/{username}/setter-cv/`` the derived portfolio + profile | ||
| - ``PUT /users/{username}/setter-cv/`` owner edits blurb/links | ||
| - ``GET /cards/solve/{username}/{problem_id}/`` the solve-card payload | ||
| - ``GET /cards/solve/{username}/{problem_id}/svg`` the shareable SVG card | ||
| - ``GET /embed/problems/{problem_id}/`` the embeddable widget payload | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import html | ||
|
|
||
| from django.contrib.auth import get_user_model | ||
| from django.utils import timezone | ||
| from django.shortcuts import get_object_or_404 | ||
| from rest_framework import permissions, serializers, status | ||
| from rest_framework.response import Response | ||
| from rest_framework.views import APIView | ||
|
|
||
| from problems.models import Problem | ||
| from submissions.models import Submission | ||
|
|
||
| from .setter import setter_cv, solve_card | ||
|
|
||
| User = get_user_model() | ||
|
|
||
|
|
||
| class SetterProfileSerializer(serializers.Serializer): | ||
| blurb = serializers.CharField(max_length=600, required=False, allow_blank=True) | ||
| links = serializers.ListField( | ||
| child=serializers.URLField(), required=False, max_length=5 | ||
| ) | ||
|
|
||
|
|
||
| class SetterCVView(APIView): | ||
| """Item 70 — the public setter CV; owners PUT their blurb/links.""" | ||
|
|
||
| def get(self, request, username): | ||
| user = User.objects.filter(username=username).first() | ||
| if user is None: | ||
| return Response({"detail": "Not found."}, status=status.HTTP_404_NOT_FOUND) | ||
| return Response(setter_cv(user)) | ||
|
|
||
| def put(self, request, username): | ||
| user = User.objects.filter(username=username).first() | ||
| if user is None: | ||
| return Response({"detail": "Not found."}, status=status.HTTP_404_NOT_FOUND) | ||
| if user.id != request.user.id: | ||
| return Response( | ||
| {"detail": "Only the setter edits their CV."}, | ||
| status=status.HTTP_403_FORBIDDEN, | ||
| ) | ||
| payload = SetterProfileSerializer(data=request.data) | ||
| payload.is_valid(raise_exception=True) | ||
| from .models import SetterProfile | ||
|
|
||
| profile, _ = SetterProfile.objects.get_or_create(user=user) | ||
| if "blurb" in payload.validated_data: | ||
| profile.blurb = payload.validated_data["blurb"] | ||
| if "links" in payload.validated_data: | ||
| profile.links = payload.validated_data["links"] | ||
| profile.save() | ||
| return Response(setter_cv(user)) | ||
|
|
||
|
|
||
| class SolveCardView(APIView): | ||
| """Item 71 — the deterministic solve-card payload.""" | ||
|
|
||
| permission_classes = [permissions.AllowAny] | ||
|
|
||
| def get(self, request, username, problem_id): | ||
| user = User.objects.filter(username=username).first() | ||
| problem = Problem.objects.filter(pk=problem_id).first() | ||
| if user is None or problem is None: | ||
| return Response({"detail": "Not found."}, status=status.HTTP_404_NOT_FOUND) | ||
| return Response(solve_card(user, problem)) | ||
|
|
||
|
|
||
| class SolveCardSvgView(APIView): | ||
| """Item 71 — the shareable SVG render of the solve card.""" | ||
|
|
||
| permission_classes = [permissions.AllowAny] | ||
|
|
||
| def get(self, request, username, problem_id): | ||
| user = User.objects.filter(username=username).first() | ||
| problem = Problem.objects.filter(pk=problem_id).first() | ||
| if user is None or problem is None: | ||
| return Response({"detail": "Not found."}, status=status.HTTP_404_NOT_FOUND) | ||
| card = solve_card(user, problem) | ||
| # The viewer's own solve stays private: the SVG only renders for | ||
| # public facts (solved-on date + ordinal) — never code or feedback. | ||
| solved_date = ( | ||
| card["solved_at"].strftime("%Y-%m-%d") if card["solved_at"] else "" | ||
| ) | ||
|
Comment on lines
+95
to
+97
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For an existing user who has never solved the requested problem, this public endpoint still returns an SVG labeled Useful? React with 👍 / 👎. |
||
| title = html.escape(card["problem_title"][:60]) | ||
| handle = html.escape(card["username"][:30]) | ||
| svg = f"""<svg xmlns="http://www.w3.org/2000/svg" width="600" height="200" viewBox="0 0 600 200"> | ||
| <rect width="600" height="200" rx="16" fill="#0f172a"/> | ||
| <rect x="8" y="8" width="584" height="184" rx="12" fill="#1e293b"/> | ||
| <text x="32" y="58" fill="#38bdf8" font-family="sans-serif" font-size="22" font-weight="bold">SOLVED</text> | ||
| <text x="32" y="96" fill="#f8fafc" font-family="sans-serif" font-size="24" font-weight="bold">{handle}</text> | ||
| <text x="32" y="128" fill="#cbd5e1" font-family="sans-serif" font-size="18">{title}</text> | ||
| <text x="32" y="162" fill="#94a3b8" font-family="sans-serif" font-size="14">solver #{card["ordinal"]} · {solved_date} · webcoder</text> | ||
| </svg>""" | ||
|
Comment on lines
+97
to
+107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Suggested FixIn Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
| from django.http import HttpResponse | ||
|
|
||
| return HttpResponse(svg, content_type="image/svg+xml") | ||
|
|
||
|
|
||
| class EmbedProblemView(APIView): | ||
| """Item 72 — the embeddable problem-widget payload (statement summary | ||
| + sample I/O + a CTA link; never hidden tests or full statements of | ||
| contest-fresh problems).""" | ||
|
|
||
| permission_classes = [permissions.AllowAny] | ||
|
|
||
| def get(self, request, problem_id): | ||
| problem = get_object_or_404(Problem, pk=problem_id) | ||
|
Comment on lines
+120
to
+121
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For a draft or pending-approval problem that is not attached to an upcoming/running contest, this anonymous endpoint returns its title and sample inputs/outputs to anyone who supplies its ID. The existing visibility boundary in Useful? React with 👍 / 👎. |
||
| from contests.models import ContestProblem | ||
|
|
||
| live_contest = ContestProblem.objects.filter( | ||
| problem=problem, | ||
| contest__end_at__gt=timezone.now(), | ||
| ).exists() | ||
| if live_contest: | ||
| return Response( | ||
| {"detail": "This problem is not embeddable right now."}, | ||
|
Comment on lines
+120
to
+130
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Suggested FixEnforce problem visibility checks matching Prompt for AI AgentAlso affects:
Did we get this right? 👍 / 👎 to inform future reviews. |
||
| status=status.HTTP_403_FORBIDDEN, | ||
| ) | ||
| title = ( | ||
| problem.title_i18n.get("en", f"Problem {problem.id}") | ||
| if isinstance(problem.title_i18n, dict) | ||
| else f"Problem {problem.id}" | ||
| ) | ||
| samples = list( | ||
| problem.test_cases.filter(is_sample=True) | ||
| .order_by("order") | ||
| .values("input_data", "expected_output_data")[:3] | ||
| ) | ||
| solved = ( | ||
| Submission.objects.filter( | ||
| problem=problem, verdict=Submission.VerdictStatus.ACCEPTED | ||
| ) | ||
| .values_list("user_id") | ||
| .distinct() | ||
| .count() | ||
| ) | ||
| return Response( | ||
| { | ||
| "problem_id": problem.id, | ||
| "title": title, | ||
| "difficulty": problem.difficulty, | ||
| "time_limit_ms": problem.default_time_limit_ms, | ||
| "memory_limit_kb": problem.default_memory_limit_kb, | ||
| "samples": samples, | ||
| "solved_by": solved, | ||
| "solve_url": f"/problems/{problem.id}", | ||
| } | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| """Setter CVs + shareable solve cards (wave F items 70-71). | ||
|
|
||
| - ``setter_cv`` — the derived authoring portfolio (approved problems by | ||
| difficulty, editorial/article credits, contest-setting credits) plus the | ||
| author-editable ``SetterProfile`` blurb and links. | ||
| - ``solve_card`` — the deterministic solve-card payload rendered as an SVG | ||
| by the view (username, problem, solve ordinal, date). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from contests.models import ContestProblem | ||
| from problems.models import Article, CommunityEditorial, Problem | ||
|
|
||
|
|
||
| def setter_cv(user) -> dict: | ||
| """The public setter portfolio (item 70).""" | ||
| approved = Problem.objects.filter( | ||
| author=user, status=Problem.ProblemStatus.APPROVED | ||
| ) | ||
| by_difficulty = {"EASY": 0, "MEDIUM": 0, "HARD": 0} | ||
| for problem in approved: | ||
| if problem.difficulty in by_difficulty: | ||
| by_difficulty[problem.difficulty] += 1 | ||
|
Comment on lines
+21
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For setters who author beginner problems, the CV silently excludes all Useful? React with 👍 / 👎.
Comment on lines
+21
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Suggested FixInclude Prompt for AI Agent |
||
| editorials = CommunityEditorial.objects.filter( | ||
| author=user, status=CommunityEditorial.Status.APPROVED | ||
| ).count() | ||
| articles = Article.objects.filter( | ||
| author=user, status=Article.Status.APPROVED | ||
| ).count() | ||
| contest_ids = set( | ||
| ContestProblem.objects.filter( | ||
| problem__in=approved.values_list("id", flat=True) | ||
| ).values_list("contest_id", flat=True) | ||
| ) | ||
| profile = getattr(user, "setter_profile", None) | ||
| return { | ||
| "username": user.username, | ||
| "blurb": profile.blurb if profile else "", | ||
| "links": profile.links if profile else [], | ||
| "problems_approved": approved.count(), | ||
| "problems_by_difficulty": by_difficulty, | ||
| "community_editorials": editorials, | ||
| "articles": articles, | ||
| "contests_contributed": len(contest_ids), | ||
| "member_since": user.date_joined, | ||
| } | ||
|
|
||
|
|
||
| def solve_ordinal(problem_id: int) -> int: | ||
| """1-based solve ordinal of a problem (the Nth solver overall).""" | ||
| from submissions.models import Submission | ||
|
|
||
| return ( | ||
| Submission.objects.filter( | ||
| problem_id=problem_id, verdict=Submission.VerdictStatus.ACCEPTED | ||
| ) | ||
| .values_list("user_id") | ||
| .distinct() | ||
| .count() | ||
| ) | ||
|
Comment on lines
+51
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Suggested FixUpdate Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
|
|
||
|
|
||
| def solve_card(user, problem) -> dict: | ||
| """The deterministic share-card payload (item 71).""" | ||
| solved_at = None | ||
| from submissions.models import Submission | ||
|
|
||
| first = ( | ||
| Submission.objects.filter( | ||
| user=user, | ||
| problem=problem, | ||
| verdict=Submission.VerdictStatus.ACCEPTED, | ||
| ) | ||
| .order_by("submission_time") | ||
| .first() | ||
| ) | ||
| if first is not None: | ||
| solved_at = first.submission_time | ||
| title = ( | ||
| problem.title_i18n.get("en", f"Problem {problem.id}") | ||
| if isinstance(problem.title_i18n, dict) | ||
| else f"Problem {problem.id}" | ||
| ) | ||
| return { | ||
| "username": user.username, | ||
| "problem_id": problem.id, | ||
| "problem_title": title, | ||
| "difficulty": problem.difficulty, | ||
| "ordinal": solve_ordinal(problem.id), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Once two users have solved a problem, both cards display Useful? React with 👍 / 👎. |
||
| "solved_at": solved_at, | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug:
SolveCardViewandSolveCardSvgViewdo not enforce problem status or contest visibility checks, leaking metadata for non-public or contest problems to unauthenticated callers.Severity: MEDIUM
Suggested Fix
Filter problem queries to only allow
APPROVEDstatus (or owner-accessible problems for authenticated users) and ensure problems locked in active contests return a 404 response.Prompt for AI Agent
Also affects:
backend/users/profile_widgets.py:87~92