NOJIRA more restructuring
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -2,4 +2,4 @@ __pycache__/
|
|||||||
.idea/
|
.idea/
|
||||||
.venv
|
.venv
|
||||||
deployable.zip
|
deployable.zip
|
||||||
token.json
|
src/token.json
|
||||||
10
pack.sh
10
pack.sh
@@ -1,7 +1,5 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
zip deployable.zip \
|
cd "$(dirname "$0")"
|
||||||
templates/*.html \
|
cd src
|
||||||
models/*.py \
|
rm ../deployable.zip
|
||||||
main.py \
|
zip ../deployable.zip -r * -x '*__pycache__*'
|
||||||
token.json \
|
|
||||||
requirements.txt
|
|
||||||
@@ -1,2 +1,4 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
cd "$(dirname "$0")"
|
||||||
|
cd src
|
||||||
functions_framework --target process
|
functions_framework --target process
|
||||||
0
src/__init__.py
Normal file
0
src/__init__.py
Normal file
@@ -1,25 +1,29 @@
|
|||||||
import datetime
|
import datetime
|
||||||
from typing import List
|
from typing import Optional
|
||||||
|
|
||||||
from flask import render_template
|
from flask import render_template
|
||||||
from google.oauth2.service_account import Credentials
|
|
||||||
from googleapiclient.discovery import build
|
|
||||||
|
|
||||||
from models.dto import DTO
|
from models.dto import DTO
|
||||||
|
from repository import Repository
|
||||||
|
|
||||||
_SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
|
_repository: Optional[Repository] = None
|
||||||
|
|
||||||
_SHEET_ID = "18vCgc5DGUiFZN1NX_GBmxSBCb47KdsBkV6Glf9Sx-wE"
|
|
||||||
_RANGE = 'Friet bestelling!A2:F'
|
|
||||||
|
|
||||||
_cache_moment = datetime.datetime.now() - datetime.timedelta(days=1)
|
_cache_moment = datetime.datetime.now() - datetime.timedelta(days=1)
|
||||||
_cache_value = DTO()
|
_cache_value = DTO()
|
||||||
|
|
||||||
|
|
||||||
def process(_) -> str:
|
def process(_) -> str:
|
||||||
|
global _repository
|
||||||
global _cache_value
|
global _cache_value
|
||||||
global _cache_moment
|
global _cache_moment
|
||||||
|
|
||||||
|
if not _repository:
|
||||||
|
_repository = Repository(
|
||||||
|
sa_file_name='token.json',
|
||||||
|
spreadsheet_id='18vCgc5DGUiFZN1NX_GBmxSBCb47KdsBkV6Glf9Sx-wE',
|
||||||
|
spreadsheet_range='Friet bestelling!A2:F',
|
||||||
|
)
|
||||||
|
|
||||||
rerun_if_later_than = _cache_moment + datetime.timedelta(seconds=10)
|
rerun_if_later_than = _cache_moment + datetime.timedelta(seconds=10)
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
if now > rerun_if_later_than:
|
if now > rerun_if_later_than:
|
||||||
@@ -28,7 +32,7 @@ def process(_) -> str:
|
|||||||
return render_template('frietlijst.html', dto=_cache_value)
|
return render_template('frietlijst.html', dto=_cache_value)
|
||||||
|
|
||||||
|
|
||||||
def _anonymize_name(name: str) -> str:
|
def anonymize_name(name: str) -> str:
|
||||||
parts = name.split(" ")
|
parts = name.split(" ")
|
||||||
result = parts[0]
|
result = parts[0]
|
||||||
for part in parts[1:]:
|
for part in parts[1:]:
|
||||||
@@ -37,26 +41,17 @@ def _anonymize_name(name: str) -> str:
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def _load_values() -> List[List[str]]:
|
|
||||||
credentials: Credentials = Credentials.from_service_account_file('token.json', scopes=_SCOPES)
|
|
||||||
service = build('sheets', 'v4', credentials=credentials)
|
|
||||||
sheets = service.spreadsheets()
|
|
||||||
result = sheets.values().get(spreadsheetId=_SHEET_ID, range=_RANGE).execute()
|
|
||||||
values = result.get('values', [])
|
|
||||||
return values
|
|
||||||
|
|
||||||
|
|
||||||
def _get_data() -> DTO:
|
def _get_data() -> DTO:
|
||||||
cutoff = datetime.datetime.now() - datetime.timedelta(days=4)
|
cutoff = datetime.datetime.now() - datetime.timedelta(days=4)
|
||||||
|
|
||||||
rows_to_process = [row for row in _load_values()
|
rows_to_process = [row for row in _repository.load_rows()
|
||||||
if row and datetime.datetime.strptime(row[0], '%d-%m-%Y %H:%M:%S') > cutoff]
|
if row and datetime.datetime.strptime(row[0], '%d-%m-%Y %H:%M:%S') > cutoff]
|
||||||
|
|
||||||
result = DTO()
|
result = DTO()
|
||||||
|
|
||||||
for row in rows_to_process:
|
for row in rows_to_process:
|
||||||
result.add(
|
result.add(
|
||||||
applicant_name=_anonymize_name(row[1]),
|
applicant_name=anonymize_name(row[1]),
|
||||||
item_names=[item_name.lower().strip() for item_name in row[2:]],
|
item_names=[item_name.lower().strip() for item_name in row[2:]],
|
||||||
)
|
)
|
||||||
|
|
||||||
27
src/repository.py
Normal file
27
src/repository.py
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
from typing import List
|
||||||
|
|
||||||
|
from google.oauth2.service_account import Credentials
|
||||||
|
from googleapiclient.discovery import build
|
||||||
|
|
||||||
|
|
||||||
|
class Repository:
|
||||||
|
def __init__(self,
|
||||||
|
sa_file_name: str,
|
||||||
|
spreadsheet_id: str,
|
||||||
|
spreadsheet_range: str,
|
||||||
|
):
|
||||||
|
credentials = Credentials.from_service_account_file(
|
||||||
|
filename=sa_file_name,
|
||||||
|
scopes=['https://www.googleapis.com/auth/spreadsheets.readonly'],
|
||||||
|
)
|
||||||
|
service = build('sheets', 'v4', credentials=credentials)
|
||||||
|
self.__sheets = service.spreadsheets()
|
||||||
|
self.__spreadsheet_id = spreadsheet_id
|
||||||
|
self.__spreadsheet_range = spreadsheet_range
|
||||||
|
|
||||||
|
def load_rows(self) -> List[List[str]]:
|
||||||
|
result = self.__sheets.values().get(
|
||||||
|
spreadsheetId=self.__spreadsheet_id,
|
||||||
|
range=self.__spreadsheet_range,
|
||||||
|
).execute()
|
||||||
|
return result.get('values', [])
|
||||||
0
test/__init__.py
Normal file
0
test/__init__.py
Normal file
@@ -1,13 +1,13 @@
|
|||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from main import _anonymize_name
|
from main import anonymize_name
|
||||||
|
|
||||||
|
|
||||||
class TestMain(unittest.TestCase):
|
class TestMain(unittest.TestCase):
|
||||||
def test_anonymize_name(self):
|
def test_anonymize_name(self):
|
||||||
given = "Alice Bobbington Charlie"
|
given = "Alice Bobbington Charlie"
|
||||||
expected = "Alice B C"
|
expected = "Alice B C"
|
||||||
actual = _anonymize_name(given)
|
actual = anonymize_name(given)
|
||||||
|
|
||||||
self.assertEqual(expected, actual)
|
self.assertEqual(expected, actual)
|
||||||
|
|
||||||
Reference in New Issue
Block a user