Permalink
Browse files

Updates

  • Loading branch information...
1 parent 9363e29 commit 873580e6e3866cc6290136316d7b7906b506d5c3 @igniteflow igniteflow committed Feb 15, 2013
Showing with 277 additions and 94 deletions.
  1. +8 −0 openkm/admin.py
  2. +45 −16 openkm/client.py
  3. +11 −11 openkm/facades.py
  4. +56 −16 openkm/models.py
  5. +30 −0 openkm/services.py
  6. +104 −42 openkm/sync.py
  7. +1 −1 openkm/tests.py
  8. +22 −8 openkm/views.py
View
@@ -0,0 +1,8 @@
+from django.contrib import admin
+
+import openkm
+
+
+class OpenKMEventAdmin(admin.ModelAdmin):
+ pass
+admin.site.register(openkm.models.OpenKMEvent, OpenKMEventAdmin)
View
@@ -1,4 +1,6 @@
-import sys, logging
+import sys
+import logging
+import datetime
from functools import wraps
from django.conf import settings
@@ -8,6 +10,7 @@
import exceptions
+from openkm.services import OpenKMAuditService
logging.getLogger('suds.client').setLevel(logging.INFO)
PATH = settings.OPENKM['configuration']['Path']
@@ -40,9 +43,6 @@ def wrapped(*args, **kwargs):
raise exception, exception(e), tb
return wraps(fn)(wrapped)
-def get_service(class_name):
- return Client(OPENKM_WSDLS[class_name]).service
-
def get_client(class_name):
return Client(OPENKM_WSDLS[class_name])
@@ -54,13 +54,13 @@ def get_token():
class BaseService(object):
- def __init__(self, start_session=True, class_name=None):
+ def __init__(self, start_session=True, class_name=None, log_events=True):
if not class_name:
class_name = self.__class__.__name__
- self.service = get_service(class_name)
self.client = get_client(class_name)
- if start_session:
- self.token = get_token()
+ self.service = self.client.service
+ self.token = get_token() if start_session else None
+ self.log_events = log_events if log_events else None
class Auth(BaseService):
@@ -155,7 +155,6 @@ def rename(self, doc_path, new_name):
"""
return self.service.rename(token=self.token, docPath=doc_path, newName=new_name)
-
def move(self, doc_path, new_name):
"""
Move a document to another location in the repository.
@@ -165,7 +164,6 @@ def move(self, doc_path, new_name):
"""
return self.service.move(token=self.token, docPath=doc_path, newName=new_name)
-
def get_properties(self, doc_path):
"""
Obtain document properties from the repository.
@@ -184,21 +182,33 @@ def set_properties(self, doc):
return self.service.setProperties(token=self.token, doc=doc)
- def set_content(self, doc_path, content):
+ def set_content(self, doc_path, content, comment=None):
"""
Set document content in the repository.
:param doc_path string
:param content byte array (Java)
+ :param comment string
:return none
+
"""
- return self.service.setContent(token=self.token, docPath=doc_path, content=content)
+ if not comment:
+ comment = "No comment"
+ return self.service.setContent(token=self.token, docPath=doc_path, content=content, comment=comment)
- def get_content(self, doc_path, checkout):
+ def get_content(self, doc_path, checkout=False):
+ """Obtain document content from the repository.
+ :param doc_path string
+ :param checkout boolean
+ """
+ return self.service.getContent(token=self.token, docPath=doc_path, checkout=checkout)
+
+ def get_content_by_uuid(self, uuid, checkout=False):
"""Obtain document content from the repository.
:param doc_path string
:param checkout boolean
"""
+ doc_path = self.get_path(uuid)
return self.service.getContent(token=self.token, docPath=doc_path, checkout=checkout)
@@ -228,14 +238,19 @@ def cancel_checkout(self, doc_path):
"""
return self.service.cancelCheckout(token=self.token, docPath=doc_path)
+ def force_cancel_checkout(self, doc_path):
+ return self.service.forceCancelCheckout(token=self.token, docPath=doc_path)
- def checkin(self, doc_path):
+ def force_unlock(self, doc_path):
+ return self.service.forceUnlock(token=self.token, docPath=doc_path)
+
+ def checkin(self, doc_path, comment='Document update'):
"""
Check in the document to create a new version.
:param doc_path string
:return A version object with the properties of the new generated version.
"""
- return self.service.checkin(token=self.token, docPath=doc_path)
+ return self.service.checkin(token=self.token, docPath=doc_path, comment=comment)
def get_version_history(self, doc_path):
@@ -307,9 +322,10 @@ def create_document(self, content, data):
"""
if not hasattr(self.service, 'createDocument'):
raise AttributeError('createDocument is not available on your instance of OpenKM')
+ if self.log_events:
+ OpenKMAuditService().record_update(self.token, content, occured=datetime.datetime.now())
return self.service.createDocument(token=self.token, content=content, data=data)
-
def update_document(self, data):
"""
Custom web service to update a document and all associated metadata in a single call
@@ -318,8 +334,21 @@ def update_document(self, data):
"""
if not hasattr(self.service, 'updateDocument'):
raise AttributeError('updateDocument is not available on your instance of OpenKM')
+ if self.log_events:
+ OpenKMAuditService().record_create(self.token, data, occured=datetime.datetime.now())
return self.service.updateDocument(token=self.token, data=data)
+ def preview_document(self, uuid, format, version=None):
+ """
+ Custom web service to get PDF preview of document given a uuid, format and version
+ """
+ if not hasattr(self.service, 'previewDocument'):
+ raise AttributeError('previewDocument is not available on your instance of OpenKM')
+ return self.service.previewDocument(token=self.token, uuid=uuid, format=format, version=version)
+
+ def get_categories(self):
+ return self.service.getCategories(token=self.token)
+
class Search(BaseService):
"""Methods related to repository search. """
View
@@ -325,15 +325,15 @@ def get_language_label(self, language_code):
return 'English'
def update_options_list(self, options, new_value):
- for option in options:
- print option.label, '==', new_value
- if (option.label == new_value) or (option.value == new_value):
- option.selected = True
- print('Updating option[%s].selected to True', option.label)
- else:
- option.selected = False
+ for option in options:
+ print option.label, '==', new_value
+ if (option.label == new_value) or (option.value == new_value):
+ option.selected = True
+ print('Updating option[%s].selected to True', option.label)
+ else:
+ option.selected = False
- return options
+ return options
def update_document_on_openkm(self, node_path, group_name, properties):
self.property_group.remove_group(node_path, group_name)
@@ -408,10 +408,10 @@ def generate_path_dependencies(self, folders):
def build_path(self, dependencies, root_path=settings.OPENKM['configuration']['UploadRoot']):
"""
- Check if root folder exists, then dependencies in order. If they don't exist then they are
- created.
+ Check if root folder exists, then dependencies in order. If they don't exist then they are
+ created.
:param dependencies: (list of strings) eg. ['sports', 'football']
- :param root_path: (string - optional) eg. '/okm:root/'
+ :param root_path: (string - optional) eg. '/okm:root/'
"""
folder = client.Folder()
repository = client.Repository()
View
@@ -1,12 +1,15 @@
import datetime
import operator
import logging
+import pickle
from django.db import models
from django.conf import settings
from django.db.models import Q
+from django.template.defaultfilters import filesizeformat
+from django.forms import forms
-import facades
+import openkm
class OpenKmMetadata(models.Model):
@@ -19,6 +22,7 @@ class OpenKmMetadata(models.Model):
okm_permissions = models.CharField(max_length=255, blank=True, null=True)
okm_subscribed = models.CharField(max_length=255, blank=True, null=True)
okm_uuid = models.CharField(max_length=255, blank=True, null=True)
+ okm_latest_version = models.CharField(max_length=255, default='None')
class Meta:
abstract = True
@@ -38,9 +42,11 @@ def custom_path_query(self, and_predicates, or_predicates):
return []
return [resource.okm_uuid for resource in query_set]
except TypeError, e:
- logging.exception(e)
+ logging.debug(e)
except AttributeError, e:
- logging.exception(e)
+ logging.debug(e)
+ except Exception, e:
+ logging.debug(e)
def get_custom_queryset(self, and_predicates, or_predicates):
@@ -49,6 +55,7 @@ def get_custom_queryset(self, and_predicates, or_predicates):
:param or_predicates: list of OR query arguments eg. ['Latin-America', 'EMEA']
:returns queryset
"""
+
if and_predicates:
and_predicates_list = self._build_and_predicate_list(and_predicates)
and_list = [Q(x) for x in and_predicates_list]
@@ -73,9 +80,11 @@ def get_custom_queryset(self, and_predicates, or_predicates):
return query_set
except TypeError, e:
- logging.exception(e)
+ logging.debug(e)
except AttributeError, e:
- logging.exception(e)
+ logging.debug(e)
+ except Exception, e:
+ logging.debug(e)
def _build_and_predicate_list(self, arguments):
args = []
@@ -87,7 +96,7 @@ def _build_and_predicate_list(self, arguments):
return args
def _build_or_predicate_list(self, arguments):
- return [('okm_path__icontains', '/%s' % argument) for argument in arguments]
+ return [('okm_path__icontains', '%s' % argument) for argument in arguments]
class OpenKmFolderList(OpenKmMetadata):
@@ -104,14 +113,33 @@ class Meta:
verbose_name_plural = verbose_name
+class OpenKmFileField(models.FileField):
+ def __init__(self, *args, **kwargs):
+ self.max_upload_size = kwargs.pop("max_upload_size")
+
+ super(OpenKmFileField, self).__init__(*args, **kwargs)
+
+ def clean(self, *args, **kwargs):
+ data = super(OpenKmFileField, self).clean(*args, **kwargs)
+
+ file = data.file
+ try:
+ if file._size > self.max_upload_size:
+ raise forms.ValidationError('Please keep filesize under %s. Current filesize %s' % (filesizeformat(self.max_upload_size), filesizeformat(file._size)))
+ except AttributeError:
+ pass
+
+ return data
+
+
class OpenKmDocument(OpenKmMetadata):
okm_filename = models.CharField(max_length=255, blank=True, null=True)
- file = models.FileField(max_length=255, upload_to='resources/%Y/%m/%d/', blank=True, null=True, help_text="Upload a file from your local machine")
+ # file = OpenKmFileField(max_length=255, max_upload_size=104857600, upload_to='resources/%Y/%m/%d/', blank=True, null=True, help_text="Upload a file from your local machine")
def upload_to_openkm(self, file_obj, taxonomy=[]):
"""Uploads the document to the OpenKM server """
- document_manager = facades.DocumentManager()
+ document_manager = openkm.facades.DocumentManager()
return document_manager.create(file_obj, taxonomy=taxonomy)
def set_model_fields(self, openkm_document):
@@ -120,15 +148,17 @@ def set_model_fields(self, openkm_document):
by OpenKM to identify the resource
"""
self.okm_author = openkm_document.author if openkm_document.author else ''
- self.okm_created = openkm_document.created
+ # self.okm_created = openkm_document.created
+ self.okm_created = datetime.datetime.now()
self.okm_path = openkm_document.path
self.okm_permissions = openkm_document.permissions
self.okm_subscribed = openkm_document.subscribed
self.okm_uuid = openkm_document.uuid
+ self.okm_latest_version = openkm_document.actualVersion.name
if hasattr(self, 'mime_type'):
self.mime_type = openkm_document.mimeType
- file_system = facades.FileSystem()
+ file_system = openkm.facades.FileSystem()
self.okm_filename = file_system.get_file_name_from_path(openkm_document.path)
def __unicode__(self):
@@ -147,11 +177,21 @@ def okm_date_string(self, date):
"""
if not isinstance(date, datetime.date):
raise Exception('Argument must be a datetime.date object')
- return date.strftime('%Y-%m-%dT00:00:00.356+01:00')
-
-
-
-
-
+ # return date.strftime('%Y-%m-%dT00:00:00.356+01:00')
+ return date.strftime('%Y%m%d%H%M%S')
+
+
+class OpenKMEvent(models.Model):
+ occured = models.DateTimeField()
+ recorded = models.DateTimeField(auto_now_add=True, null=True, blank=True)
+ token = models.CharField(max_length=255, blank=True, null=True)
+ content = models.TextField(blank=True, null=True)
+ extra = models.TextField(blank=True, null=True)
+
+ def save(self, *args, **kwargs):
+ # import ipdb; ipdb.set_trace()
+ if self.content:
+ self.content = pickle.dumps(self.content)
+ super(OpenKMEvent, self).save(*args, **kwargs)
View
@@ -0,0 +1,30 @@
+from openkm import models
+
+
+class OpenKMAuditService(object):
+
+ def record_update(self, token, content, occured):
+ """
+ token (string)
+ content (instance of document) as returned by
+ openkm.client.Document.create_document_content_object
+ occured (datetime.datetime)
+ """
+ return models.OpenKMEvent.objects.create(
+ token=token,
+ content=content,
+ occured=occured
+ )
+
+ def record_create(self, token, content, occured):
+ """
+ token (string)
+ content (instance of document) as returned by
+ openkm.client.Document.create_document_content_object
+ occured (datetime.datetime)
+ """
+ return models.OpenKMEvent.objects.create(
+ token=token,
+ content=content,
+ occured=occured
+ )
Oops, something went wrong.

0 comments on commit 873580e

Please sign in to comment.