recognition: Recognition model w. grant_if_new; sky_save returns stargazer on first chart save — TDD

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Disco DeDisco
2026-04-22 02:13:29 -04:00
parent 83ce238a2f
commit be061f6bc2
5 changed files with 205 additions and 3 deletions

View File

@@ -2,7 +2,8 @@
sky_view — GET /dashboard/sky/ → renders sky template
sky_preview — GET /dashboard/sky/preview → proxies to PySwiss (no DB write)
sky_save — POST /dashboard/sky/save → saves natal data to User model
sky_save — POST /dashboard/sky/save → saves natal data to User model;
grants Stargazer Recognition on first save with real chart_data
"""
import json
@@ -11,6 +12,7 @@ from unittest.mock import patch, MagicMock
from django.test import TestCase
from django.urls import reverse
from apps.drama.models import Recognition
from apps.lyric.models import User
@@ -221,3 +223,68 @@ class SkyPreviewErrorPathTest(TestCase):
response = self.client.get(self.url, {"date": "1990-06-15", "lat": "51.5", "lon": "-0.1"})
self.assertEqual(response.status_code, 502)
_REAL_CHART = {
"planets": {"Sun": {"degree": 66.7, "sign": "Gemini", "retrograde": False}},
"houses": {"cusps": [180, 210, 240, 270, 300, 330, 0, 30, 60, 90, 120, 150]},
"elements": {"Fire": 1, "Stone": 2, "Air": 4, "Water": 0, "Time": 1, "Space": 1},
"aspects": [],
}
class SkySaveRecognitionTest(TestCase):
"""sky_save grants the Stargazer Recognition on the first save with real chart_data."""
def setUp(self):
self.user = User.objects.create(email="star@test.io")
self.client.force_login(self.user)
self.url = reverse("sky_save")
def _post(self, chart_data=_REAL_CHART):
return self.client.post(
self.url,
data=json.dumps({
"birth_dt": "1990-06-15T08:30:00",
"birth_lat": 51.5074,
"birth_lon": -0.1278,
"birth_place": "London, UK",
"birth_tz": "Europe/London",
"house_system": "O",
"chart_data": chart_data,
}),
content_type="application/json",
)
def test_first_save_with_chart_data_returns_stargazer_recognition(self):
data = self._post().json()
self.assertIn("recognition", data)
recog = data["recognition"]
self.assertEqual(recog["slug"], "stargazer")
self.assertIn("title", recog)
self.assertIn("description", recog)
self.assertIn("earned_at", recog)
def test_first_save_creates_recognition_in_db(self):
self._post()
self.assertEqual(Recognition.objects.filter(user=self.user, slug="stargazer").count(), 1)
def test_second_save_returns_null_recognition(self):
self._post()
data = self._post().json()
self.assertIsNone(data["recognition"])
def test_second_save_does_not_create_duplicate_recognition(self):
self._post()
self._post()
self.assertEqual(Recognition.objects.filter(user=self.user, slug="stargazer").count(), 1)
def test_save_with_empty_chart_data_does_not_grant_recognition(self):
data = self._post(chart_data={}).json()
self.assertIsNone(data["recognition"])
self.assertEqual(Recognition.objects.filter(user=self.user, slug="stargazer").count(), 0)
def test_save_with_null_chart_data_does_not_grant_recognition(self):
data = self._post(chart_data=None).json()
self.assertIsNone(data["recognition"])
self.assertEqual(Recognition.objects.filter(user=self.user, slug="stargazer").count(), 0)

View File

@@ -17,6 +17,7 @@ from django.views.decorators.csrf import ensure_csrf_cookie
from apps.applets.utils import applet_context, apply_applet_toggle
from apps.dashboard.forms import ExistingNoteItemForm, ItemForm
from apps.dashboard.models import Item, Note
from apps.drama.models import Recognition
from apps.epic.utils import _compute_distinctions
from apps.lyric.models import PaymentMethod, Token, User, Wallet
@@ -372,7 +373,19 @@ def sky_save(request):
'sky_birth_dt', 'sky_birth_lat', 'sky_birth_lon',
'sky_birth_place', 'sky_birth_tz', 'sky_house_system', 'sky_chart_data',
])
return JsonResponse({"saved": True})
recognition_payload = None
if user.sky_chart_data:
recog, created = Recognition.grant_if_new(user, "stargazer")
if created:
recognition_payload = {
"slug": recog.slug,
"title": "Stargazer",
"description": "You saved your first personal sky chart.",
"earned_at": recog.earned_at.isoformat(),
}
return JsonResponse({"saved": True, "recognition": recognition_payload})
@login_required(login_url="/")