rename files to documents and use media for media files
This commit is contained in:
parent
aaa153e19d
commit
8da6badf4c
27 changed files with 1010 additions and 856 deletions
0
pandora/document/__init__.py
Normal file
0
pandora/document/__init__.py
Normal file
127
pandora/document/managers.py
Normal file
127
pandora/document/managers.py
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from django.db.models import Q, Manager
|
||||
|
||||
|
||||
def parseCondition(condition, user):
|
||||
'''
|
||||
'''
|
||||
k = condition.get('key', 'name')
|
||||
k = {
|
||||
'user': 'user__username',
|
||||
}.get(k, k)
|
||||
if not k:
|
||||
k = 'name'
|
||||
v = condition['value']
|
||||
op = condition.get('operator')
|
||||
if not op:
|
||||
op = '='
|
||||
if op.startswith('!'):
|
||||
op = op[1:]
|
||||
exclude = True
|
||||
else:
|
||||
exclude = False
|
||||
if k == 'id':
|
||||
try:
|
||||
public_id = v.split(':')
|
||||
username = public_id[0]
|
||||
name = ":".join(public_id[1:])
|
||||
extension = name.split('.')
|
||||
name = '.'.join(extension[:-1])
|
||||
extension = extension[-1].lower()
|
||||
q = Q(user__username=username, name=name, extension=extension)
|
||||
except:
|
||||
q = Q(id__in=[])
|
||||
return q
|
||||
if isinstance(v, bool): #featured and public flag
|
||||
key = k
|
||||
else:
|
||||
key = "%s%s" % (k, {
|
||||
'==': '__iexact',
|
||||
'^': '__istartswith',
|
||||
'$': '__iendswith',
|
||||
}.get(op, '__icontains'))
|
||||
key = str(key)
|
||||
if exclude:
|
||||
q = ~Q(**{key: v})
|
||||
else:
|
||||
q = Q(**{key: v})
|
||||
return q
|
||||
|
||||
def parseConditions(conditions, operator, user):
|
||||
'''
|
||||
conditions: [
|
||||
{
|
||||
value: "war"
|
||||
}
|
||||
{
|
||||
key: "year",
|
||||
value: "1970-1980,
|
||||
operator: "!="
|
||||
},
|
||||
{
|
||||
key: "country",
|
||||
value: "f",
|
||||
operator: "^"
|
||||
}
|
||||
],
|
||||
operator: "&"
|
||||
'''
|
||||
conn = []
|
||||
for condition in conditions:
|
||||
if 'conditions' in condition:
|
||||
q = parseConditions(condition['conditions'],
|
||||
condition.get('operator', '&'), user)
|
||||
if q:
|
||||
conn.append(q)
|
||||
pass
|
||||
else:
|
||||
conn.append(parseCondition(condition, user))
|
||||
if conn:
|
||||
q = conn[0]
|
||||
for c in conn[1:]:
|
||||
if operator == '|':
|
||||
q = q | c
|
||||
else:
|
||||
q = q & c
|
||||
return q
|
||||
return None
|
||||
|
||||
|
||||
class DocumentManager(Manager):
|
||||
|
||||
def get_query_set(self):
|
||||
return super(DocumentManager, self).get_query_set()
|
||||
|
||||
def find(self, data, user):
|
||||
'''
|
||||
query: {
|
||||
conditions: [
|
||||
{
|
||||
value: "war"
|
||||
}
|
||||
{
|
||||
key: "year",
|
||||
value: "1970-1980,
|
||||
operator: "!="
|
||||
},
|
||||
{
|
||||
key: "country",
|
||||
value: "f",
|
||||
operator: "^"
|
||||
}
|
||||
],
|
||||
operator: "&"
|
||||
}
|
||||
'''
|
||||
|
||||
#join query with operator
|
||||
qs = self.get_query_set()
|
||||
conditions = parseConditions(data['query'].get('conditions', []),
|
||||
data['query'].get('operator', '&'),
|
||||
user)
|
||||
if conditions:
|
||||
qs = qs.filter(conditions)
|
||||
|
||||
return qs
|
||||
|
||||
18
pandora/document/migration_utils.py
Normal file
18
pandora/document/migration_utils.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import os
|
||||
from south.models import MigrationHistory
|
||||
|
||||
def was_applied(migration_file_path, app_name):
|
||||
"""true if migration with a given file name ``migration_file``
|
||||
was applied to app with name ``app_name``"""
|
||||
try:
|
||||
migration_file = os.path.basename(migration_file_path)
|
||||
migration_name = migration_file.split('.')[0]
|
||||
MigrationHistory.objects.get(
|
||||
app_name = app_name,
|
||||
migration = migration_name
|
||||
)
|
||||
return True
|
||||
except MigrationHistory.DoesNotExist:
|
||||
return False
|
||||
|
||||
104
pandora/document/migrations/0001_initial.py
Normal file
104
pandora/document/migrations/0001_initial.py
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import datetime
|
||||
from south.db import db
|
||||
from south.v2 import SchemaMigration
|
||||
from django.db import models
|
||||
|
||||
from ..migration_utils import was_applied
|
||||
|
||||
class Migration(SchemaMigration):
|
||||
|
||||
def forwards(self, orm):
|
||||
if was_applied(__file__, 'file'):
|
||||
return
|
||||
|
||||
# Adding model 'File'
|
||||
db.create_table('file_file', (
|
||||
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
|
||||
('created', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, blank=True)),
|
||||
('modified', self.gf('django.db.models.fields.DateTimeField')(auto_now=True, blank=True)),
|
||||
('user', self.gf('django.db.models.fields.related.ForeignKey')(related_name='files', to=orm['auth.User'])),
|
||||
('name', self.gf('django.db.models.fields.CharField')(max_length=255)),
|
||||
('extension', self.gf('django.db.models.fields.CharField')(max_length=255)),
|
||||
('size', self.gf('django.db.models.fields.IntegerField')(default=0)),
|
||||
('matches', self.gf('django.db.models.fields.IntegerField')(default=0)),
|
||||
('ratio', self.gf('django.db.models.fields.FloatField')(default=1)),
|
||||
('description', self.gf('django.db.models.fields.TextField')(default='')),
|
||||
('oshash', self.gf('django.db.models.fields.CharField')(max_length=16, unique=True, null=True)),
|
||||
('file', self.gf('django.db.models.fields.files.FileField')(default=None, max_length=100, null=True, blank=True)),
|
||||
('uploading', self.gf('django.db.models.fields.BooleanField')(default=False)),
|
||||
('name_sort', self.gf('django.db.models.fields.CharField')(max_length=255)),
|
||||
('description_sort', self.gf('django.db.models.fields.CharField')(max_length=512)),
|
||||
))
|
||||
db.send_create_signal('file', ['File'])
|
||||
|
||||
# Adding unique constraint on 'File', fields ['user', 'name', 'extension']
|
||||
db.create_unique('file_file', ['user_id', 'name', 'extension'])
|
||||
|
||||
|
||||
def backwards(self, orm):
|
||||
# Removing unique constraint on 'File', fields ['user', 'name', 'extension']
|
||||
db.delete_unique('file_file', ['user_id', 'name', 'extension'])
|
||||
|
||||
# Deleting model 'File'
|
||||
db.delete_table('file_file')
|
||||
|
||||
|
||||
models = {
|
||||
'auth.group': {
|
||||
'Meta': {'object_name': 'Group'},
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'}),
|
||||
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
|
||||
},
|
||||
'auth.permission': {
|
||||
'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
|
||||
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
|
||||
},
|
||||
'auth.user': {
|
||||
'Meta': {'object_name': 'User'},
|
||||
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
|
||||
'email': ('django.db.models.fields.EmailField', [], {'max_length': '255', 'blank': 'True'}),
|
||||
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
|
||||
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
|
||||
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
|
||||
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
|
||||
'password': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
|
||||
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
|
||||
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'})
|
||||
},
|
||||
'contenttypes.contenttype': {
|
||||
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
|
||||
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
|
||||
},
|
||||
'file.file': {
|
||||
'Meta': {'unique_together': "(('user', 'name', 'extension'),)", 'object_name': 'File'},
|
||||
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
|
||||
'description': ('django.db.models.fields.TextField', [], {'default': "''"}),
|
||||
'description_sort': ('django.db.models.fields.CharField', [], {'max_length': '512'}),
|
||||
'extension': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
|
||||
'file': ('django.db.models.fields.files.FileField', [], {'default': 'None', 'max_length': '100', 'null': 'True', 'blank': 'True'}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'matches': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
|
||||
'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
|
||||
'name_sort': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
|
||||
'oshash': ('django.db.models.fields.CharField', [], {'max_length': '16', 'unique': 'True', 'null': 'True'}),
|
||||
'ratio': ('django.db.models.fields.FloatField', [], {'default': '1'}),
|
||||
'size': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
|
||||
'uploading': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||
'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'files'", 'to': "orm['auth.User']"})
|
||||
}
|
||||
}
|
||||
|
||||
complete_apps = ['file']
|
||||
103
pandora/document/migrations/0002_data_folders.py
Normal file
103
pandora/document/migrations/0002_data_folders.py
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import datetime
|
||||
from south.db import db
|
||||
from south.v2 import DataMigration
|
||||
from django.db import models
|
||||
|
||||
from ..migration_utils import was_applied
|
||||
|
||||
class Migration(DataMigration):
|
||||
|
||||
def forwards(self, orm):
|
||||
if was_applied(__file__, 'file'):
|
||||
return
|
||||
|
||||
"Write your forwards methods here."
|
||||
# Note: Remember to use orm['appname.ModelName'] rather than "from appname.models..."
|
||||
import os
|
||||
from os.path import exists
|
||||
from django.conf import settings
|
||||
|
||||
media_path = os.path.join(settings.MEDIA_ROOT, 'media')
|
||||
files_path = os.path.join(settings.MEDIA_ROOT, 'files')
|
||||
uploads_path = os.path.join(settings.MEDIA_ROOT, 'uploads')
|
||||
if not exists(media_path) and exists(files_path):
|
||||
os.rename(files_path, media_path)
|
||||
if not exists(files_path) and exists(uploads_path):
|
||||
os.rename(uploads_path, files_path)
|
||||
for f in orm['file.File'].objects.all():
|
||||
f.file.name = f.file.name.replace('uploads/', 'files/')
|
||||
f.save()
|
||||
|
||||
def backwards(self, orm):
|
||||
import os
|
||||
from os.path import exists
|
||||
from django.conf import settings
|
||||
|
||||
media_path = os.path.join(settings.MEDIA_ROOT, 'media')
|
||||
files_path = os.path.join(settings.MEDIA_ROOT, 'files')
|
||||
uploads_path = os.path.join(settings.MEDIA_ROOT, 'uploads')
|
||||
if exists(media_path) and exists(files_path) and not exists(uploads_path):
|
||||
os.rename(files_path, uploads_path)
|
||||
if exists(media_path) and not exists(files_path):
|
||||
os.rename(media_path, files_path)
|
||||
|
||||
models = {
|
||||
'auth.group': {
|
||||
'Meta': {'object_name': 'Group'},
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'}),
|
||||
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
|
||||
},
|
||||
'auth.permission': {
|
||||
'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
|
||||
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
|
||||
},
|
||||
'auth.user': {
|
||||
'Meta': {'object_name': 'User'},
|
||||
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
|
||||
'email': ('django.db.models.fields.EmailField', [], {'max_length': '255', 'blank': 'True'}),
|
||||
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
|
||||
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
|
||||
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
|
||||
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
|
||||
'password': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
|
||||
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
|
||||
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'})
|
||||
},
|
||||
'contenttypes.contenttype': {
|
||||
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
|
||||
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
|
||||
},
|
||||
'file.file': {
|
||||
'Meta': {'unique_together': "(('user', 'name', 'extension'),)", 'object_name': 'File'},
|
||||
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
|
||||
'description': ('django.db.models.fields.TextField', [], {'default': "''"}),
|
||||
'description_sort': ('django.db.models.fields.CharField', [], {'max_length': '512'}),
|
||||
'extension': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
|
||||
'file': ('django.db.models.fields.files.FileField', [], {'default': 'None', 'max_length': '100', 'null': 'True', 'blank': 'True'}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'matches': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
|
||||
'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
|
||||
'name_sort': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
|
||||
'oshash': ('django.db.models.fields.CharField', [], {'max_length': '16', 'unique': 'True', 'null': 'True'}),
|
||||
'ratio': ('django.db.models.fields.FloatField', [], {'default': '1'}),
|
||||
'size': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
|
||||
'uploading': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||
'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'files'", 'to': "orm['auth.User']"})
|
||||
}
|
||||
}
|
||||
|
||||
complete_apps = ['file']
|
||||
symmetrical = True
|
||||
90
pandora/document/migrations/0003_rename.py
Normal file
90
pandora/document/migrations/0003_rename.py
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import os
|
||||
from os.path import exists, join
|
||||
import datetime
|
||||
|
||||
from south.db import db
|
||||
from south.v2 import SchemaMigration
|
||||
from django.db import models
|
||||
from django.conf import settings
|
||||
|
||||
class Migration(SchemaMigration):
|
||||
|
||||
def forwards(self, orm):
|
||||
files_path = join(settings.MEDIA_ROOT, 'files')
|
||||
documents_path = join(settings.MEDIA_ROOT, 'documents')
|
||||
if not exists(documents_path) and exists(files_path):
|
||||
os.rename(files_path, documents_path)
|
||||
db.rename_table('file_file', 'document_document')
|
||||
for f in orm['document.Document'].objects.all():
|
||||
f.file.name = f.file.name.replace('files/', 'documents/')
|
||||
f.save()
|
||||
|
||||
def backwards(self, orm):
|
||||
files_path = join(settings.MEDIA_ROOT, 'files')
|
||||
documents_path = join(settings.MEDIA_ROOT, 'documents')
|
||||
if not exists(files_path) and exists(documents_path):
|
||||
os.rename(documents_path, files_path)
|
||||
for f in orm['document.Document'].objects.all():
|
||||
f.file.name = f.file.name.replace('documents/', 'files/')
|
||||
f.save()
|
||||
db.rename_table('document_document', 'file_file')
|
||||
|
||||
models = {
|
||||
'auth.group': {
|
||||
'Meta': {'object_name': 'Group'},
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'}),
|
||||
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
|
||||
},
|
||||
'auth.permission': {
|
||||
'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
|
||||
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
|
||||
},
|
||||
'auth.user': {
|
||||
'Meta': {'object_name': 'User'},
|
||||
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
|
||||
'email': ('django.db.models.fields.EmailField', [], {'max_length': '255', 'blank': 'True'}),
|
||||
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
|
||||
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
|
||||
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
|
||||
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
|
||||
'password': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
|
||||
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
|
||||
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'})
|
||||
},
|
||||
'contenttypes.contenttype': {
|
||||
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
|
||||
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
|
||||
},
|
||||
'document.document': {
|
||||
'Meta': {'unique_together': "(('user', 'name', 'extension'),)", 'object_name': 'Document'},
|
||||
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
|
||||
'description': ('django.db.models.fields.TextField', [], {'default': "''"}),
|
||||
'description_sort': ('django.db.models.fields.CharField', [], {'max_length': '512'}),
|
||||
'extension': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
|
||||
'file': ('django.db.models.fields.files.FileField', [], {'default': 'None', 'max_length': '100', 'null': 'True', 'blank': 'True'}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'matches': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
|
||||
'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
|
||||
'name_sort': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
|
||||
'oshash': ('django.db.models.fields.CharField', [], {'max_length': '16', 'unique': 'True', 'null': 'True'}),
|
||||
'ratio': ('django.db.models.fields.FloatField', [], {'default': '1'}),
|
||||
'size': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
|
||||
'uploading': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||
'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'files'", 'to': "orm['auth.User']"})
|
||||
}
|
||||
}
|
||||
|
||||
complete_apps = ['document']
|
||||
0
pandora/document/migrations/__init__.py
Normal file
0
pandora/document/migrations/__init__.py
Normal file
204
pandora/document/models.py
Normal file
204
pandora/document/models.py
Normal file
|
|
@ -0,0 +1,204 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from __future__ import division, with_statement
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
from urllib import quote, unquote
|
||||
|
||||
from django.db import models
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models.signals import pre_delete
|
||||
|
||||
import Image
|
||||
import ox
|
||||
|
||||
import managers
|
||||
|
||||
|
||||
class Document(models.Model):
|
||||
|
||||
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))
|
||||
|
||||
objects = managers.DocumentManager()
|
||||
uploading = models.BooleanField(default = False)
|
||||
|
||||
name_sort = models.CharField(max_length=255)
|
||||
description_sort = models.CharField(max_length=512)
|
||||
|
||||
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()
|
||||
|
||||
self.name_sort = ox.sort_string(self.name or u'')[:255].lower()
|
||||
self.description_sort = ox.sort_string(self.description or u'')[:512].lower()
|
||||
|
||||
super(Document, self).save(*args, **kwargs)
|
||||
|
||||
def __unicode__(self):
|
||||
return self.get_id()
|
||||
|
||||
@classmethod
|
||||
def get(cls, id):
|
||||
username, name, extension = cls.parse_id(id)
|
||||
return cls.objects.get(user__username=username, name=name, extension=extension)
|
||||
|
||||
@classmethod
|
||||
def parse_id(cls, id):
|
||||
public_id = id.split(':')
|
||||
username = public_id[0]
|
||||
name = ":".join(public_id[1:])
|
||||
extension = name.split('.')
|
||||
name = '.'.join(extension[:-1])
|
||||
extension = extension[-1].lower()
|
||||
return username, name, extension
|
||||
|
||||
def get_absolute_url(self):
|
||||
return ('/files/%s' % quote(self.get_id())).replace('%3A', ':')
|
||||
|
||||
def get_id(self):
|
||||
return u'%s:%s.%s' % (self.user.username, self.name, self.extension)
|
||||
|
||||
def editable(self, user):
|
||||
if not user or user.is_anonymous():
|
||||
return False
|
||||
if self.user == user or \
|
||||
user.is_staff or \
|
||||
user.get_profile().capability('canEditDocuments') == True:
|
||||
return True
|
||||
return False
|
||||
|
||||
def edit(self, data, user):
|
||||
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
|
||||
while Document.objects.filter(name=name, user=self.user, extension=self.extension).exclude(id=self.id).count()>0:
|
||||
num += 1
|
||||
name = data['name'] + ' [%d]' % num
|
||||
self.name = name
|
||||
elif key == 'description':
|
||||
self.description = ox.sanitize_html(data['description'])
|
||||
|
||||
def json(self, keys=None, user=None):
|
||||
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))
|
||||
return response
|
||||
|
||||
def path(self, name=''):
|
||||
h = "%07d" % self.id
|
||||
return os.path.join('documents', h[:2], h[2:4], h[4:6], h[6:], name)
|
||||
|
||||
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):
|
||||
return '%s.jpg' % self.file.path
|
||||
|
||||
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]
|
||||
|
||||
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:
|
||||
Document.objects.filter(id=self.id).update(matches=matches)
|
||||
self.matches = matches
|
||||
|
||||
def delete_document(sender, **kwargs):
|
||||
t = kwargs['instance']
|
||||
if t.file:
|
||||
if t.extension == 'pdf':
|
||||
thumb = t.thumbnail()
|
||||
if os.path.exists(thumb):
|
||||
os.unlink(thumb)
|
||||
t.file.delete()
|
||||
pre_delete.connect(delete_document, sender=Document)
|
||||
|
||||
241
pandora/document/views.py
Normal file
241
pandora/document/views.py
Normal file
|
|
@ -0,0 +1,241 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from __future__ import division
|
||||
|
||||
from ox.utils import json
|
||||
from ox.django.api import actions
|
||||
from ox.django.decorators import login_required_json
|
||||
from ox.django.http import HttpFileResponse
|
||||
from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response
|
||||
from django import forms
|
||||
|
||||
from item import utils
|
||||
from item.models import Item
|
||||
import models
|
||||
|
||||
def get_document_or_404_json(id):
|
||||
username, name, extension = models.Document.parse_id(id)
|
||||
return get_object_or_404_json(models.Document, user__username=username, name=name, extension=extension)
|
||||
|
||||
@login_required_json
|
||||
def addDocument(request):
|
||||
'''
|
||||
add document(s) to item
|
||||
takes {
|
||||
item: string
|
||||
id: string
|
||||
or
|
||||
ids: [string]
|
||||
}
|
||||
returns {
|
||||
}
|
||||
'''
|
||||
response = json_response()
|
||||
data = json.loads(request.POST['data'])
|
||||
if 'ids' in data:
|
||||
ids = data['ids']
|
||||
else:
|
||||
ids = [data['id']]
|
||||
item = Item.objects.get(itemId=data['item'])
|
||||
if item.editable(request.user):
|
||||
for id in ids:
|
||||
file = models.Document.get(id)
|
||||
item.documents.add(file)
|
||||
else:
|
||||
response = json_response(status=403, file='permission denied')
|
||||
return render_to_json_response(response)
|
||||
actions.register(addDocument, cache=False)
|
||||
|
||||
@login_required_json
|
||||
def editDocument(request):
|
||||
'''
|
||||
takes {
|
||||
id: string
|
||||
name: string
|
||||
description: string
|
||||
}
|
||||
returns {
|
||||
id:
|
||||
...
|
||||
}
|
||||
'''
|
||||
response = json_response()
|
||||
data = json.loads(request.POST['data'])
|
||||
if data['id']:
|
||||
file = models.Document.get(data['id'])
|
||||
if file.editable(request.user):
|
||||
file.edit(data, request.user)
|
||||
file.save()
|
||||
response['data'] = file.json(user=request.user)
|
||||
else:
|
||||
response = json_response(status=403, file='permission denied')
|
||||
else:
|
||||
response = json_response(status=500, file='invalid request')
|
||||
return render_to_json_response(response)
|
||||
actions.register(editDocument, cache=False)
|
||||
|
||||
|
||||
def _order_query(qs, sort):
|
||||
order_by = []
|
||||
for e in sort:
|
||||
operator = e['operator']
|
||||
if operator != '-':
|
||||
operator = ''
|
||||
key = {
|
||||
'name': 'name_sort',
|
||||
'description': 'description_sort',
|
||||
}.get(e['key'], e['key'])
|
||||
order = '%s%s' % (operator, key)
|
||||
order_by.append(order)
|
||||
if order_by:
|
||||
qs = qs.order_by(*order_by)
|
||||
qs = qs.distinct()
|
||||
return qs
|
||||
|
||||
def parse_query(data, user):
|
||||
query = {}
|
||||
query['range'] = [0, 100]
|
||||
query['sort'] = [{'key':'user', 'operator':'+'}, {'key':'name', 'operator':'+'}]
|
||||
for key in ('keys', 'group', 'file', 'range', 'position', 'positions', 'sort'):
|
||||
if key in data:
|
||||
query[key] = data[key]
|
||||
query['qs'] = models.Document.objects.find(data, user).exclude(name='')
|
||||
return query
|
||||
|
||||
|
||||
def findDocuments(request):
|
||||
'''
|
||||
takes {
|
||||
query: {
|
||||
conditions: [
|
||||
{
|
||||
key: 'user',
|
||||
value: 'something',
|
||||
operator: '='
|
||||
}
|
||||
]
|
||||
operator: ","
|
||||
},
|
||||
sort: [{key: 'name', operator: '+'}],
|
||||
range: [0, 100]
|
||||
keys: []
|
||||
}
|
||||
|
||||
possible query keys:
|
||||
name, user, extension, size
|
||||
|
||||
possible keys:
|
||||
name, user, extension, size
|
||||
|
||||
}
|
||||
returns {
|
||||
items: [object]
|
||||
}
|
||||
'''
|
||||
data = json.loads(request.POST['data'])
|
||||
query = parse_query(data, request.user)
|
||||
|
||||
#order
|
||||
qs = _order_query(query['qs'], query['sort'])
|
||||
response = json_response()
|
||||
if 'keys' in data:
|
||||
qs = qs[query['range'][0]:query['range'][1]]
|
||||
|
||||
response['data']['items'] = [l.json(data['keys'], request.user) for l in qs]
|
||||
elif 'position' in data:
|
||||
#FIXME: actually implement position requests
|
||||
response['data']['position'] = 0
|
||||
elif 'positions' in data:
|
||||
ids = [i.get_id() for i in qs]
|
||||
response['data']['positions'] = utils.get_positions(ids, query['positions'])
|
||||
else:
|
||||
response['data']['items'] = qs.count()
|
||||
return render_to_json_response(response)
|
||||
actions.register(findDocuments)
|
||||
|
||||
@login_required_json
|
||||
def removeDocument(request):
|
||||
'''
|
||||
takes {
|
||||
id: string,
|
||||
or
|
||||
ids: [string]
|
||||
}
|
||||
returns {
|
||||
}
|
||||
'''
|
||||
data = json.loads(request.POST['data'])
|
||||
response = json_response()
|
||||
|
||||
if 'ids' in data:
|
||||
ids = data['ids']
|
||||
else:
|
||||
ids = [data['id']]
|
||||
for id in ids:
|
||||
file = models.Document.get(id)
|
||||
if file.editable(request.user):
|
||||
file.delete()
|
||||
else:
|
||||
response = json_response(status=403, file='not allowed')
|
||||
break
|
||||
return render_to_json_response(response)
|
||||
actions.register(removeDocument, cache=False)
|
||||
|
||||
def file(request, id):
|
||||
document = models.Document.get(id)
|
||||
return HttpFileResponse(document.file.path)
|
||||
|
||||
def thumbnail(request, id):
|
||||
document = models.Document.get(id)
|
||||
return HttpFileResponse(document.thumbnail())
|
||||
|
||||
class ChunkForm(forms.Form):
|
||||
chunk = forms.FileField()
|
||||
chunkId = forms.IntegerField(required=False)
|
||||
done = forms.IntegerField(required=False)
|
||||
|
||||
@login_required_json
|
||||
def upload(request):
|
||||
if 'id' in request.GET:
|
||||
file = models.Document.get(request.GET['id'])
|
||||
else:
|
||||
extension = request.POST['filename'].split('.')
|
||||
name = '.'.join(extension[:-1])
|
||||
extension = extension[-1].lower()
|
||||
response = json_response(status=400, text='this request requires POST')
|
||||
if 'chunk' in request.FILES:
|
||||
form = ChunkForm(request.POST, request.FILES)
|
||||
if form.is_valid() and file.editable(request.user):
|
||||
c = form.cleaned_data['chunk']
|
||||
chunk_id = form.cleaned_data['chunkId']
|
||||
response = {
|
||||
'result': 1,
|
||||
'resultUrl': request.build_absolute_uri(file.get_absolute_url())
|
||||
}
|
||||
if not file.save_chunk(c, chunk_id, form.cleaned_data['done']):
|
||||
response['result'] = -1
|
||||
if form.cleaned_data['done']:
|
||||
response['done'] = 1
|
||||
return render_to_json_response(response)
|
||||
#init upload
|
||||
else:
|
||||
created = False
|
||||
num = 1
|
||||
_name = name
|
||||
while not created:
|
||||
file, created = models.Document.objects.get_or_create(
|
||||
user=request.user, name=name, extension=extension)
|
||||
if not created:
|
||||
num += 1
|
||||
name = _name + ' [%d]' % num
|
||||
file.name = name
|
||||
file.extension = extension
|
||||
file.uploading = True
|
||||
file.save()
|
||||
upload_url = request.build_absolute_uri('/api/upload/file?id=%s' % file.get_id())
|
||||
return render_to_json_response({
|
||||
'uploadUrl': upload_url,
|
||||
'url': request.build_absolute_uri(file.get_absolute_url()),
|
||||
'result': 1
|
||||
})
|
||||
return render_to_json_response(response)
|
||||
Loading…
Add table
Add a link
Reference in a new issue