GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — DjangoEssentials/djangoessentials/service.py at main · CoderMungan/DjangoEssentials Expand file tree/ Copy pathservice.pyMore file actions 66 lines (54 loc) · 2.35 KB / Copy pathservice.pyFile metadata and controls66 lines (54 loc) · 2.35 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 from abc import ABC from typing import Generic, TypeVar, Optional from django.db.models import QuerySet R = TypeVar("R") class BaseService(ABC, Generic[R]): def __init__(self, repository: R): self._repository = repository def get_all(self) -> QuerySet: """Fetch all objects.""" return self._repository.get_all() def get_by_id(self, obj_id: int) -> Optional[R]: """Fetch a single object by ID.""" return self._repository.get_by_id(obj_id) def create(self, data: Optional[dict] = None, **kwargs) -> R: """ Create a new object. Supports both a dictionary (`data`) or keyword arguments (`**kwargs`). """ return self._repository.create(data, **kwargs) def update(self, obj_id: int, data: Optional[dict] = None, **kwargs) -> Optional[R]: """ Update an object with the given data. Supports both a dictionary (`data`) or keyword arguments (`**kwargs`). """ return self._repository.update(obj_id, data, **kwargs) def delete(self, obj_id: int) -> bool: """Delete an object by ID.""" return self._repository.delete(obj_id) def filter(self, data: Optional[dict] = None, **filters) -> QuerySet: """ Filter objects based on given criteria. Supports both a dictionary (`data`) or keyword arguments (`**filters`). """ return self._repository.filter(data, **filters) def exists(self, data: Optional[dict] = None, **kwargs) -> bool: """ Check if an object exists with the given criteria. Supports both a dictionary (`data`) or keyword arguments (`**kwargs`). """ return self._repository.exists(data, **kwargs) def count(self, data: Optional[dict] = None, **kwargs) -> int: """ Count objects with the given criteria. Supports both a dictionary (`data`) or keyword arguments (`**kwargs`). """ return self._repository.count(data, **kwargs) def get_or_create( self, defaults: Optional[dict] = None, data: Optional[dict] = None, **kwargs ) -> tuple[R, bool]: """ Get or create an object with the given criteria. Supports both a dictionary (`data`) or keyword arguments (`**kwargs`). """ return self._repository.get_or_create(defaults=defaults, data=data, **kwargs) |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |