-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexample_create_community.py
More file actions
69 lines (53 loc) · 2.13 KB
/
Copy pathexample_create_community.py
File metadata and controls
69 lines (53 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
"""
Example script demonstrating how to create a community programmatically.
This script shows how to use the create_community function to add
a new community with a hashed password to the database.
"""
import asyncio
import os
import sys
# Add the app directory to the Python path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from app.services.auth import hash_password # noqa: E402
from app.services.database.database import ( # noqa: E402
AsyncSessionLocal,
init_db,
)
from app.services.database.models.communities import Community # noqa: E402
async def create_community_example():
"""Example of creating a community with hashed password."""
# Initialize database
await init_db()
print("Database initialized successfully")
# Community data
username = "example_user"
email = "example@domain.com"
plain_password = "my_secure_password_123"
print(f"Creating community for: {username}")
print(f"Email: {email}")
print("Password will be hashed before storing")
# Hash the password using the hash_password function
hashed_password = hash_password(plain_password)
print(f"Password hashed: {hashed_password[:20]}... (truncated)")
# Create new community instance
new_community = Community(
username=username,
email=email,
password=hashed_password.decode("utf-8"), # Convert bytes to string
)
# Save to database using async session
async with AsyncSessionLocal() as session:
session.add(new_community)
await session.commit()
await session.refresh(new_community)
print("\n✅ Community created successfully!")
print(f" ID: {new_community.id}")
print(f" Username: {new_community.username}")
print(f" Email: {new_community.email}")
print(f" Created at: {new_community.created_at}")
print(f" Updated at: {new_community.updated_at}")
# Verify the password hash is stored correctly
print(f" Stored password hash: {new_community.password[:30]}...")
if __name__ == "__main__":
asyncio.run(create_community_example())