recipe links
This commit is contained in:
@@ -187,6 +187,44 @@ class RecipeSubrecipe(models.Model):
|
||||
return f'{self.recipe} includes {self.component}'
|
||||
|
||||
|
||||
class RecipeLink(models.Model):
|
||||
"""A non-compositional suggestion between two recipes owned by one user."""
|
||||
|
||||
class Relationship(models.TextChoices):
|
||||
GOES_WELL_WITH = 'goes_well_with', 'Goes well with'
|
||||
TRY_WITH = 'try_with', 'Try with'
|
||||
ALTERNATIVE_TO = 'alternative_to', 'Alternative to'
|
||||
|
||||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
|
||||
related_name='recipe_links')
|
||||
source = models.ForeignKey(Recipe, on_delete=models.CASCADE, related_name='outgoing_links')
|
||||
target = models.ForeignKey(Recipe, on_delete=models.CASCADE, related_name='incoming_links')
|
||||
relationship = models.CharField(max_length=24, choices=Relationship.choices,
|
||||
default=Relationship.GOES_WELL_WITH)
|
||||
note = models.CharField(max_length=255, blank=True)
|
||||
reciprocal = models.BooleanField(default=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ('created_at', 'id')
|
||||
constraints = [
|
||||
models.UniqueConstraint(fields=('owner', 'source', 'target'), name='recipes_unique_link_direction'),
|
||||
models.CheckConstraint(condition=~Q(source=models.F('target')), name='recipes_link_not_self'),
|
||||
]
|
||||
|
||||
def clean(self):
|
||||
super().clean()
|
||||
if self.source_id and self.target_id:
|
||||
if self.source_id == self.target_id:
|
||||
raise ValidationError({'target': 'A recipe cannot link to itself.'})
|
||||
if self.source.owner_id != self.owner_id or self.target.owner_id != self.owner_id:
|
||||
raise ValidationError({'target': 'Choose one of your own recipes.'})
|
||||
if self.relationship not in self.Relationship.values:
|
||||
raise ValidationError({'relationship': 'Choose a supported relationship.'})
|
||||
|
||||
|
||||
class RecipeIngredientSubstitution(models.Model):
|
||||
ingredient = models.ForeignKey(RecipeIngredient, on_delete=models.CASCADE,
|
||||
related_name='substitutions')
|
||||
|
||||
Reference in New Issue
Block a user