Пример #1
0
    def post(self, received_id):
        res = {}
        try:
            send_id = get_jwt_identity()
            body = request.get_json()
            received = resCon.get_user_name(
                json.loads(User.objects.get(id=received_id).to_json()))
            sender = resCon.get_user_name(
                json.loads(User.objects.get(id=send_id).to_json()))

            users = [received, sender]

            message = Message()
            message.from_user = send_id
            message.to_user = received_id
            message.text = body["message"]

            messages = [message]

            Conversation.objects.get(users=users)
            res = response.user_existed()
        except DoesNotExist:
            conversation = Conversation(users=users, messages=messages).save()
            session["chat"] = str(conversation.id)
            res = response.sucess()
        except Exception:
            raise Exception
            res = response.internal_server()
        return jsonify(res)
Пример #2
0
 def post(self):
     try:
         user_id = get_jwt_identity()
         self.res = response.sucess()
     except Exception:
         self.res = response.internal_server()
     return jsonify(self.res)
Пример #3
0
 def get(self):
     user = User.objects()
     user_id = []
     for i in user:
         user_id.append(i["id"])
     print(user_id)
     return jsonify(response.sucess())
Пример #4
0
 def delete(self, received_id):
     res = {}
     try:
         current_user_id = get_jwt_identity()
         users = get_list_embedded(current_user_id, received_id)
         conversation = Conversation.objects.get_users(
             users).first().delete()
         res = response.sucess()
         # return Response(conversation, mimetype="application/json")
     except Exception:
         res = response.internal_server()
     return jsonify(res)
 def get(self):
     try:
         body = request.get_json()
         user = User.objects.get(phonenumber=body.get('phonenumber'))
         if user.verify:
             raise response.HaveDoneVerify
         self.res = response.sucess()
     except response.HaveDoneVerify:
         self.res = response.action_done_previously()
     except DoesNotExist:
         self.res = response.user_is_not_validated()
     except Exception:
         self.res = response.internal_server()
     return jsonify(self.res)
Пример #6
0
 def delete(self, received_id):
     res = {}
     try:
         from_user = get_jwt_identity()
         users = get_list_embedded(from_user, received_id)
         conversation = Conversation.objects.get_users(users).first()
         if conversation == None:
             raise Exception
         body = request.get_json()
         index = body["index"]
         message = conversation.get_message_by_index(index)
         if message != None:
             conversation.update(pull__messages=message)
             res = response.sucess()
     except Exception:
         res = response.internal_server()
     return jsonify(res)
 def put(self):
     res = {}
     try:
         current_user_id = get_jwt_identity()
         user = User.objects.get(id=current_user_id)
         body = request.get_json()
         if "firstname" in body:
             body["username"] = body["firstname"] + " " + user.lastname
         if "lastname" in body:
             body["username"] = user.firstname + " " + body["lastname"]
         user.update(**body)
         res = response.sucess()
     except DoesNotExist:
         res = response.user_is_invalid()
     except Exception:
         raise Exception
         res = response.internal_server()
     return jsonify(res)
Пример #8
0
    def post(self):
        try:
            body = request.get_json()
            user = User.objects.get(phonenumber=body.get('phonenumber'))
            authorized = user.check_password(body.get('password'))
            if not authorized:
                raise UnauthorizedError
            expires = datetime.timedelta(days=365)
            access_token = create_access_token(identity=str(user.id),
                                               expires_delta=expires)

            data = remove_password_convert_dict(user)
            self.res = response.sucess()
            self.res = response_value(self.res, data)
            self.res["data"]["token"] = access_token
        except (UnauthorizedError, DoesNotExist):
            self.res = response.user_is_not_validated()
        except Exception:
            self.res = response.internal_server()
        return jsonify(self.res)
Пример #9
0
 def post(self):
     try:
         body = request.get_json()
         user = User(**body)
         user.hash_password()
         user.default()
         data = remove_password_convert_dict(user)
         self.res = response.sucess()
         self.res = response_value(self.res, data)
         user.save()
         Friend(owner=user["id"]).save()
         Notification(owner=user["id"]).save()
     except FieldDoesNotExist:
         self.res = response.parameter_not_enough()
     except NotUniqueError:
         self.res = response.user_existed()
     except Exception:
         raise Exception
         self.res = response.internal_server()
     return jsonify(self.res)
Пример #10
0
 def post(self):
     try:
         body = request.get_json()
         user = User.objects.get(phonenumber=body.get('phonenumber'))
         if user.verify:
             raise response.HaveDoneVerify
         if body.get('code_verify') != CODE_VERIFY:
             raise response.ParameterValueInvalid
         user.verify = True
         user.save()
         self.res = response.sucess()
     except response.HaveDoneVerify:
         self.res = response.user_existed()
     except response.ParameterValueInvalid:
         self.res = response.parameter_value_invalid()
     except DoesNotExist:
         self.res = response.user_is_not_validated()
     except Exception:
         self.res = response.internal_server()
     return jsonify(self.res)
Пример #11
0
 def post(self):
     res = {}
     try:
         user_id = get_jwt_identity()
         body = request.get_json()
         user = User.objects.get(id=user_id)
         authorized = user.check_password(body.get('password'))
         if not authorized:
             raise UnauthorizedError
         user.password = body.get("new_password")
         if not user.compare_password(body.get('password'),
                                      body.get("new_password")):
             raise response.PasswordInvalid
         user.hash_password()
         user.save()
         res = response.sucess()
     except UnauthorizedError:
         res = response.wrong_password()
     except response.PasswordInvalid:
         res = response.password_invalid()
     except Exception:
         raise Exception
         res = response.internal_server()
     return jsonify(res)
 def post(self):
     try:
         body = request.files.get("file")
         user_id = get_jwt_identity()
         user = User.objects.get(id=user_id)
         if body == "":
             return {
                 'data': '',
                 'message': 'No file found',
                 'status': 'error'
             }
         else:
             filename = str(uuid.uuid4().hex) + '.jpeg'
             user.avatar.replace(
                 body, content_type='image/jpeg', filename=filename)
             user.save()
             self.res = response.sucess()
             self.res["avatar"] = filename
     except DoesNotExist:
         self.res = response.user_is_invalid()
     except Exception:
         raise Exception
         self.res = response.upload_file_failed()
     return jsonify(self.res)
Пример #13
0
def like_convert(post):
    res = {}
    res = response.sucess()
    res["like"] = post.like
    return res