from django db import models from PIL import Image class Tags models M

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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)