RealWorld: aiohttp, Tortoise ORM

There is no example for aiohttp on Real World , and I decided to make one. Experienced developers, it seems, have no time to do this, and beginners in aiohttp do not understand how to do it right. I started doing it with Tortoise ORM. While starting to do authentication.


I want to make this project right, so there are a lot of questions for experienced aiohttp developers under the cut.


Project Installation


Repository


Clone the repository:


git clone git@github.com: nomhoi / aiohttp-realworld-example-app.git

In development, I used Python 3.8. Install the dependencies:


pip install -r requirements.txt

Initialize the database:


python init_db.py

We start the server:


python -m conduit

In the repository there is a Postman collection RealWorld.postman_collection.json . Import it into Postman and you can try the resulting API.


PostgreSQL


By default, the SQLite database is used.


To use PostrgeSQL, you need to change the DB_URL variable in /conduit/settings.py.


DB_URL = "postgres: // postgres: postgres@0.0.0.0: 5432 / postgres"

Start the database service:


docker-compose up -d

After that, initialize the database:


python init_db.py

Project file structure


Django: . .


, , RealWorld Django.


— conduit.


API


API


POST /api/users


POST /api/users/login


GET /api/user


/authentication/views.py.


CORS


aiohttp_cors.


# Configure default CORS settings.
cors = aiohttp_cors.setup(app, defaults={
    "*": aiohttp_cors.ResourceOptions(
        allow_credentials=True,
        expose_headers="*",
        allow_headers="*",
    )
})

# Configure CORS on all routes.
for route in list(app.router.routes()):
    cors.add(route)

, , .


, .


JWT


aiohttp_jwt:


    app = web.Application(
        middlewares=[
            JWTMiddleware(
                settings.SECRET_KEY,
                whitelist=[
                    r'/api/users',
                ]
            )
        ]
    )

JWT , Authorization Token Bearer. Django — Token. .


User


Tortoise ORM


class TimestampedMixin:
    created_at = fields.DatetimeField(auto_now_add=True)
    updated_at = fields.DatetimeField(auto_now=True)

class AbstractBaseModel(Model):
    id = fields.IntField(pk=True)

    class Meta:
        abstract = True

class User(TimestampedMixin, AbstractBaseModel):
    username = fields.CharField(db_index=True, max_length=255, unique=True)
    email = fields.CharField(db_index=True, max_length=255, unique=True)
    password = fields.CharField(max_length=128)

    class Meta:
        table = "user"

    def __str__(self):
        return self.username

, Django :


ordering = ['-created_at', '-updated_at']

, User. .


Django: + PBKDF2. .



Tortoise ORM . , .



So far, done without serializers. Found such a project , but there is an old version of Tortoise ORM.


There are other implementations of serializers. Probably, with them the development is idiomatic and faster. Need to try. ORM hooked, now we need to add serializers. Or maybe add more renders?


Tests


Not yet added. According to the conditions of the task, tests can be done, but they are not required.


Conclusion


You also need to add support for profiles, articles and much more. Everyone is invited to the project to glorify their name for centuries and millennia. Hints and comments from experienced developers are welcome.

Source: https://habr.com/ru/post/undefined/


All Articles