Not working version, issues with get

This commit is contained in:
retoor 2025-08-08 17:28:35 +02:00
parent 89d639e44e
commit 5e99e894e9
8 changed files with 1243 additions and 29 deletions

View File

@ -40,7 +40,8 @@ dependencies = [
"pillow-heif",
"IP2Location",
"bleach",
"sentry-sdk"
"sentry-sdk",
"aiosqlite"
]
[tool.setuptools.packages.find]

View File

@ -9,7 +9,7 @@ from contextlib import asynccontextmanager
from snek import snode
from snek.view.threads import ThreadsView
from snek.system.ads import AsyncDataSet
logging.basicConfig(level=logging.DEBUG)
from concurrent.futures import ThreadPoolExecutor
from ipaddress import ip_address
@ -142,6 +142,7 @@ class Application(BaseApplication):
client_max_size=1024 * 1024 * 1024 * 5 * args,
**kwargs,
)
self.db = AsyncDataSet(kwargs["db_path"].replace("sqlite:///", ""))
session_setup(self, EncryptedCookieStorage(SESSION_KEY))
self.tasks = asyncio.Queue()
self._middlewares.append(session_middleware)
@ -174,7 +175,7 @@ class Application(BaseApplication):
self.on_startup.append(self.prepare_asyncio)
self.on_startup.append(self.start_user_availability_service)
self.on_startup.append(self.start_ssh_server)
self.on_startup.append(self.prepare_database)
#self.on_startup.append(self.prepare_database)
async def prepare_stats(self, app):
app['stats'] = create_stats_structure()
@ -245,18 +246,8 @@ class Application(BaseApplication):
async def prepare_database(self, app):
self.db.query("PRAGMA journal_mode=WAL")
self.db.query("PRAGMA syncnorm=off")
try:
if not self.db["user"].has_index("username"):
self.db["user"].create_index("username", unique=True)
if not self.db["channel_member"].has_index(["channel_uid", "user_uid"]):
self.db["channel_member"].create_index(["channel_uid", "user_uid"])
if not self.db["channel_message"].has_index(["channel_uid", "user_uid"]):
self.db["channel_message"].create_index(["channel_uid", "user_uid"])
except:
pass
await self.db.query_raw("PRAGMA journal_mode=WAL")
await self.db.query_raw("PRAGMA syncnorm=off")
await self.services.drive.prepare_all()
self.loop.create_task(self.task_runner())

View File

@ -101,6 +101,8 @@ class UserService(BaseService):
model.username.value = username
model.password.value = await security.hash(password)
if await self.save(model):
for x in range(10):
print("Jazeker!!!")
if model:
channel = await self.services.channel.ensure_public_channel(
model["uid"]

View File

@ -7,7 +7,8 @@ class UserPropertyService(BaseService):
mapper_name = "user_property"
async def set(self, user_uid, name, value):
self.mapper.db["user_property"].upsert(
self.mapper.db.upsert(
"user_property",
{
"user_uid": user_uid,
"name": name,

1207
src/snek/system/ads.py Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
DEFAULT_LIMIT = 30
import asyncio
import typing
import traceback
from snek.system.model import BaseModel
@ -51,7 +51,9 @@ class BaseMapper:
kwargs["uid"] = uid
if not kwargs.get("deleted_at"):
kwargs["deleted_at"] = None
record = await self.run_in_executor(self.table.find_one, **kwargs)
#traceback.print_exc()
record = await self.db.get(self.table_name, kwargs)
if not record:
return None
record = dict(record)
@ -61,23 +63,29 @@ class BaseMapper:
return model
async def exists(self, **kwargs):
return await self.run_in_executor(self.table.exists, **kwargs)
return await self.db.count(self.table_name, kwargs)
#return await self.run_in_executor(self.table.exists, **kwargs)
async def count(self, **kwargs) -> int:
return await self.run_in_executor(self.table.count, **kwargs)
return await self.db.count(self.table_name,kwargs)
async def save(self, model: BaseModel) -> bool:
if not model.record.get("uid"):
raise Exception(f"Attempt to save without uid: {model.record}.")
model.updated_at.update()
return await self.run_in_executor(self.table.upsert, model.record, ["uid"],use_semaphore=True)
model.updated_at.update()
await self.upsert(model)
return model
#return await self.run_in_executor(self.table.upsert, model.record, ["uid"],use_semaphore=True)
async def find(self, **kwargs) -> typing.AsyncGenerator:
if not kwargs.get("_limit"):
kwargs["_limit"] = self.default_limit
if not kwargs.get("deleted_at"):
kwargs["deleted_at"] = None
for record in await self.run_in_executor(self.table.find, **kwargs):
for record in await self.db.find(self.table_name, kwargs):
model = await self.new()
for key, value in record.items():
model[key] = value
@ -88,21 +96,21 @@ class BaseMapper:
return "insert" in sql or "update" in sql or "delete" in sql
async def query(self, sql, *args):
for record in await self.run_in_executor(self.db.query, sql, *args, use_semaphore=await self._use_semaphore(sql)):
for record in await self.db.query(sql, *args):
yield dict(record)
async def update(self, model):
if not model["deleted_at"] is None:
raise Exception("Can't update deleted record.")
model.updated_at.update()
return await self.run_in_executor(self.table.update, model.record, ["uid"],use_semaphore=True)
return await self.db.update(self.table_name, model.record, {"uid": model["uid"]})
async def upsert(self, model):
model.updated_at.update()
return await self.run_in_executor(self.table.upsert, model.record, ["uid"],use_semaphore=True)
await self.db.upsert(self.table_name, model.record, {"uid": model["uid"]})
return model
async def delete(self, **kwargs) -> int:
if not kwargs or not isinstance(kwargs, dict):
raise Exception("Can't execute delete with no filter.")
kwargs["use_semaphore"] = True
return await self.run_in_executor(self.table.delete, **kwargs)
return await self.db.delete(self.table_name, kwargs)

View File

@ -50,7 +50,7 @@ class BaseService:
if result and result.__class__ == self.mapper.model_class:
return result
kwargs["uid"] = uid
print(kwargs,"ZZZZZZZ")
result = await self.mapper.get(**kwargs)
if result:
await self.cache.set(result["uid"], result)

View File

@ -38,6 +38,10 @@ class WebView(BaseView):
channel = await self.services.channel.get(
uid=self.request.match_info.get("channel")
)
print(self.session.get("uid"),"ZZZZZZZZZZ")
qq = await self.services.user.get(uid=self.session.get("uid"))
print("GGGGGGGGGG",qq)
if not channel:
user = await self.services.user.get(
uid=self.request.match_info.get("channel")