Ejemplo n.º 1
0
    def update(self, commit=True, **kwargs):
        '''更新数据'''
        url = self._update_url()
        '''post方法'''
        method = HttpMethods.get('post')

        for attr, value in kwargs.items():
            if value is not None:
                setattr(self, attr, value)
        if not commit:
            return None
        try:
            my_http = HttpService(url=url, headers=self.headers, auth=())
            response = my_http.method(method['method'])(js_data=self.to_json())
            if response.status_code == method['code']:
                #for attr, value in response.json().items():
                #	if value is not None:
                #		setattr(self, attr, value)
                #return response.json()
                '''成功返回True, 否则返回False'''
                return response.json()
            else:
                raise DatabaseError('Invalid upload data',
                                    response.status_code, response.text)
        except NetworkError as e:
            print(e.args)
            raise DatabaseError(e.error, 500, e.description)
Ejemplo n.º 2
0
 def get_by_id(cls, user_id):
     id_str = str(user_id)
     url = cls._get_by_id_url()
     method = HttpMethods.get('post')
     try:
         my_http = HttpService(url=url, headers=cls.headers, auth=())
         response = my_http.method(method['method'])(data=id_str)
         if response.status_code == method['code']:
             return cls(**response.json())
         return None
     except NetworkError as e:
         print(e.args)
         raise DatabaseError(e.error, 500, e.description)
Ejemplo n.º 3
0
 def get_user(cls, user_id=None, username=None, email=None):
     url = cls.url + 'exist'
     method = HttpMethods.get('post')
     data = {'id': user_id, 'username': username, 'email': email}
     try:
         my_http = HttpService(url=url, headers=cls.headers, auth=())
         response = my_http.method(method['method'])(js_data=data)
         if response.status_code == method['code']:
             if response.json() == None:  #没有这个用户
                 return None
             return cls(**response.json())
         return None
     except NetworkError as e:
         print(e.args)
         raise DatabaseError(e.error, 500, e.description)
Ejemplo n.º 4
0
    def get_all(cls):

        url = cls._get_all_url()

        method = HttpMethods.get('post')

        try:
            my_http = HttpService(url=url, headers=cls.headers, auth=())
            response = my_http.method(method['method'])()
            if response.status_code == method['code']:
                return response.json()
            return None
        except NetworkError as e:
            print(e.args)
            raise DatabaseError(e.error, 500, e.description)
Ejemplo n.º 5
0
    def delete(self, commit=True):
        '''删除对象'''
        url = self._delete_url()
        method = HttpMethods.get('delete')

        if not commit:
            return None
        try:
            #TODO 此处服务器有问题, 返回的数据不是json
            my_http = HttpService(url=url, headers=self.headers, auth=())
            response = my_http.method(method['method'])()
            return response.json()

        except NetworkError as e:
            print(e.args)
            raise DatabaseError(e.error, 500, e.description)
Ejemplo n.º 6
0
 def verify_password(self, password):
     url = self.url + 'exist'
     data = {
         'username': self.username,
         'email': self.email,
         'password': password
     }
     method = HttpMethods.get('post')
     try:
         my_http = HttpService(url=url, headers=self.headers, auth=())
         response = my_http.method(method['method'])(js_data=data)
         if response.status_code == method['code']:
             return True
         return False
     except NetworkError as e:
         print(e.args)
         raise DatabaseError(e.error, 500, e.description)
Ejemplo n.º 7
0
 def save(self, commit=True):
     '''写入数据'''
     url = self._save_url()
     method = HttpMethods.get('post')
     if not commit:
         return None
     try:
         my_http = HttpService(url=url, headers=self.headers, auth=())
         response = my_http.method(method['method'])(js_data=self.to_json())
         if response.status_code == method['code']:
             '''更新数据,注册后数据库服务器返回id'''
             for attr, value in response.json().items():
                 if value is not None:
                     setattr(self, attr, value)
             return self
         else:
             raise DatabaseError('Invalid upload data',
                                 response.status_code, response.text)
     except NetworkError as e:
         print(e.args)
         raise DatabaseError(e.error, 500, e.description)