Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.5 kB
1
Indexable
Never
# -*- coding: utf-8 -*-
import os
from django.conf import settings
from django.urls import reverse
from django.db import models
from django.db.models.signals import pre_save
from django.dispatch import receiver
from django.utils.text import slugify
from django.utils.translation import ugettext_lazy as _

#from sorl.thumbnail import ImageField
from moviesapp.apps.movies import managers
from moviesapp.apps.categories.models  import Category
 
from moviesapp.apps.core.utils import generate_random_string


class Movie(models.Model):
   
	#def upload_location(instance, filename):
	#	try:
	#		MovieModel = instance.__class__
	#		new_id = MovieModel.objects.order_by("id").last().id + 1
	#	except:
	#		new_id=1

	#	return "{} {}".format(new_id, filename)
	# Relations
	categories = models.ManyToManyField(Category,  default=[1],  verbose_name=_(
		"related_categories"),related_name="movie_categories" )
 
	user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1,  verbose_name=_(
		"related_user"),related_name="movies", on_delete=models.CASCADE,)
	# Attributes - Mandatory
	title = models.CharField(max_length=200, verbose_name=_(
		"title"), help_text=_("Enter the Movie title"))
	slug = models.SlugField(max_length=255, unique=True)
	content = models.TextField()
 
	
	draft = models.BooleanField(default=False)
 
	publish = models.DateField(auto_now=False, auto_now_add=False)
	created_at = models.DateTimeField(
		auto_now=True, auto_now_add=False, verbose_name=("created_at"))
	updated_at = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name=_(
		"updated_at"), help_text=_("Enter a new date"))
	# Attributes - Optional
 
	# Object Manager
	objects = managers.MovieManager()

	# Custom Properties

	# Methods
	def get_absolute_url(self):
		return reverse("movies:detail", kwargs={"slug": self.slug})

 
	# Meta and String
	class Meta:
		verbose_name = _("Movie")
		verbose_name_plural = _("Movies")
		ordering = ( "title")
		unique_together = ( "title")
		
	def __str__(self):
		return "{} - {}".format(self.title)

	def human_format_time(self):
	   from django.utils.timesince import timesince
	   return timesince(self.publish)



	

@receiver(pre_save, sender=Movie)
def pre_save_article_receiver(sender, instance, raw, using, **kwargs):
	if instance and not instance.slug:
		slug = slugify(instance.title)
		random_string = generate_random_string()
		instance.slug = slug + "_" + random_string


pre_save.connect(pre_save_article_receiver, sender=Article)