Skip to content

Instantly share code, notes, and snippets.

@gonzaloamadio
Created March 24, 2019 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gonzaloamadio/88b7e6de3b41c9e690c0789897a37984 to your computer and use it in GitHub Desktop.
Save gonzaloamadio/88b7e6de3b41c9e690c0789897a37984 to your computer and use it in GitHub Desktop.
class CreateJob(UseCaseInterface):
def __init__(
self,
repository: JobRepositoryInterface,
title,
date_start,
date_end,
slug=None
):
self._title = title
self._date_start = date_start
self._date_end = date_end
self._slug = None
# -- Other objects
self.__obj = None # To use with is valid
self._repository = repository
@property
def repository(self) -> JobRepositoryInterface:
return self._repository
def execute(self) -> Job:
self._factory()
self.is_valid()
self.__obj.slug = self._generate_slug(job.id, job.title)
self._repository.save(self.__obj) # This calls self.__obj.save()
return self.__obj
def is_valid(self):
# Check date order
if self._date_end and self._date_start and self._date_end <= self._date_start:
# raise ValidationError(_("Start date should be before end date"))
raise InvalidDateOrder(_("Start date should be before end date"))
# Executa programatically model validations. Raises validation error.
if not self.__obj:
self._factory()
self.__obj.full_clean()
return True
def _generate_slug(self, uuid, title):
return slug_generator(uuid, title)
def _factory(self):
"""Create an instance of a Job, and save it into self.__obj"""
# Remove _ from keys, so we pass correct arguments to create,
# and leave values that are not None
params = {k[1:] if k[0] == '_' else k:v for k,v in self.__dict__.items() if v is not None}
self.__obj = self._repository.factory(**params)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment