Cleanups, unused import, unused arguments removal
This commit is contained in:
@@ -1,104 +0,0 @@
|
|||||||
# better_yaml.py
|
|
||||||
|
|
||||||
"""
|
|
||||||
Customized YAML serializer, with more condensed and readable output.
|
|
||||||
Rather than producing a flat list of objects with the same three attributes:
|
|
||||||
|
|
||||||
- fields: {...}
|
|
||||||
model: modelname
|
|
||||||
pk: 123
|
|
||||||
|
|
||||||
This serializer nests the data, grouping by model name, then indexing by
|
|
||||||
primary key. For example, instead of this output, as produced by the default
|
|
||||||
YAML serializer:
|
|
||||||
|
|
||||||
- fields: {name: blue}
|
|
||||||
model: app.firstmodel
|
|
||||||
pk: 3
|
|
||||||
- fields: {name: red}
|
|
||||||
model: app.firstmodel
|
|
||||||
pk: 1
|
|
||||||
- fields: {name: green}
|
|
||||||
model: app.firstmodel
|
|
||||||
pk: 2
|
|
||||||
- fields: {name: crumbly}
|
|
||||||
model: app.secondmodel
|
|
||||||
pk: 2
|
|
||||||
- fields: {name: squishy}
|
|
||||||
model: app.secondmodel
|
|
||||||
pk: 1
|
|
||||||
|
|
||||||
You'll get this output:
|
|
||||||
|
|
||||||
app.firstmodel:
|
|
||||||
1: {name: red}
|
|
||||||
2: {name: green}
|
|
||||||
3: {name: blue}
|
|
||||||
app.secondmodel:
|
|
||||||
1: {name: squishy}
|
|
||||||
2: {name: crumbly}
|
|
||||||
|
|
||||||
To use this customized serializer and deserializer, save this file
|
|
||||||
somewhere in your Django project, then add this to your settings.py:
|
|
||||||
|
|
||||||
SERIALIZATION_MODULES = {
|
|
||||||
'yaml': 'path.to.better_yaml',
|
|
||||||
}
|
|
||||||
|
|
||||||
Note that this serializer is NOT compatible with the default Django
|
|
||||||
YAML serializer; this one uses nested dictionaries, while the default
|
|
||||||
one uses a flat list of object dicts.
|
|
||||||
|
|
||||||
Requires PyYaml (http://pyyaml.org/), of course.
|
|
||||||
"""
|
|
||||||
|
|
||||||
from StringIO import StringIO
|
|
||||||
import yaml
|
|
||||||
|
|
||||||
from django.core.serializers.python import Deserializer as PythonDeserializer
|
|
||||||
from django.core.serializers.pyyaml import Serializer as YamlSerializer
|
|
||||||
from django.utils.encoding import smart_unicode
|
|
||||||
|
|
||||||
|
|
||||||
class Serializer (YamlSerializer):
|
|
||||||
"""
|
|
||||||
Serialize database objects as nested dicts, indexed first by
|
|
||||||
model name, then by primary key.
|
|
||||||
"""
|
|
||||||
def start_serialization(self):
|
|
||||||
self._current = None
|
|
||||||
self.objects = {}
|
|
||||||
|
|
||||||
def end_object(self, obj):
|
|
||||||
model = smart_unicode(obj._meta)
|
|
||||||
pk = obj._get_pk_val()
|
|
||||||
|
|
||||||
if model not in self.objects:
|
|
||||||
self.objects[model] = {}
|
|
||||||
|
|
||||||
self.objects[model][pk] = self._current
|
|
||||||
self._current = None
|
|
||||||
|
|
||||||
|
|
||||||
def Deserializer(stream_or_string, **options):
|
|
||||||
"""
|
|
||||||
Deserialize a stream or string of YAML data,
|
|
||||||
as written by the Serializer above.
|
|
||||||
"""
|
|
||||||
if isinstance(stream_or_string, basestring):
|
|
||||||
stream = StringIO(stream_or_string)
|
|
||||||
else:
|
|
||||||
stream = stream_or_string
|
|
||||||
|
|
||||||
# Reconstruct the flat object list as PythonDeserializer expects
|
|
||||||
# NOTE: This could choke on large data sets, since it
|
|
||||||
# constructs the flattened data list in memory
|
|
||||||
data = []
|
|
||||||
for model, objects in yaml.load(stream).iteritems():
|
|
||||||
# Add the model name back into each object dict
|
|
||||||
for pk, fields in objects.iteritems():
|
|
||||||
data.append({'model': model, 'pk': pk, 'fields': fields})
|
|
||||||
|
|
||||||
# Deserialize the flattened data
|
|
||||||
for obj in PythonDeserializer(data, **options):
|
|
||||||
yield obj
|
|
||||||
@@ -174,7 +174,8 @@ def validate_path(path):
|
|||||||
# If doesn't exist try to create it
|
# If doesn't exist try to create it
|
||||||
try:
|
try:
|
||||||
os.mkdir(path)
|
os.mkdir(path)
|
||||||
except:
|
except Exception as exception:
|
||||||
|
logger.debug('unhandled exception: %s', exception)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Check if it is writable
|
# Check if it is writable
|
||||||
@@ -182,7 +183,8 @@ def validate_path(path):
|
|||||||
fd, test_filepath = tempfile.mkstemp(dir=path)
|
fd, test_filepath = tempfile.mkstemp(dir=path)
|
||||||
os.close(fd)
|
os.close(fd)
|
||||||
os.unlink(test_filepath)
|
os.unlink(test_filepath)
|
||||||
except:
|
except Exception as exception:
|
||||||
|
logger.debug('unhandled exception: %s', exception)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|||||||
@@ -11,8 +11,6 @@ from documents.models import Document, DocumentType
|
|||||||
from .managers import IndexManager
|
from .managers import IndexManager
|
||||||
from .settings import AVAILABLE_INDEXING_FUNCTIONS
|
from .settings import AVAILABLE_INDEXING_FUNCTIONS
|
||||||
|
|
||||||
available_indexing_functions_string = (_(u'Available functions: %s') % u','.join([u'%s()' % name for name, function in AVAILABLE_INDEXING_FUNCTIONS.items()])) if AVAILABLE_INDEXING_FUNCTIONS else u''
|
|
||||||
|
|
||||||
|
|
||||||
class Index(models.Model):
|
class Index(models.Model):
|
||||||
name = models.CharField(unique=True, max_length=64, verbose_name=_(u'Name'), help_text=_(u'Internal name used to reference this index.'))
|
name = models.CharField(unique=True, max_length=64, verbose_name=_(u'Name'), help_text=_(u'Internal name used to reference this index.'))
|
||||||
@@ -43,7 +41,7 @@ class Index(models.Model):
|
|||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
"""Automatically create the root index template node"""
|
"""Automatically create the root index template node"""
|
||||||
super(Index, self).save(*args, **kwargs)
|
super(Index, self).save(*args, **kwargs)
|
||||||
index_template_node_root, created = IndexTemplateNode.objects.get_or_create(parent=None, index=self)
|
IndexTemplateNode.objects.get_or_create(parent=None, index=self)
|
||||||
|
|
||||||
def get_document_types_names(self):
|
def get_document_types_names(self):
|
||||||
return u', '.join([unicode(document_type) for document_type in self.document_types.all()] or [u'All'])
|
return u', '.join([unicode(document_type) for document_type in self.document_types.all()] or [u'All'])
|
||||||
@@ -70,7 +68,6 @@ class IndexTemplateNode(MPTTModel):
|
|||||||
parent = TreeForeignKey('self', null=True, blank=True)
|
parent = TreeForeignKey('self', null=True, blank=True)
|
||||||
index = models.ForeignKey(Index, verbose_name=_(u'Index'), related_name='node_templates')
|
index = models.ForeignKey(Index, verbose_name=_(u'Index'), related_name='node_templates')
|
||||||
expression = models.CharField(max_length=128, verbose_name=_(u'Indexing expression'), help_text=_(u'Enter a python string expression to be evaluated.'))
|
expression = models.CharField(max_length=128, verbose_name=_(u'Indexing expression'), help_text=_(u'Enter a python string expression to be evaluated.'))
|
||||||
# % available_indexing_functions_string)
|
|
||||||
enabled = models.BooleanField(default=True, verbose_name=_(u'Enabled'), help_text=_(u'Causes this node to be visible and updated when document data changes.'))
|
enabled = models.BooleanField(default=True, verbose_name=_(u'Enabled'), help_text=_(u'Causes this node to be visible and updated when document data changes.'))
|
||||||
link_documents = models.BooleanField(default=False, verbose_name=_(u'Link documents'), help_text=_(u'Check this option to have this node act as a container for documents and not as a parent for further nodes.'))
|
link_documents = models.BooleanField(default=False, verbose_name=_(u'Link documents'), help_text=_(u'Check this option to have this node act as a container for documents and not as a parent for further nodes.'))
|
||||||
|
|
||||||
|
|||||||
@@ -110,14 +110,14 @@ class APIDocumentFolderListView(generics.ListAPIView):
|
|||||||
|
|
||||||
|
|
||||||
class APIFolderDocumentView(views.APIView):
|
class APIFolderDocumentView(views.APIView):
|
||||||
def delete(self, request, *args, **kwargs):
|
def delete(self, request):
|
||||||
"""Remove a document from the selected folder."""
|
"""Remove a document from the selected folder."""
|
||||||
|
|
||||||
folder = get_object_or_404(Folder, pk=self.kwargs['pk'])
|
folder = get_object_or_404(Folder, pk=self.kwargs['pk'])
|
||||||
try:
|
try:
|
||||||
Permission.objects.check_permissions(self.request.user, [PERMISSION_FOLDER_REMOVE_DOCUMENT])
|
Permission.objects.check_permissions(request.user, [PERMISSION_FOLDER_REMOVE_DOCUMENT])
|
||||||
except PermissionDenied:
|
except PermissionDenied:
|
||||||
AccessEntry.objects.check_access(PERMISSION_FOLDER_REMOVE_DOCUMENT, self.request.user, folder)
|
AccessEntry.objects.check_access(PERMISSION_FOLDER_REMOVE_DOCUMENT, request.user, folder)
|
||||||
|
|
||||||
document = get_object_or_404(Document, pk=self.kwargs['document_pk'])
|
document = get_object_or_404(Document, pk=self.kwargs['document_pk'])
|
||||||
folder.documents.remove(document)
|
folder.documents.remove(document)
|
||||||
@@ -128,9 +128,9 @@ class APIFolderDocumentView(views.APIView):
|
|||||||
|
|
||||||
folder = get_object_or_404(Folder, pk=self.kwargs['pk'])
|
folder = get_object_or_404(Folder, pk=self.kwargs['pk'])
|
||||||
try:
|
try:
|
||||||
Permission.objects.check_permissions(self.request.user, [PERMISSION_FOLDER_ADD_DOCUMENT])
|
Permission.objects.check_permissions(request.user, [PERMISSION_FOLDER_ADD_DOCUMENT])
|
||||||
except PermissionDenied:
|
except PermissionDenied:
|
||||||
AccessEntry.objects.check_access(PERMISSION_FOLDER_ADD_DOCUMENT, self.request.user, folder)
|
AccessEntry.objects.check_access(PERMISSION_FOLDER_ADD_DOCUMENT, request.user, folder)
|
||||||
|
|
||||||
document = get_object_or_404(Document, pk=self.kwargs['document_pk'])
|
document = get_object_or_404(Document, pk=self.kwargs['document_pk'])
|
||||||
folder.documents.add(document)
|
folder.documents.add(document)
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
import tempfile
|
|
||||||
|
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
|
|
||||||
from converter.exceptions import UnkownConvertError, UnknownFileFormat
|
from converter.exceptions import UnkownConvertError, UnknownFileFormat
|
||||||
|
|||||||
@@ -111,9 +111,9 @@ class APIDocumentTagView(views.APIView):
|
|||||||
|
|
||||||
document = get_object_or_404(Document, pk=self.kwargs['document_pk'])
|
document = get_object_or_404(Document, pk=self.kwargs['document_pk'])
|
||||||
try:
|
try:
|
||||||
Permission.objects.check_permissions(self.request.user, [PERMISSION_TAG_REMOVE])
|
Permission.objects.check_permissions(request.user, [PERMISSION_TAG_REMOVE])
|
||||||
except PermissionDenied:
|
except PermissionDenied:
|
||||||
AccessEntry.objects.check_access(PERMISSION_TAG_REMOVE, self.request.user, document)
|
AccessEntry.objects.check_access(PERMISSION_TAG_REMOVE, request.user, document)
|
||||||
|
|
||||||
tag = get_object_or_404(Tag, pk=self.kwargs['pk'])
|
tag = get_object_or_404(Tag, pk=self.kwargs['pk'])
|
||||||
tag.documents.remove(document)
|
tag.documents.remove(document)
|
||||||
@@ -126,9 +126,9 @@ class APIDocumentTagView(views.APIView):
|
|||||||
|
|
||||||
document = get_object_or_404(Document, pk=self.kwargs['document_pk'])
|
document = get_object_or_404(Document, pk=self.kwargs['document_pk'])
|
||||||
try:
|
try:
|
||||||
Permission.objects.check_permissions(self.request.user, [PERMISSION_TAG_ATTACH])
|
Permission.objects.check_permissions(request.user, [PERMISSION_TAG_ATTACH])
|
||||||
except PermissionDenied:
|
except PermissionDenied:
|
||||||
AccessEntry.objects.check_access(PERMISSION_TAG_ATTACH, self.request.user, document)
|
AccessEntry.objects.check_access(PERMISSION_TAG_ATTACH, request.user, document)
|
||||||
|
|
||||||
tag = get_object_or_404(Tag, pk=self.kwargs['pk'])
|
tag = get_object_or_404(Tag, pk=self.kwargs['pk'])
|
||||||
tag.documents.add(document)
|
tag.documents.add(document)
|
||||||
|
|||||||
Reference in New Issue
Block a user