
#This is the model we are going to use to store images bookmarked from different
#sites

from __future__ import unicode_literals
from django.utils.text import slugify
from django.db import models
from django.conf import settings

# Create your models here.

class Image(models.Model):
	user = models.ForeignKey(settings.AUTH_USER_MODEL,related_name='images_created', on_delete = models.CASCADE)
	title = models.CharField(max_length=200)
	slug = models.SlugField(max_length=200,blank=True)
	url = models.URLField()
	image = models.ImageField(upload_to='images/%Y/%m/%d')
	description = models.TextField(blank=True)
	created = models.DateField(auto_now_add=True,db_index=True)
	users_like = models.ManyToManyField(settings.AUTH_USER_MODEL,related_name='images_liked',blank=True)

	def __str__(self):
		return self.title
#we use the slufigy() function provided by Django to automatically
#generate the image slug for the given title when no slug is provided.


	def save(self, *args, **kwargs):
		if not self.slug:
			self.slug = slugify(self.title)
			super(Image, self).save(*args, **kwargs)