diff --git a/src/apps/dashboard/migrations/0003_list_shared_with.py b/src/apps/dashboard/migrations/0003_list_shared_with.py new file mode 100644 index 0000000..38f740f --- /dev/null +++ b/src/apps/dashboard/migrations/0003_list_shared_with.py @@ -0,0 +1,20 @@ +# Generated by Django 6.0 on 2026-02-18 18:13 + +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('dashboard', '0002_list_owner'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.AddField( + model_name='list', + name='shared_with', + field=models.ManyToManyField(blank=True, related_name='shared_lists', to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/src/apps/dashboard/models.py b/src/apps/dashboard/models.py index 768b0ea..347b40f 100644 --- a/src/apps/dashboard/models.py +++ b/src/apps/dashboard/models.py @@ -10,6 +10,12 @@ class List(models.Model): on_delete=models.CASCADE, ) + shared_with = models.ManyToManyField( + "lyric.User", + related_name="shared_lists", + blank=True, + ) + @property def name(self): return self.item_set.first().text diff --git a/src/apps/dashboard/tests/test_views.py b/src/apps/dashboard/tests/test_views.py index 9b603eb..d9e2bae 100644 --- a/src/apps/dashboard/tests/test_views.py +++ b/src/apps/dashboard/tests/test_views.py @@ -1,7 +1,8 @@ import lxml.html +from unittest import skip from django.test import TestCase from django.utils import html -from unittest import skip + from ..forms import ( DUPLICATE_ITEM_ERROR, EMPTY_ITEM_ERROR, @@ -9,6 +10,7 @@ from ..forms import ( from ..models import Item, List from apps.lyric.models import User + class HomePageTest(TestCase): def test_uses_home_template(self): response = self.client.get('/') @@ -180,3 +182,22 @@ class MyListsTest(TestCase): response = self.client.get(f"/apps/dashboard/users/{user1.id}/") # assert 403 self.assertEqual(response.status_code, 403) + +class ShareListTest(TestCase): + def test_post_to_share_list_url_redirects_to_list(self): + our_list = List.objects.create() + alice = User.objects.create(email="alice@example.com") + response = self.client.post( + f"/apps/dashboard/{our_list.id}/share_list", + data={"recipient": "alice@example.com"}, + ) + self.assertRedirects(response, f"/apps/dashboard/{our_list.id}/") + + def test_post_with_email_adds_user_to_shared_with(self): + our_list = List.objects.create() + alice = User.objects.create(email="alice@example.com") + self.client.post( + f"/apps/dashboard/{our_list.id}/share_list", + data={"recipient": "alice@example.com"}, + ) + self.assertIn(alice, our_list.shared_with.all()) diff --git a/src/apps/dashboard/urls.py b/src/apps/dashboard/urls.py index 5718e11..8520160 100644 --- a/src/apps/dashboard/urls.py +++ b/src/apps/dashboard/urls.py @@ -5,4 +5,5 @@ urlpatterns = [ path('new_list', views.new_list, name='new_list'), path('/', views.view_list, name='view_list'), path('users//', views.my_lists, name='my_lists'), + path('/share_list', views.share_list, name="share_list"), ] diff --git a/src/apps/dashboard/views.py b/src/apps/dashboard/views.py index ead4ae2..2b46cb1 100644 --- a/src/apps/dashboard/views.py +++ b/src/apps/dashboard/views.py @@ -37,3 +37,9 @@ def my_lists(request, user_id): if request.user.id != owner.id: return HttpResponseForbidden() return render(request, "apps/dashboard/my_lists.html", {"owner": owner}) + +def share_list(request, list_id): + our_list = List.objects.get(id=list_id) + recipient = User.objects.get(email=request.POST["recipient"]) + our_list.shared_with.add(recipient) + return redirect(our_list) diff --git a/src/functional_tests/test_sharing.py b/src/functional_tests/test_sharing.py index 14256e9..5ff7520 100644 --- a/src/functional_tests/test_sharing.py +++ b/src/functional_tests/test_sharing.py @@ -36,7 +36,7 @@ class SharingTest(FunctionalTest): "friend@example.com", ) - list_page.share_list_with("friend@example.com") + list_page.share_list_with("alice@example.com") self.browser = ali_browser MyListsPage(self).go_to_my_lists_page("alice@example.com") diff --git a/src/templates/apps/dashboard/list.html b/src/templates/apps/dashboard/list.html index e583f5b..7a00162 100644 --- a/src/templates/apps/dashboard/list.html +++ b/src/templates/apps/dashboard/list.html @@ -11,6 +11,7 @@ {% block content %}
+ List created by: {{ list.owner.email }}
{% for item in list.item_set.all %} @@ -19,6 +20,35 @@
+ +
+
+ +
+ {% csrf_token %} + + {% if form.errors %} +
+ {{ form.errors.recipient.0 }} +
+ {% endif %} + + +
+ List shared with: + {% for user in list.shared_with.all %} + {{ user.email }} + {% endfor %} + +
+
{% endblock content %} {% block scripts %} diff --git a/src/templates/apps/dashboard/my_lists.html b/src/templates/apps/dashboard/my_lists.html index c3c55bc..1a4934c 100644 --- a/src/templates/apps/dashboard/my_lists.html +++ b/src/templates/apps/dashboard/my_lists.html @@ -9,4 +9,10 @@
  • {{ list.name }}
  • {% endfor %} +

    Lists shared with me

    +
      + {% for list in owner.shared_lists.all %} +
    • {{ list.name }}
    • + {% endfor %} +
    {% endblock content %}