Permalink
Browse files

Merge pull request #23 from gustavofonseca/master

fixed bug when updating attached files
  • Loading branch information...
2 parents 8c7a990 + ea5cb14 commit 7a26cbd9f9ca60aed872d3331e9fcddd1a311f29 @gustavofonseca gustavofonseca committed Nov 9, 2011
Showing with 31 additions and 25 deletions.
  1. +16 −14 isis/model/couchdb.py
  2. +3 −3 isis/model/tests/test_couchdb.py
  3. +12 −8 setup.py
View
@@ -33,10 +33,10 @@ def _attach_exists(old_doc, property_name):
return True
except (AttributeError, TypeError):
pass
-
+
return False
-def _attach_updated(new_doc, property_name):
+def _attach_updated(new_doc, property_name):
try:
return new_doc[property_name]['fp'] is not None
except KeyError:
@@ -64,12 +64,12 @@ def to_python(self):
pstruct['_id'] = self._id
except AttributeError:
pass
-
+
try:
pstruct['_rev'] = self._rev
except AttributeError:
pass
-
+
return pstruct
@classmethod
@@ -79,19 +79,20 @@ def from_python(cls, pystruct):
if pystruct.get('_rev',None) == 'None':
pystruct['_rev'] = None
-
+
return super(CouchdbDocument, cls).from_python(pystruct)
def save(self, db):
new_doc = self.to_python()
new_doc = self.__clean_before_save(new_doc)
-
+
old_doc = db.get(new_doc['_id']) if '_rev' in new_doc else None
-
+
while True:
try:
if old_doc is not None and '_attachments' in old_doc:
new_doc['_attachments'] = old_doc['_attachments']
+
db.save_doc(new_doc)
break
except couchdbkit.ResourceConflict:
@@ -100,29 +101,30 @@ def save(self, db):
for key in self.__class__:
prop = self.__class__.__getattribute__(self.__class__,key)
- if isinstance(prop, FileProperty):
+ if isinstance(prop, FileProperty):
+
if old_doc is None:
- #New document
+ #New document
file_metadata = getattr(self,key, None)
if file_metadata:
db.put_attachment(new_doc, file_metadata['fp'], getattr(self, key)['filename'])
- elif _attach_exists(old_doc, key) and not _attach_updated(new_doc, key):
+ elif _attach_exists(old_doc, key) and not hasattr(self,key):
#Attachment exists and had not been changed
new_doc[key] = old_doc[key]
db.save_doc(new_doc)
else:
#Attachment updated
file_metadata = getattr(self,key, None)
- if file_metadata:
+ if file_metadata and file_metadata.get('fp'):
db.put_attachment(new_doc, file_metadata['fp'], getattr(self, key)['filename'])
self._id, self._rev = new_doc['_id'], new_doc['_rev']
-
+
@classmethod
def get(cls, db, doc_id, controls=True):
doc = db.get(doc_id)
couchdocument = cls.from_python(doc)
-
+
if not controls:
del(couchdocument._id)
del(couchdocument._rev)
@@ -133,7 +135,7 @@ def get(cls, db, doc_id, controls=True):
@classmethod
def get_schema(cls, controls=True):
schema = super(CouchdbDocument, cls).get_schema()
-
+
if controls:
rev_definition = colander.SchemaNode(colander.String(),
widget = deform.widget.HiddenWidget(),
@@ -65,8 +65,8 @@
----------------------------------------
_attach_updated method tests
----------------------------------------
-
- >>> updated_attach = {u'title': u'Title example', u'_rev': u'25-9b1b088acc5d42cc9cb97bf18781811b', u'cover': {u'filename': u'updated.xls', u'uid': u'BCE0I1KH1Y', u'md5': u'7da75d0e101dbf5c2eec9e117aj32b7'}, u'_attachments': {u'updated.xls': {u'stub': True, u'length': 19408, u'revpos': 26, u'content_type': u'application/vnd.ms-excel'}}, u'_id': u'rhvkd', u'TYPE': u'Monograph'}
+ >>> tmpfile = open(__file__)
+ >>> updated_attach = {u'title': u'Title example', u'_rev': u'25-9b1b088acc5d42cc9cb97bf18781811b', u'cover': {u'filename': u'updated.xls', u'uid': u'BCE0I1KH1Y', u'md5': u'7da75d0e101dbf5c2eec9e117aj32b7', u'fp': tmpfile}, u'_attachments': {u'updated.xls': {u'stub': True, u'length': 19408, u'revpos': 26, u'content_type': u'application/vnd.ms-excel'}}, u'_id': u'rhvkd', u'TYPE': u'Monograph'}
>>> updated_doc = {u'title': u'Title updated', u'_rev': u'25-9b1b088acc5d42cc9cb97bf18781811b', u'_attachments': {u'campos lilacs_scielolivros.xls': {u'stub': True, u'length': 17408, u'revpos': 25, u'content_type': u'application/vnd.ms-excel'}}, u'_id': u'rhvkd', u'TYPE': u'Monograph'}
>>> updated_doc_no_attach = {u'title': u'Title updated', u'_rev': u'25-9b1b088acc5d42cc9cb97bf18781811b', u'_id': u'rhvkd', u'TYPE': u'Monograph'}
>>> _attach_updated(updated_attach, 'cover')
@@ -86,7 +86,7 @@
False
>>> _attach_exists(old_doc, 'not_exists')
False
-
+
"""
from isis.model import CouchdbDocument
from isis.model import TextProperty, MultiTextProperty
View
@@ -2,17 +2,20 @@
import os
here = os.path.abspath(os.path.dirname(__file__))
+try:
+ README = open(os.path.join(here, 'README.txt')).read()
+ CHANGES = open(os.path.join(here, 'CHANGES.txt')).read()
+except IOError:
+ README = CHANGES = ''
-README_FILEPATH = os.path.join(here, 'README.txt')
-CHANGES_FILEPATH = os.path.join(here, 'CHANGES.txt')
+requirements = ['colander',
+ 'deform',
+ 'couchdbkit']
-README = open(README_FILEPATH).read()
-CHANGES = open(CHANGES_FILEPATH).read()
+additional_files = ['README.txt',
+ 'CHANGES.txt']
-requirements = ['colander', 'deform', 'couchdbkit']
-additional_files = [README_FILEPATH, CHANGES_FILEPATH]
-
-setup(name='isisdm', version='0.2.1',
+setup(name='isisdm', version='0.2.2',
packages=find_packages(),
long_description=README + "\n\n" + CHANGES,
namespace_packages=['isis'],
@@ -31,6 +34,7 @@
download_url='https://github.com/bireme/isisdm',
license="LGPL v2.1 (http://www.gnu.org/licenses/lgpl-2.1.txt)",
install_requires=requirements,
+ include_package_data=True,
test_suite='isis.model',
tests_require=['Nose'],
description='ISIS-DM allows programmers to express constraints and functionality similar to those of the CDS/ISIS Field Definition Table and the CDS/ISIS Formatting Language, but in contemporary, object-oriented programming languages.',

0 comments on commit 7a26cbd

Please sign in to comment.