@@ -82,7 +82,8 @@ def save(self, waitForSync = False, **docArgs) :
8282 update = True
8383
8484 data = r .json ()
85- if (r .status_code == 201 or r .status_code == 202 ) and not data ['error' ] :
85+
86+ if (r .status_code == 201 or r .status_code == 202 ) and "error" not in data :
8687 if update :
8788 self ._rev = data ['_rev' ]
8889 else :
@@ -95,6 +96,8 @@ def save(self, waitForSync = False, **docArgs) :
9596
9697 self .modified = False
9798
99+ self ._patchStore = {}
100+
98101 def forceSave (self , ** docArgs ) :
99102 "saves even if the document has not been modified since the last save"
100103 self .modified = True
@@ -125,20 +128,23 @@ def patch(self, keepNull = True, **docArgs) :
125128
126129 r = self .connection .session .patch (self .URL , params = params , data = payload )
127130 data = r .json ()
128- if (r .status_code == 201 or r .status_code == 202 ) and not data [ 'error' ] :
131+ if (r .status_code == 201 or r .status_code == 202 ) and "error" not in data :
129132 self ._rev = data ['_rev' ]
130133 else :
131134 raise UpdateError (data ['errorMessage' ], data )
132135
133136 self .modified = False
134137
138+ self ._patchStore = {}
139+
135140 def delete (self ) :
136141 "deletes the document from the database"
137142 if self .URL is None :
138143 raise DeletionError ("Can't delete a document that was not saved" )
139144 r = self .connection .session .delete (self .URL )
140145 data = r .json ()
141- if (r .status_code != 200 and r .status_code != 202 ) or data ['error' ] :
146+
147+ if (r .status_code != 200 and r .status_code != 202 ) or 'error' in data :
142148 raise DeletionError (data ['errorMessage' ], data )
143149 self .reset (self .collection )
144150
@@ -218,52 +224,37 @@ def reset(self, edgeCollection, jsonFieldInit = {}) :
218224 Document .reset (self , edgeCollection , jsonFieldInit )
219225 self .typeName = "ArangoEdge"
220226
221- def setPrivates (self , jsonFieldInit ) :
222- try :
223- self ._from = jsonFieldInit ["_from" ]
224- del (jsonFieldInit ["_from" ])
225- self ._to = jsonFieldInit ["_to" ]
226- del (jsonFieldInit ["_to" ])
227- except KeyError :
228- self ._from , self ._to = None , None
229- Document .setPrivates (self , jsonFieldInit )
230-
231227 def links (self , fromVertice , toVertice , ** edgeArgs ) :
232- "An alias of save that works only for first saves. It will also trigger the saving of fromVertice and toVertice"
233- if self .URL is not None :
234- raise AttributeError ("It appears that the edge has already been saved. You can now use save() and patch()" )
235-
236- fromVertice .save ()
237- toVertice .save ()
238-
239- self .save (fromVertice , toVertice , ** edgeArgs )
240-
241- def save (self , fromVertice = None , toVertice = None , ** edgeArgs ) :
242- """Works like Document's except that the first time you save an Edge you must specify the 'from' and 'to' vertices.
243- There's also a links() function especially for first saves"""
244- import types
245-
246- if self .URL is None and (fromVertice is None or toVertice is None ) :
247- raise ValueError ("The first time you save an Edge you must specify the 'from' and 'to' vertices" )
228+ """
229+ An alias to save that updates the _from and _to attributes.
230+ fromVertice and toVertice, can be either strings or documents. It they are unsaved documents, they will be automatically saved.
231+ """
248232
249233 if fromVertice .__class__ is Document :
250- edgeArgs ["from" ] = fromVertice ._id
251- self ._from = edgeArgs ["from" ]
234+ if not fromVertice ._id :
235+ fromVertice ._id .save ()
236+
237+ self ["_from" ] = fromVertice ._id
252238 elif (type (fromVertice ) is types .StringType ) or (type (fromVertice ) is types .UnicodeType ) :
253- edgeArgs ["from" ] = fromVertice
254- self ._from = edgeArgs ["from" ]
255- else :
256- if not self ._from :
257- raise ValueError ("fromVertice must be either a Document or a String, got: %s" % fromVertice )
239+ self ["_from" ] = fromVertice
258240
259241 if toVertice .__class__ is Document :
260- edgeArgs ["to" ] = toVertice ._id
261- self ._to = edgeArgs ["to" ]
242+ if not toVertice ._id :
243+ toVertice ._id .save ()
244+
245+ self ["_to" ] = toVertice ._id
262246 elif (type (toVertice ) is types .StringType ) or (type (toVertice ) is types .UnicodeType ) :
263- edgeArgs ["to" ] = toVertice
264- self ._to = edgeArgs ["to" ]
265- else :
266- if not self ._to :
267- raise ValueError ("toVertice must be either a Document or a String, got: %s" % fromVertice )
247+ self ["_to" ] = toVertice
248+
249+ self .save (** edgeArgs )
250+
251+ def save (self , ** edgeArgs ) :
252+ """Works like Document's except that you must specify '_from' and '_to' vertices before.
253+ There's also a links() function especially for first saves."""
254+
255+ import types
256+
257+ if "_from" not in self ._store or "_to" not in self ._store :
258+ raise AttributeError ("You must specify '_from' and '_to' attributes before saving. You can also use the function 'links()'" )
268259
269260 Document .save (self , ** edgeArgs )
0 commit comments