from django.db import models
from PIL import Image
class Tags(models.Model):
tag_name=models.CharField(max_length=30)
def __unicode__(self):
return self.tag_name
class Meta:
ordering=('tag_name',)
verbose_name_plural=("VideFileTags")
class CategoryVideoSubjects(models.Model):
category=models.CharField(max_length=30)
is_published=models.BooleanField(default=True)
class Meta:
ordering=('category',)
verbose_name_plural=("VideoFileCategories")
def __unicode__(self):
return self.category
# Create your models here.
class Upload(models.Model):
title=models.CharField(max_length=200)
subject=models.ForeignKey(CategoryVideoSubjects)
author=models.CharField(max_length=200)
source_url=models.URLField()
about=models.TextField()
description=models.TextField()
tag=models.ManyToManyField(Tags)
file_name=models.FileField(upload_to="vid_file")
file_img=models.ImageField(upload_to="screan")
"""
Resize photos in the model
"""
def save(self, size=(400, 300)):
if not self.id and not self.file_img:
return
super(Upload, self).save()
pw=self.file_img.width
ph=self.file_img.height
nw=size[0]
nh=size[1]
if (pw, ph) != (nw, nh):
filename = str(self.file_img.path)
image = Image.open(filename)
pr = float(pw) / float(ph)
nr = float(nw) / float(nh)
if pr > nr:
tw=int(round(nh*pr))
image=image.resize((tw, nh), Image.ANTIALIAS)
l=int(round((tw-nw)/2.0))
image=image.crop((l, 0, l+nw, nh))
elif pr<nr:
th=int(round(nw/pr))
image=image.resize((nw, th), Image.ANTIALIAS)
t=int(round((th-nh)/2.0))
image=image.crop((0, t, nw, t + nh))
else:
image=image.resize(size, Image.ANTIALIAS)
image.save(filename)
small_img=models.ImageField(upload_to="small_img")
def save(self, size=(400, 300)):
if not self.id and not self.small_img:
return
super(Upload, self).save()
pw=self.small_img.width
ph=self.small_img.height
nw=size[0]
nh=size[1]
if (pw, ph) != (nw, nh):
filename = str(self.small_img.path)
image = Image.open(filename)
pr = float(pw) / float(ph)
nr = float(nw) / float(nh)
if pr > nr:
tw=int(round(nh*pr))
image=image.resize((tw, nh), Image.ANTIALIAS)
l=int(round((tw-nw)/2.0))
image=image.crop((l, 0, l+nw, nh))
elif pr<nr:
th=int(round(nw/pr))
image=image.resize((nw, th), Image.ANTIALIAS)
t=int(round((th-nh)/2.0))
image=image.crop((0, t, nw, t + nh))
else:
image=image.resize(size, Image.ANTIALIAS)
image.save(filename)
img3=models.ImageField(upload_to="small_img")
img4=models.ImageField(upload_to="small_img")
pub_date=models.DateField(auto_now_add=True)
modify_date=models.DateField(auto_now_add=True)
voiting=models.IntegerField(default=0)
class Meta:
verbose_name_plural=("Video files")
def __unicode__(self):
return(self.title)
#
class Categories(models.Model):
category_name=models.CharField(max_length=200)
published=models.BooleanField(default=True)
class Static(models.Model):
title=models.CharField(max_length=200)
about=models.TextField()
description=models.TextField()
class Links(models.Model):
title=models.CharField(max_length=200)
about=models.TextField()
link=models.URLField()
published=models.BooleanField(default=True)