Last active
January 12, 2020 10:03
-
-
Save Ingco/c29eee29988c8e9ca5f7f03d595939d1 to your computer and use it in GitHub Desktop.
interface inheritance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3.7 | |
from abc import ABC, abstractmethod | |
from decimal import Decimal | |
from typing import NamedTuple, List, Tuple | |
# STUBS | |
def _(val): | |
return val | |
class MyOrderDict: | |
def get(self, *args, **kwargs): | |
return 1 | |
class HttpRequest: | |
POST = MyOrderDict() | |
class Manager: | |
def get(self, *args, **kwargs): | |
return '' | |
def all(self, *args, **kwargs): | |
return '' | |
class Model: | |
objects = Manager() | |
class DoesNotExist(Exception): | |
pass | |
class ProductionOrderJob(Model): | |
pass | |
class Queryset: | |
pass | |
class timezone: | |
pass | |
def get_user_model(): | |
return Model | |
# End STUBS | |
class ObjectNamedTuple(NamedTuple): | |
title: str = '' | |
value: Decimal = Decimal('0.00') | |
class FinishedJobsNamedTuple(NamedTuple): | |
title: str = '' | |
count: int = 0 | |
cost: Decimal = Decimal('0.00') | |
class BaseReportInterface(ABC): | |
"""Base Interface for Reports""" | |
def __init__(self, request_: HttpRequest, *args, **kwargs): | |
self._request = request_ | |
@property | |
@abstractmethod | |
def years(self) -> tuple: | |
"""Get tuple with years""" | |
raise NotImplemented | |
@property | |
def months(self) -> tuple: | |
"""Get tuple with months""" | |
return ( | |
_('January'), _('February'), _('March'), _('April'), _('May'), | |
_('June'), _('July'), _('August'), _('September'), | |
_('October'), _('November'), _('December') | |
) | |
@property | |
def current_year(self) -> str: | |
"""Get current year""" | |
# return self._request.POST.get('finish_at__year', str(timezone.now().year)) | |
return '2020' | |
@property | |
def current_month(self) -> str: | |
"""Get current month number""" | |
# return self._request.POST.get('finish_at__month', str(timezone.now().month)) | |
return '01' | |
@property | |
def current_month_name(self) -> str: | |
"""Get current month name""" | |
# month = self._request.POST.get( | |
# 'finish_at__month', str(timezone.now().month)) | |
# return self.months[month - 1] | |
return 'January' | |
class FinishedProductionJobsAggregator: | |
"""Creator finished Jobs list for the reports""" | |
def __init__(self, queryset: Queryset): | |
self._queryset = queryset | |
def get_finished_jobs_data(self) -> Tuple[FinishedJobsNamedTuple]: | |
"""Get a list with FinishedJobs of NamedTuple""" | |
return ( | |
FinishedJobsNamedTuple(title='Test', count=1, cost=Decimal('2.00')), | |
) | |
class TechnicianReportInterface(BaseReportInterface): | |
"""Technician Report Interface. Inherited BaseReportInterface""" | |
@property | |
@abstractmethod | |
def technician_name(self) -> str: | |
"""Get technician name""" | |
raise NotImplemented | |
@property | |
@abstractmethod | |
def base(self) -> ObjectNamedTuple: | |
"""Get Base sum""" | |
raise NotImplemented | |
@property | |
@abstractmethod | |
def finished_jobs(self) -> list: | |
"""Get finished jobs list""" | |
raise NotImplemented | |
class MainReportInterface(BaseReportInterface): | |
@property | |
@abstractmethod | |
def technician_reports(self) -> List['TechnicianReport']: | |
"""Get list 'TechnicianReport's""" | |
raise NotImplemented | |
@property | |
@abstractmethod | |
def created_orders(self) -> ObjectNamedTuple: | |
"""Get ObjectNamedTuple with count created orders and title""" | |
raise NotImplemented | |
class TechnicianReport(TechnicianReportInterface): | |
"""Technician report""" | |
_make_finished_jobs = False | |
def __init__(self, request_: HttpRequest, make_jobs=False, *args, **kwargs): | |
super(TechnicianReport, self).__init__( | |
request_, args, **kwargs) | |
# Flag: call to FinishedProductionJobsAggregator or not | |
self._make_finished_jobs = make_jobs | |
# Queryset wit all data | |
self._queryset = self._get_queryset(request_) | |
# We are looking for technician | |
self.technician_model = get_user_model() | |
technician_id = request_.POST.get('technician', 0) | |
try: | |
self._technician = self.technician_model.objects.get(id=technician_id) | |
except self.technician_model.DoesNotExist: | |
self._technician = None | |
def _get_queryset(self, request_): | |
"""Get Queryset with data""" | |
return ProductionOrderJob.objects.all() | |
@property | |
def years(self) -> tuple: | |
return (2019, 2019), (2020, 2020) | |
@property | |
def months(self) -> tuple: | |
return ('1', 'Jan'), ('2', 'Feb') | |
@property | |
def technician_name(self) -> str: | |
return 'Test Test' | |
@property | |
def base(self) -> ObjectNamedTuple: | |
return ObjectNamedTuple( | |
title='Base, €', | |
value=Decimal('1.00') | |
) | |
@property | |
def finished_jobs(self) -> tuple: | |
"""Get list with ProductionOrderJobs""" | |
if not self._make_finished_jobs: | |
return tuple() | |
aggregator = FinishedProductionJobsAggregator(self._queryset) | |
return aggregator.get_finished_jobs_data() | |
class MainReport(MainReportInterface): | |
@property | |
def years(self) -> tuple: | |
return (2019, 2019), (2020, 2020) | |
@property | |
def technician_reports(self) -> List[TechnicianReport]: | |
return [] | |
@property | |
def created_orders(self) -> ObjectNamedTuple: | |
return ObjectNamedTuple(title='Main Test', value=Decimal('3.00')) | |
if __name__ == '__main__': | |
request = HttpRequest() | |
model = Model() | |
print(' TechnicianReport: ') | |
report = TechnicianReport(request, make_jobs=True) | |
print(report.current_month) | |
print(report.technician_name) | |
print(report.finished_jobs) | |
print(' --- ') | |
print(' MainReport: ') | |
main_report = MainReport(request) | |
print(main_report.technician_reports) | |
print(main_report.created_orders) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment