from datetime import datetime
import random

from reobject.query import QuerySet, EmptyQuerySet
from reobject.model import Model

class ManagerDescriptor(object):
    """
    Descriptor class to deny access of manager methods via model instances.
    """
    def __init__(self):
        self.manager = None

    def __get__(self, instance, owner):
        if instance is not None:
            raise AttributeError(
                "Manager isn't accessible via %s instances" % owner.__name__
            )
        else:
            if not self.manager:
                self.manager = Manager(model=owner)

            return self.manager


class Manager(object):
    """
    Manager class holding the centralized object store, and providing proxies
    to various QuerySet methods.
    """
    def __init__(self, model):
        self._object_store = set()
        self.model = model

    def __contains__(self, item):
        return bool(self.get(name=item))

    def all(self) -> QuerySet:
        """
        Returns a QuerySet of all model instances.
        """
        return QuerySet(
            self._object_store,
            model=self.model
        )

    def count(self) -> int:
        """
        Returns an integer representing the total number of model instances.

        Proxy to the QuerySet.count() method.
        """
        return self.all().count()

    def earliest(self, field_name: str = 'created') -> Model:
        """
        Returns the earliest object, by date, using the field_name provided as
        the date field.

        Proxy to the QuerySet.earliest method.

        :param field_name: Name of attribute containing datetime object
        :return: Model instance if exists, None otherwise.
        """

        return self.all().earliest(field_name)

    def exclude(self, **kwargs):
        return self.all().exclude(**kwargs)

    def filter(self, **kwargs):
        return self.all().filter(**kwargs)

    def first(self) -> Model:
        """
        Returns the first model instance created.
        """
        return self.all().earliest()

    def get(self, **kwargs):
        return self.all().get(**kwargs)

    def get_or_create(self, defaults=None, **kwargs):
        return self.all().get_or_create(defaults, **kwargs)

    def last(self) -> Model:
        """
        Returns the last model instance created.
        """
        return self.all().latest()

    def latest(self, field_name: str = 'created') -> Model:
        """
        Returns the latest object, by date, using the field_name provided as
        the date field.

        Proxy to the QuerySet.latest method.

        :param field_name: Name of attribute containing datetime object
        :return: Model instance if exists, None otherwise.
        """
        return self.all().latest(field_name)

    def none(self) -> EmptyQuerySet:
        """
        Returns an EmptyQuerySet.

        Useful in reduce operations on QuerySets.
        """
        return EmptyQuerySet(model=self.model)

    def random(self) -> Model:
        """
        Returns a random model instance.
        """
        return self.all().random()

    def _clear(self):
        self._object_store.clear()

    def _delete(self, obj):
        self._object_store.remove(obj)

@@ -1,49 +1,4 @@
-from datetime import datetime%0Aimport random%0A%0A
 from
@@ -48,40 +48,8 @@
 Set%0A
-from reobject.model import Model
 %0A%0Acl
@@ -1337,37 +1337,38 @@
 = 'created') -%3E 
-Model
+object
 :%0A        %22%22%22%0A  
@@ -1878,37 +1878,38 @@
  first(self) -%3E 
-Model
+object
 :%0A        %22%22%22%0A  
@@ -2205,37 +2205,38 @@
 f last(self) -%3E 
-Model
+object
 :%0A        %22%22%22%0A  
@@ -2379,29 +2379,30 @@
 reated') -%3E 
-Model
+object
 :%0A        %22%22
@@ -2958,21 +2958,22 @@
 elf) -%3E 
-Model
+object
 :%0A      

Fix type annotations