예제 #1
0
 def post(self):
     arguments_data = _ARGUMENTS.parse_args()
     error, code = self.__validate_value(arguments_data)
     if error is not None:
         return error, code
     credit = CreditModel(**arguments_data)
     try:
         credit.save()
     except Exception as e:
         traceback.print_exc()
         return errors._SAVE_ERROR, server_code.INTERNAL_SERVER_ERROR
     return credit.json(), server_code.OK
예제 #2
0
    def post(self):
        data = _register_parser.parse_args()

        if UserModel.find_by_email(data['email']):
            return {"message": "Email already registered"}, 400

        # Determine if address given is valid or not
        conn = http.client.HTTPConnection('api.positionstack.com')
        address = '{}, {}, Singapore {}'.format(data['addressLine1'],
                                                data['addressLine2'],
                                                data['addressPostalCode'])
        params = urllib.parse.urlencode({
            'access_key': 'bc0e165286c05e84509479ea679117eb',
            'query': address,
            'region': 'Singapore',
            'limit': 1,
        })
        conn.request('GET', '/v1/forward?{}'.format(params))
        res = conn.getresponse()
        returnData = res.read()
        response = json.loads(returnData.decode('utf-8'))
        if len(response['data']) == 0:
            return {"message": "Invalid Address"}, 400
        user = UserModel(lat=response['data'][0]['latitude'],
                         lng=response['data'][0]['longitude'],
                         **data)
        user.save_to_db()
        birthday = datetime.datetime.strptime(data['birthday'], '%Y-%m-%d')
        today = datetime.date.today()
        age = today.year - birthday.year - ((today.month, today.day) <
                                            (birthday.month, birthday.day))
        credits = calculateSignUpCredits(age, data['income'],
                                         data['householdCount'],
                                         data['householdType'])
        credit = CreditModel(userID=user.id, credits=credits)
        credit.save_to_db()
        return {"message": "User created successfully."}, 201