2013-03-24 12:28:57 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
from __future__ import division, with_statement
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
2013-03-24 12:53:32 +00:00
|
|
|
from urllib import quote, unquote
|
2013-03-24 12:28:57 +00:00
|
|
|
|
|
|
|
from django.db import models
|
2013-05-27 11:21:08 +00:00
|
|
|
from django.db.models import Max
|
2013-03-24 12:28:57 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.db.models.signals import pre_delete
|
|
|
|
|
|
|
|
import Image
|
|
|
|
import ox
|
|
|
|
|
2013-05-27 11:21:08 +00:00
|
|
|
from item.models import Item
|
|
|
|
|
2013-03-24 12:28:57 +00:00
|
|
|
import managers
|
|
|
|
|
|
|
|
|
2013-05-27 10:13:59 +00:00
|
|
|
class Document(models.Model):
|
2013-03-24 12:28:57 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ("user", "name", "extension")
|
|
|
|
|
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
modified = models.DateTimeField(auto_now=True)
|
|
|
|
|
|
|
|
user = models.ForeignKey(User, related_name='files')
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
extension = models.CharField(max_length=255)
|
|
|
|
size = models.IntegerField(default=0)
|
|
|
|
matches = models.IntegerField(default=0)
|
|
|
|
ratio = models.FloatField(default=1)
|
|
|
|
description = models.TextField(default="")
|
|
|
|
oshash = models.CharField(max_length=16, unique=True, null=True)
|
|
|
|
|
|
|
|
file = models.FileField(default=None, blank=True,null=True, upload_to=lambda f, x: f.path(x))
|
|
|
|
|
2013-05-27 10:13:59 +00:00
|
|
|
objects = managers.DocumentManager()
|
2013-03-24 12:28:57 +00:00
|
|
|
uploading = models.BooleanField(default = False)
|
|
|
|
|
|
|
|
name_sort = models.CharField(max_length=255)
|
|
|
|
description_sort = models.CharField(max_length=512)
|
|
|
|
|
2013-05-27 11:21:08 +00:00
|
|
|
items = models.ManyToManyField(Item, through='ItemProperties', related_name='documents')
|
|
|
|
|
2013-03-24 12:28:57 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if not self.uploading:
|
|
|
|
if self.file:
|
|
|
|
self.size = self.file.size
|
|
|
|
if self.extension == 'pdf' and not os.path.exists(self.thumbnail()):
|
|
|
|
self.make_thumbnail()
|
|
|
|
|
2013-03-25 12:57:20 +00:00
|
|
|
self.name_sort = ox.sort_string(self.name or u'')[:255].lower()
|
|
|
|
self.description_sort = ox.sort_string(self.description or u'')[:512].lower()
|
2013-03-24 12:28:57 +00:00
|
|
|
|
2013-05-27 10:13:59 +00:00
|
|
|
super(Document, self).save(*args, **kwargs)
|
2013-03-24 12:28:57 +00:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.get_id()
|
|
|
|
|
2013-05-27 11:21:08 +00:00
|
|
|
def add(self, item):
|
|
|
|
p, created = ItemProperties.objects.get_or_create(item=item, document=self)
|
|
|
|
if created:
|
|
|
|
p.index = ItemProperties.objects.filter(item=item).aggregate(Max('index'))['index__max'] + 1
|
|
|
|
p.save()
|
|
|
|
|
|
|
|
def remove(self, item):
|
|
|
|
ItemProperties.objects.filter(item=item, document=self).delete()
|
|
|
|
|
2013-03-24 12:28:57 +00:00
|
|
|
@classmethod
|
|
|
|
def get(cls, id):
|
2013-12-23 11:26:48 +00:00
|
|
|
return cls.objects.get(pk=ox.fromAZ(id))
|
2013-03-24 12:28:57 +00:00
|
|
|
|
|
|
|
def get_absolute_url(self):
|
2013-05-27 20:03:18 +00:00
|
|
|
return ('/documents/%s' % quote(self.get_id())).replace('%3A', ':')
|
2013-03-24 12:28:57 +00:00
|
|
|
|
|
|
|
def get_id(self):
|
2013-12-23 11:26:48 +00:00
|
|
|
return ox.toAZ(self.id)
|
2013-03-24 12:28:57 +00:00
|
|
|
|
|
|
|
def editable(self, user):
|
|
|
|
if not user or user.is_anonymous():
|
|
|
|
return False
|
|
|
|
if self.user == user or \
|
|
|
|
user.is_staff or \
|
2013-05-27 10:13:59 +00:00
|
|
|
user.get_profile().capability('canEditDocuments') == True:
|
2013-03-24 12:28:57 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2013-05-27 11:21:08 +00:00
|
|
|
def edit(self, data, user, item=None):
|
2013-03-24 12:28:57 +00:00
|
|
|
for key in data:
|
|
|
|
if key == 'name':
|
|
|
|
data['name'] = re.sub(' \[\d+\]$', '', data['name']).strip()
|
|
|
|
if not data['name']:
|
|
|
|
data['name'] = "Untitled"
|
|
|
|
name = data['name']
|
|
|
|
num = 1
|
2013-05-27 10:13:59 +00:00
|
|
|
while Document.objects.filter(name=name, user=self.user, extension=self.extension).exclude(id=self.id).count()>0:
|
2013-03-24 12:28:57 +00:00
|
|
|
num += 1
|
|
|
|
name = data['name'] + ' [%d]' % num
|
|
|
|
self.name = name
|
2013-05-27 11:21:08 +00:00
|
|
|
elif key == 'description' and not item:
|
2013-03-24 12:28:57 +00:00
|
|
|
self.description = ox.sanitize_html(data['description'])
|
2013-05-27 11:21:08 +00:00
|
|
|
if item:
|
|
|
|
p, created = ItemProperties.objects.get_or_create(item=item, document=self)
|
|
|
|
if 'description' in data:
|
|
|
|
p.description = ox.sanitize_html(data['description'])
|
|
|
|
p.save()
|
2013-03-24 12:28:57 +00:00
|
|
|
|
2013-05-27 11:21:08 +00:00
|
|
|
def json(self, keys=None, user=None, item=None):
|
2013-03-24 12:28:57 +00:00
|
|
|
if not keys:
|
|
|
|
keys=[
|
|
|
|
'description',
|
|
|
|
'editable',
|
|
|
|
'id',
|
|
|
|
'name',
|
|
|
|
'extension',
|
|
|
|
'oshash',
|
|
|
|
'size',
|
|
|
|
'ratio',
|
|
|
|
'user'
|
|
|
|
]
|
|
|
|
response = {}
|
|
|
|
_map = {
|
|
|
|
}
|
|
|
|
for key in keys:
|
|
|
|
if key == 'id':
|
|
|
|
response[key] = self.get_id()
|
|
|
|
elif key == 'editable':
|
|
|
|
response[key] = self.editable(user)
|
|
|
|
elif key == 'user':
|
|
|
|
response[key] = self.user.username
|
|
|
|
elif hasattr(self, _map.get(key, key)):
|
|
|
|
response[key] = getattr(self, _map.get(key,key))
|
2013-05-27 11:21:08 +00:00
|
|
|
if item:
|
|
|
|
d = self.descriptions.filter(item=item)
|
|
|
|
if d.exists():
|
|
|
|
if 'description' in keys and d[0].description:
|
|
|
|
response['description'] = d[0].description
|
|
|
|
response['index'] = d[0].index
|
2013-03-24 12:28:57 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
def path(self, name=''):
|
2013-12-23 11:30:22 +00:00
|
|
|
h = ox.toAZ(self.id)
|
|
|
|
h = (7-len(h))*'0' + h
|
2013-05-27 10:13:59 +00:00
|
|
|
return os.path.join('documents', h[:2], h[2:4], h[4:6], h[6:], name)
|
2013-03-24 12:28:57 +00:00
|
|
|
|
|
|
|
def save_chunk(self, chunk, chunk_id=-1, done=False):
|
|
|
|
if self.uploading:
|
|
|
|
if not self.file:
|
|
|
|
name = 'data.%s' % self.extension
|
|
|
|
self.file.name = self.path(name)
|
|
|
|
ox.makedirs(os.path.dirname(self.file.path))
|
|
|
|
with open(self.file.path, 'w') as f:
|
|
|
|
f.write(chunk.read())
|
|
|
|
self.save()
|
|
|
|
else:
|
|
|
|
with open(self.file.path, 'a') as f:
|
|
|
|
f.write(chunk.read())
|
|
|
|
if done:
|
|
|
|
self.uploading = False
|
|
|
|
self.get_ratio()
|
|
|
|
self.oshash = ox.oshash(self.file.path)
|
|
|
|
self.save()
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def thumbnail(self):
|
2013-12-23 11:26:48 +00:00
|
|
|
if self.extension == 'pdf':
|
|
|
|
thumbnail = '%s.jpg' % self.file.path
|
|
|
|
else:
|
|
|
|
thumbnail = self.file.path
|
|
|
|
return thumbnail
|
2013-03-24 12:28:57 +00:00
|
|
|
|
|
|
|
def make_thumbnail(self, force=False):
|
|
|
|
thumb = self.thumbnail()
|
|
|
|
if not os.path.exists(thumb) or force:
|
|
|
|
cmd = ['convert', '%s[0]' % self.file.path,
|
|
|
|
'-background', 'white', '-flatten', '-resize', '256x256', thumb]
|
|
|
|
p = subprocess.Popen(cmd)
|
|
|
|
p.wait()
|
|
|
|
|
|
|
|
def get_ratio(self):
|
|
|
|
if self.extension == 'pdf':
|
|
|
|
self.make_thumbnail()
|
|
|
|
image = self.thumbnail()
|
|
|
|
else:
|
|
|
|
image = self.file.path
|
|
|
|
try:
|
|
|
|
size = Image.open(image).size
|
|
|
|
except:
|
|
|
|
size = [1,1]
|
|
|
|
self.ratio = size[0] / size[1]
|
|
|
|
|
2013-03-24 12:53:32 +00:00
|
|
|
def update_matches(self):
|
|
|
|
import annotation.models
|
|
|
|
import item.models
|
|
|
|
import text.models
|
|
|
|
urls = [self.get_absolute_url()]
|
|
|
|
url = unquote(urls[0])
|
|
|
|
if url != urls[0]:
|
|
|
|
urls.append(url)
|
|
|
|
matches = 0
|
|
|
|
for url in urls:
|
|
|
|
matches += annotation.models.Annotation.objects.filter(value__contains=url).count()
|
|
|
|
matches += item.models.Item.objects.filter(data__contains=url).count()
|
|
|
|
matches += text.models.Text.objects.filter(text__contains=url).count()
|
|
|
|
if matches != self.matches:
|
2013-05-27 10:13:59 +00:00
|
|
|
Document.objects.filter(id=self.id).update(matches=matches)
|
2013-03-24 12:53:32 +00:00
|
|
|
self.matches = matches
|
2013-03-24 12:28:57 +00:00
|
|
|
|
2013-05-27 10:13:59 +00:00
|
|
|
def delete_document(sender, **kwargs):
|
2013-03-24 12:28:57 +00:00
|
|
|
t = kwargs['instance']
|
|
|
|
if t.file:
|
|
|
|
if t.extension == 'pdf':
|
|
|
|
thumb = t.thumbnail()
|
|
|
|
if os.path.exists(thumb):
|
|
|
|
os.unlink(thumb)
|
|
|
|
t.file.delete()
|
2013-05-27 10:13:59 +00:00
|
|
|
pre_delete.connect(delete_document, sender=Document)
|
2013-03-24 12:28:57 +00:00
|
|
|
|
2013-05-27 11:21:08 +00:00
|
|
|
class ItemProperties(models.Model):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ("item", "document")
|
|
|
|
|
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
modified = models.DateTimeField(auto_now=True)
|
|
|
|
|
|
|
|
item = models.ForeignKey(Item)
|
|
|
|
document = models.ForeignKey(Document, related_name='descriptions')
|
|
|
|
description = models.TextField(default="")
|
|
|
|
index = models.IntegerField(default=0)
|
|
|
|
|