@@ -63,6 +63,11 @@ def read_list(self, object_list, bundle):
6363 def create_detail (self , object_list , bundle ):
6464 if not self .check_contribution_permission (object_list , bundle , 'add' ):
6565 raise Unauthorized ("Sorry, only staff or contributors can create resource." )
66+ # check if user can add regarding to his plan
67+ topic = get_topic_from_request (bundle .request )
68+ owner_profile = topic .author .detectiveprofileuser
69+ if owner_profile .nodes_max () > - 1 and owner_profile .nodes_count ()[topic .slug ] >= owner_profile .nodes_max ():
70+ raise Unauthorized ("Sorry, you have to upgrade your plan." )
6671 return True
6772
6873 def update_detail (self , object_list , bundle ):
@@ -489,7 +494,6 @@ def arr_no_dict_dup(in_arr):
489494
490495 def get_patch (self , request , ** kwargs ):
491496 self .method_check (request , allowed = ['post' ])
492- #self.is_authenticated(request)
493497 self .throttle_check (request )
494498 self .is_authenticated (request )
495499 bundle = self .build_bundle (request = request )
@@ -498,7 +502,7 @@ def get_patch(self, request, **kwargs):
498502 try :
499503 node = model .objects .get (id = kwargs ["pk" ])
500504 except ObjectDoesNotExist :
501- raise Http404 ("Sorry, unkown node." )
505+ raise Http404 ("Sorry, unknown node." )
502506 # Parse only body string
503507 body = json .loads (request .body ) if type (request .body ) is str else request .body
504508 # Copy data to allow dictionary resizing
@@ -515,29 +519,48 @@ def get_patch(self, request, **kwargs):
515519 attr = getattr (node , field )
516520 # It's a relationship
517521 if hasattr (attr , "_rel" ):
522+ existing_rels_id = [r .id for r in attr .all ()]
523+ rules = request .current_topic .get_rules ()
524+ # Model that manages properties
525+ though = rules .model ( self .get_model () ).field (field ).get ("through" )
518526 related_model = attr ._rel .relationship .target_model
519- # Clean the field to avoid duplicates
520- attr .clear ()
521527 # Load the json-formated relationships
522528 data [field ] = rels = value
523- # For each relation ...
529+ # For each relationship ...
524530 for idx , rel in enumerate (rels ):
525- if type (rel ) in [str , int ]: rel = dict (id = rel )
531+ if type (rel ) in [str , int ]:
532+ rel = dict (id = rel )
526533 # We receied an object with an id
527534 if rel .has_key ("id" ):
535+ # skip for existing relationships
536+ if rel ["id" ] in existing_rels_id :
537+ continue
528538 # Get the related object
529539 try :
530540 related = related_model .objects .get (id = rel ["id" ])
531- # Creates the relationship between the two objects
532- attr .add (related )
533541 except ObjectDoesNotExist :
534542 del data [field ][idx ]
535543 # Too bad! Go to the next related object
536544 continue
545+ else :
546+ attr .add (related )
547+ # removing unused relationship
548+ rel_type = self .get_model_field (field ).rel_type
549+ for relationship in node .node .relationships .all (types = [rel_type ]):
550+ if relationship .end .id not in [rel ["id" ] for rel in rels ]:
551+ relation_id = relationship .id
552+ relationship .delete ()
553+ try :
554+ property = though .objects .get (_relationship = relation_id )
555+ except ObjectDoesNotExist :
556+ pass
557+ else :
558+ property .delete ()
559+
537560 # It's a literal value and not the ID
538- elif field != 'id' and value is not None :
561+ elif field != 'id' :
539562 field_prop = self .get_model_field (field )._property
540- if isinstance (field_prop , DateProperty ):
563+ if isinstance (field_prop , DateProperty ) and value != None :
541564 try :
542565 # It's a date and therefor `value` should be converted as it
543566 value = datetime .strptime (value , RFC_DATETIME_FORMAT )
@@ -570,15 +593,14 @@ def get_patch(self, request, **kwargs):
570593
571594 def get_relationships (self , request , ** kwargs ):
572595 # Extract node id from given node uri
573- node_id = lambda uri : re .search (r'(\d+)$' , uri ).group (1 )
596+ def node_id ( uri ) : return re .search (r'(\d+)$' , uri ).group (1 )
574597 # Get the end of the given relationship
575- rel_from = lambda rel , side : node_id (rel .__dict__ ["_dic" ][side ])
576- connected = lambda rel , idx : rel_from (rel , "end" ) == idx or rel_from (rel , "start" ) == idx
598+ def rel_from ( rel , side ): return node_id (rel .__dict__ ["_dic" ][side ])
599+ def connected ( rel , idx ): return rel_from (rel , "end" ) == idx or rel_from (rel , "start" ) == idx
577600
578601 self .method_check (request , allowed = ['get' ])
579602 self .throttle_check (request )
580603 pk = kwargs ['pk' ]
581-
582604 node = connection .nodes .get (pk )
583605 # Only the relationships for a given field
584606 if "field" in kwargs :
0 commit comments