comparison roundup/rest.py @ 5593:344b6a87dac6 REST-rebased

Added support to print error .. to output when server DEBUG_MODE is true, some coding improvements committer: Ralf Schlatterbeck <rsc@runtux.com>
author Chau Nguyen <dangchau1991@yahoo.com>
date Wed, 30 Jan 2019 10:26:35 +0100
parents a25d79e874cb
children 864cf6cb5790
comparison
equal deleted inserted replaced
5592:adcb5cbe82bd 5593:344b6a87dac6
41 code = 402 # nothing to pay, just a mark for debugging purpose 41 code = 402 # nothing to pay, just a mark for debugging purpose
42 data = 'Method under development' 42 data = 'Method under development'
43 except: 43 except:
44 exc, val, tb = sys.exc_info() 44 exc, val, tb = sys.exc_info()
45 code = 400 45 code = 400
46 # if self.DEBUG_MODE in roundup_server 46 ts = time.ctime()
47 # else data = 'An error occurred. Please check...', 47 if self.client.request.DEBUG_MODE:
48 data = val 48 data = val
49 49 else:
50 data = '%s: An error occurred. Please check the server log' \
51 ' for more information.' % ts
50 # out to the logfile 52 # out to the logfile
51 print 'EXCEPTION AT', time.ctime() 53 print 'EXCEPTION AT', ts
52 traceback.print_exc() 54 traceback.print_exc()
53 55
54 # decorate it 56 # decorate it
55 self.client.response_code = code 57 self.client.response_code = code
56 if code >= 400: # any error require error format 58 if code >= 400: # any error require error format
68 return format_object 70 return format_object
69 71
70 72
71 class RestfulInstance(object): 73 class RestfulInstance(object):
72 """The RestfulInstance performs REST request from the client""" 74 """The RestfulInstance performs REST request from the client"""
75
76 __default_patch_op = "replace" # default operator for PATCH method
73 77
74 def __init__(self, client, db): 78 def __init__(self, client, db):
75 self.client = client 79 self.client = client
76 self.db = db 80 self.db = db
77 81
619 the object 623 the object
620 """ 624 """
621 try: 625 try:
622 op = input['op'].value.lower() 626 op = input['op'].value.lower()
623 except KeyError: 627 except KeyError:
624 op = "replace" 628 op = self.__default_patch_op
625 class_obj = self.db.getclass(class_name) 629 class_obj = self.db.getclass(class_name)
626 630
627 props = self.props_from_args(class_obj, input.value, item_id) 631 props = self.props_from_args(class_obj, input.value, item_id)
628 632
629 for prop, value in props.iteritems(): 633 for prop, value in props.iteritems():
687 the object 691 the object
688 """ 692 """
689 try: 693 try:
690 op = input['op'].value.lower() 694 op = input['op'].value.lower()
691 except KeyError: 695 except KeyError:
692 op = "replace" 696 op = self.__default_patch_op
693 class_obj = self.db.getclass(class_name)
694 697
695 if not self.db.security.hasPermission( 698 if not self.db.security.hasPermission(
696 'Edit', self.db.getuid(), class_name, attr_name, item_id 699 'Edit', self.db.getuid(), class_name, attr_name, item_id
697 ): 700 ):
698 raise Unauthorised( 701 raise Unauthorised(
816 "Access-Control-Allow-Methods", 819 "Access-Control-Allow-Methods",
817 "HEAD, OPTIONS, GET, PUT, DELETE, PATCH" 820 "HEAD, OPTIONS, GET, PUT, DELETE, PATCH"
818 ) 821 )
819 try: 822 try:
820 class_name, item_id = hyperdb.splitDesignator(resource_uri) 823 class_name, item_id = hyperdb.splitDesignator(resource_uri)
821 except hyperdb.DesignatorError, msg: 824 except hyperdb.DesignatorError:
822 class_name = resource_uri 825 class_name = resource_uri
823 item_id = None 826 item_id = None
824 827
825 # Call the appropriate method 828 # Call the appropriate method
826 if (class_name not in self.db.classes) or (len(uri_split) > 3): 829 if (class_name not in self.db.classes) or (len(uri_split) > 3):

Roundup Issue Tracker: http://roundup-tracker.org/