diff --git a/propositions/migrations/0006_proposition_profile_to_user.py b/propositions/migrations/0006_proposition_profile_to_user.py new file mode 100644 index 0000000..d0ebd6a --- /dev/null +++ b/propositions/migrations/0006_proposition_profile_to_user.py @@ -0,0 +1,53 @@ +# Generated by Django 2.2.9 on 2020-01-05 16:28 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +def move_profile_to_user(apps, schema_editor): + Proposition = apps.get_model("propositions", "Proposition") + for proposition in Proposition.objects.all(): + proposition.user = proposition.profile.user + proposition.save() + + +def move_user_to_profile(apps, schema_editor): + # One should do something similar to ``move_profile_to_user`` AND make the + # ``profile`` field temporarily nullable in the operations below. + # => Grosse flemme + raise NotImplementedError("Who uses migrations backwards anyway?") + + +class Migration(migrations.Migration): + + dependencies = [ + ("propositions", "0005_remove_nb_yes_no_fields"), + ] + + operations = [ + migrations.RenameField( + model_name="proposition", old_name="user", new_name="profile" + ), + migrations.AddField( + model_name="proposition", + name="user", + field=models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to=settings.AUTH_USER_MODEL, + verbose_name="Proposé par", + null=True, + ), + ), + migrations.RunPython(move_profile_to_user, move_user_to_profile), + migrations.RemoveField(model_name="proposition", name="profile"), + migrations.AlterField( + model_name="proposition", + name="user", + field=models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to=settings.AUTH_USER_MODEL, + verbose_name="Proposé par", + ), + ), + ] diff --git a/propositions/models.py b/propositions/models.py index 0ff1f74..1c6dcfa 100644 --- a/propositions/models.py +++ b/propositions/models.py @@ -1,6 +1,5 @@ from django.contrib.auth import get_user_model from django.db import models -from gestion.models import ErnestoUser User = get_user_model() @@ -8,9 +7,7 @@ User = get_user_model() class Proposition(models.Model): name = models.CharField(max_length=100, verbose_name="nom du morceau") artist = models.CharField(blank=True, max_length=100) - user = models.ForeignKey( - ErnestoUser, on_delete=models.CASCADE, verbose_name="Proposé par" - ) + user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="Proposé par") link = models.URLField(blank=True) def __str__(self): diff --git a/propositions/tests.py b/propositions/tests.py index b8493f2..087feeb 100644 --- a/propositions/tests.py +++ b/propositions/tests.py @@ -43,7 +43,7 @@ class PropositionCreateTest(TestCase): self.assertEqual(proposition.name, "foo") self.assertEqual(proposition.artist, "bar") self.assertEqual(proposition.link, "http://example.com") - self.assertEqual(proposition.user, user.profile) + self.assertEqual(proposition.user, user) class PropositionDeleteTest(TestCase): @@ -54,7 +54,7 @@ class PropositionDeleteTest(TestCase): self.chef.profile.is_chef = True self.chef.profile.save() - proposition = Proposition.objects.create(name="prop", user=self.owner.profile) + proposition = Proposition.objects.create(name="prop", user=self.owner) self.url = reverse("propositions:delete", args=(proposition.id,)) def test_anonymous_cannot_get(self): @@ -110,10 +110,10 @@ class PropositionListTest(TestCase): def setUp(self): self.user = new_user("toto") for name in ["foo", "bar", "baz"]: - p = Proposition.objects.create(name=name, user=self.user.profile) + p = Proposition.objects.create(name=name, user=self.user) Answer.objects.create(proposition=p, user=self.user, answer=Answer.YES) for name in ["oof", "rab", "zab"]: - p = Proposition.objects.create(name=name, user=self.user.profile) + p = Proposition.objects.create(name=name, user=self.user) Answer.objects.create(proposition=p, user=self.user, answer=Answer.NO) def test_anonymous_cannot_get(self): @@ -131,7 +131,7 @@ class PropositionListTest(TestCase): class ReponseTest(TestCase): def setUp(self): self.user = new_user("toto") - self.prop = Proposition.objects.create(name="foo", user=self.user.profile) + self.prop = Proposition.objects.create(name="foo", user=self.user) def _url(self, rep): assert rep in Answer.REP_CHOICES diff --git a/propositions/views.py b/propositions/views.py index 13251f1..6947afb 100644 --- a/propositions/views.py +++ b/propositions/views.py @@ -17,7 +17,7 @@ class PropositionCreate(LoginRequiredMixin, CreateView): def form_valid(self, form): proposition = form.save(commit=False) - proposition.user = self.request.user.profile + proposition.user = self.request.user proposition.save() return HttpResponseRedirect(self.success_url) @@ -44,7 +44,7 @@ class PropositionDelete(LoginRequiredMixin, UserPassesTestMixin, DeleteView): def test_func(self): proposition = self.get_object() user = self.request.user - return (proposition.user == user.profile or user.profile.is_chef) + return (proposition.user == user or user.profile.is_chef) @login_required