from django.db import models
from django.urls import reverse
from datetime import date
from django.contrib.auth.models import User                       #! 1 - Import user models

MEALS = (
    ('B', 'Breakfast'),
    ('L', 'Lunch'),
    ('D', 'Dinner')
)

class Toy(models.Model):
  name = models.CharField(max_length=50)
  color = models.CharField(max_length=20)

  def __str__(self):
    return self.name

  def get_absolute_url(self):
    return reverse('toys_detail', kwargs={'pk': self.id})

class Cat(models.Model):
  name = models.CharField(max_length=100)
  breed = models.CharField(max_length=100)
  description = models.TextField(max_length=250)
  age = models.IntegerField()
  toys = models.ManyToManyField(Toy)
  user = models.ForeignKey(User, on_delete=models.CASCADE)          #+ 1.1 - Add user as ForeignKey (URL mapping, we use this to point to our class based views)

  def __str__(self):
    return self.name

  def get_absolute_url(self):
    return reverse('detail', kwargs={'cat_id': self.id})

  def fed_for_today(self):
    return self.feeding_set.filter(date=date.today()).count() >= len(MEALS)

class Feeding(models.Model):
  date = models.DateField('feeding date')
  meal = models.CharField(
    max_length=1,
    choices=MEALS,
    default=MEALS[0][0]
  )
  cat = models.ForeignKey(Cat, on_delete=models.CASCADE)

  def __str__(self):
    # Nice method for obtaining the friendly value of a Field.choice
    return f"{self.get_meal_display()} on {self.date}"

  # change the default sort
  class Meta:
    ordering = ['-date']

class Photo(models.Model):
  url = models.CharField(max_length=200)
  cat = models.ForeignKey(Cat, on_delete=models.CASCADE)

  def __str__(self):
    return f"Photo for cat_id: {self.cat_id} @{self.url}"
[((291, 322), 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(50)'}), '(max_length=50)\n', (307, 322), False, 'from django.db import models\n'), ((333, 364), 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(20)'}), '(max_length=20)\n', (349, 364), False, 'from django.db import models\n'), ((532, 564), 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(100)'}), '(max_length=100)\n', (548, 564), False, 'from django.db import models\n'), ((575, 607), 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(100)'}), '(max_length=100)\n', (591, 607), False, 'from django.db import models\n'), ((624, 656), 'django.db.models.TextField', 'models.TextField', ([], {'max_length': '(250)'}), '(max_length=250)\n', (640, 656), False, 'from django.db import models\n'), ((665, 686), 'django.db.models.IntegerField', 'models.IntegerField', ([], {}), '()\n', (684, 686), False, 'from django.db import models\n'), ((696, 723), 'django.db.models.ManyToManyField', 'models.ManyToManyField', (['Toy'], {}), '(Toy)\n', (718, 723), False, 'from django.db import models\n'), ((733, 782), 'django.db.models.ForeignKey', 'models.ForeignKey', (['User'], {'on_delete': 'models.CASCADE'}), '(User, on_delete=models.CASCADE)\n', (750, 782), False, 'from django.db import models\n'), ((1159, 1191), 'django.db.models.DateField', 'models.DateField', (['"""feeding date"""'], {}), "('feeding date')\n", (1175, 1191), False, 'from django.db import models\n'), ((1201, 1267), 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(1)', 'choices': 'MEALS', 'default': 'MEALS[0][0]'}), '(max_length=1, choices=MEALS, default=MEALS[0][0])\n', (1217, 1267), False, 'from django.db import models\n'), ((1292, 1340), 'django.db.models.ForeignKey', 'models.ForeignKey', (['Cat'], {'on_delete': 'models.CASCADE'}), '(Cat, on_delete=models.CASCADE)\n', (1309, 1340), False, 'from django.db import models\n'), ((1591, 1623), 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(200)'}), '(max_length=200)\n', (1607, 1623), False, 'from django.db import models\n'), ((1632, 1680), 'django.db.models.ForeignKey', 'models.ForeignKey', (['Cat'], {'on_delete': 'models.CASCADE'}), '(Cat, on_delete=models.CASCADE)\n', (1649, 1680), False, 'from django.db import models\n'), ((450, 496), 'django.urls.reverse', 'reverse', (['"""toys_detail"""'], {'kwargs': "{'pk': self.id}"}), "('toys_detail', kwargs={'pk': self.id})\n", (457, 496), False, 'from django.urls import reverse\n'), ((970, 1015), 'django.urls.reverse', 'reverse', (['"""detail"""'], {'kwargs': "{'cat_id': self.id}"}), "('detail', kwargs={'cat_id': self.id})\n", (977, 1015), False, 'from django.urls import reverse\n'), ((1084, 1096), 'datetime.date.today', 'date.today', ([], {}), '()\n', (1094, 1096), False, 'from datetime import date\n')]