Пример #1
0
    def delete(self):
        """
        Delete a user
        ---
        tags:
            - User API
        parameters:
            - name: JWT Token
              in: header
              type: string
              required: true
              description: The JWT Token with format "Authorization Bearer <JWT Token>"
        responses:
            200:
                description: user sucessfully deleted
                schema:
                    id: user_delete_response
                    properties:
                        message:
                            type: string
                            description: User deleted!
            404:
                description: Error user not found.
        """
        user = UserModel.find_user_by_id(get_jwt_identity())
        if user:
            user.remove_from_db()
            return {"message": "User deleted!"}, 200

        return {"message": "User not found!"}, 404
Пример #2
0
    def post(self):
        """
        Change the user password
        ---
        tags:
            - User API
        parameters:
            - name: JWT Token
              in: header
              type: string
              required: true
              description: The JWT Token with format "Authorization Bearer <JWT Token>"
            - name: newpw
              in: body
              type: string
              required: true
              description: the new password for the user
        responses:
            200:
                description: Password successfully changed.
            500:
                description: Error user not found.
        """
        user = UserModel.find_user_by_id(get_jwt_identity())
        data = request.get_json()
        print(data)
        if not data['newpw']:
            return {"message": "No password provided"}, 500

        if user:
            user.password = data['newpw']
            user.save_to_db()
            return {"message": "Password successfully changed"}, 200
Пример #3
0
    def get(self):
        """
        Get user infos
        ---
        tags:
            - User API
        parameters:
            - name: JWT Token
              in: header
              type: string
              required: true
              description: The JWT Token with format "Authorization Bearer <JWT Token>"
        responses:
            200:
                description: Returns all profile infos about the user
                schema:
                    id: user_respone
                    properties:
                        id:
                            type: string
                            description: The id of the user
                        username:
                            type: string
                            description: The username of the user
                        email:
                            type: string
                            description: The email of the user
                        phonenumber:
                            type: string
                            description: The given phonenumber
            404:
                description: Error user not found.
        """
        user = UserModel.find_user_by_id(get_jwt_identity())
        if user:
            return user.json()

        return {"message": "User not found!"}, 404
Пример #4
0
    def post(self):
        """
        Create a new entry
        ---
        tags:
            - Entry API
        parameters:
            - name: JWT Token
              in: header
              type: string
              required: true
              description: The JWT Token with format "Authorization Bearer <JWT Token>"
            - name: title
              in: body
              type: string
              required: true
              description: the title of the entry
            - name: text
              in: body
              type: string
              required: true
              description: the text of the entry
            - name: tags
              in: body
              type: array
              required: true
              description: the tags for a entry
        responses:
            200:
                description: returns the new entry
                schema:
                    id: user_respone
                    properties:
                        id:
                            type: string
                            description: The id of an entry
                        title:
                            type: string
                            description: The title of the entry
                        text:
                            type: string
                            description: the text for an entry
                        author:
                            type: string
                            description: The username of the author
                        author_id:
                            type: string
                            description: The id of the author
                        date:
                            type: string
                            description: the creation date for an entry
                        tags:
                            type: array
                            description: the tags for an entry
        """
        conn = sqlite3.connect('data.db')
        c = conn.cursor()

        data = _entry_parser.parse_args()

        entry = EntryModel(data["title"], data["text"], UserModel.find_user_by_id(get_jwt_identity()).json()[0]['username'], get_jwt_identity())
        entry.save_to_db()
        tags_to_db = ""

        counter = 0
        for i in data['tags']:
            counter += 1
            tags_to_db += str(i) 
            if counter == len(data['tags']):
                pass
            else:
                tags_to_db += ";"
        c.execute("UPDATE entries SET tags = '" + tags_to_db +"' WHERE ID=" + str(entry.json()['id']))
        conn.commit()
        conn.close()
        ret_json = EntryModel.find_entry_by_id(entry.json()['id']).json()
        ret_json["tags"] = data['tags']
        return ret_json
Пример #5
0
    def put(self):
        """
        Change an entry
        ---
        tags:
            - Entry API
        parameters:
            - name: JWT Token
              in: header
              type: string
              required: true
              description: The JWT Token with format "Authorization Bearer <JWT Token>"
            - name: id
              in: body
              type: string
              required: true
              description: the id of the entry that you want to change
            - name: title
              in: body
              type: string
              required: true
              description: the title of the entry
            - name: text
              in: body
              type: string
              required: true
              description: the text of the entry
            - name: tags
              in: body
              type: array
              required: true
              description: the tags for a entry
        responses:
            200:
                description: Entry changed!
            404:
                description: Entry not found!
            403:
                description: You are not allowed to change the entry! Only the Author can change it
        """
        data = _entry_parser.parse_args()

        entry = EntryModel.find_entry_by_id(data["id"])

        if entry:
            if UserModel.find_user_by_id(get_jwt_identity()) is not UserModel.find_user_by_username(entry.json()["author"]):
                return {
                        "message": "You are not allowed to change the entry!"
                    }, 403
            else:
                tags_to_db = ""
                counter = 0
                for i in data['tags']:
                    counter += 1
                    tags_to_db += str(i) 
                    if counter == len(data['tags']):
                        pass
                    else:
                        tags_to_db += ";"
                data["tags"] = tags_to_db
                entry.title = data["title"]
                entry.text = data["text"]
                entry.date = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
                entry.tags = tags_to_db
                entry.save_to_db()
                
                return {
                        "message": "Entry changed!"
                    }, 200
        return {
                   "message": "Entry not found!"
               }, 404