From 0e19980e39f3d97601ca94d57fb78b864834b746 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Mon, 10 Sep 2018 14:52:15 +0900 Subject: [PATCH 01/47] Docs: Add: (refs #183) spec for '/results' endpoint --- api/spec/routes/api/results.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 api/spec/routes/api/results.yml diff --git a/api/spec/routes/api/results.yml b/api/spec/routes/api/results.yml new file mode 100644 index 0000000..740a1ec --- /dev/null +++ b/api/spec/routes/api/results.yml @@ -0,0 +1,14 @@ +return PDF file that contains the results of previous lotteries +--- +produces: + - application/pdf +responses: + '200': + description: PDF file which contains whole winners for previous winners + schema: + type: file + '400': + description: not acceptable time + schema: + $ref: '#/definitions/ErrorMessage' +summary: return PDF file that contains the results of previous lotteries From 3fb5c1a902ea11284f83c0ac1d6d423609bff4a2 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Mon, 10 Sep 2018 16:14:01 +0900 Subject: [PATCH 02/47] Add: (refs #183) time_management> 'get_prev_time_index' to get previous time index This method would work like this: |-- TIME_POINTS[0] | | |-- TIME_POINTS[1] | | | ||-- TIMEPOINT_END_MARGIN | || | || |-- DRAWWING_TIME EXTENSION | || | | || | |-- Void time (annoucements, the show is going...) | || | | v index: x vv v v index: y index: z |=============|-|******| |=============|-|******| |=============|-| |++++++++++++++++++++++++++++++++| |________________________________| In the range of |+|, `get_prev_time_index` will return `x`. In the range of |_|, `get_prev_time_index` will return `y`. --- api/time_management.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/api/time_management.py b/api/time_management.py index 6cb889e..7e1cd48 100644 --- a/api/time_management.py +++ b/api/time_management.py @@ -96,3 +96,25 @@ def get_time_index(time=None): return i raise OutOfAcceptingHoursError() + + +def get_prev_time_index(time=None): + """ + get the previous lottery index from the time + args: + time(datetime.time|datetime.datetime): the time + return: + i(int): the lottery index + raises: + OutOfHoursError, OutOfAcceptingHoursError + """ + time = _validate_and_get_time(time) + en_margin = current_app.config['TIMEPOINT_END_MARGIN'] + ends = [mod_time(time_point[1], en_margin) for time_point + in current_app.config['TIMEPOINTS']] + + for i in range(len(ends)): + if ends[i] <= time <= ends[i+1]: + return i + + raise OutOfAcceptingHoursError() From 1da0dd22cd3e4d190cc58537cbe1287514a29366 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Mon, 10 Sep 2018 17:06:11 +0900 Subject: [PATCH 03/47] Docs: Add: (refs #183) add description to graphical info in previous commit --- api/time_management.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/api/time_management.py b/api/time_management.py index 7e1cd48..03255af 100644 --- a/api/time_management.py +++ b/api/time_management.py @@ -107,6 +107,11 @@ def get_prev_time_index(time=None): i(int): the lottery index raises: OutOfHoursError, OutOfAcceptingHoursError + + the graphical description for this method is + in the commit message of commit 3fb5c1a . + See it if you confusing how to imagine this work. + And also, it lives in notes for that commit. """ time = _validate_and_get_time(time) en_margin = current_app.config['TIMEPOINT_END_MARGIN'] From b0039dd7e5bdcfff522a01ba2c183e3de601ce5e Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Mon, 10 Sep 2018 18:30:44 +0900 Subject: [PATCH 04/47] Docs: Fix: (refs #183) fix comment not to refer to commit message The diagram on the commit message was wrong, so DON'T PREFER TO IT. Instead, see notes. you can show notes by using: ``` git notes show 3fb5c1a ``` --- api/time_management.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/api/time_management.py b/api/time_management.py index 03255af..df997c0 100644 --- a/api/time_management.py +++ b/api/time_management.py @@ -109,9 +109,10 @@ def get_prev_time_index(time=None): OutOfHoursError, OutOfAcceptingHoursError the graphical description for this method is - in the commit message of commit 3fb5c1a . + in the commit note of commit 3fb5c1a . See it if you confusing how to imagine this work. - And also, it lives in notes for that commit. + And also, it lived in the commit message, + but it was wrong. so DON'T REFER TO COMMIT MESSAGE """ time = _validate_and_get_time(time) en_margin = current_app.config['TIMEPOINT_END_MARGIN'] From 48fdb61a16a31026bda233b44838082b388b7e77 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Mon, 10 Sep 2018 18:34:45 +0900 Subject: [PATCH 05/47] Fix: (refs #183) time_management> adjust loop index --- api/time_management.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/time_management.py b/api/time_management.py index df997c0..cbe4d4c 100644 --- a/api/time_management.py +++ b/api/time_management.py @@ -119,7 +119,7 @@ def get_prev_time_index(time=None): ends = [mod_time(time_point[1], en_margin) for time_point in current_app.config['TIMEPOINTS']] - for i in range(len(ends)): + for i in range(len(ends)-1): if ends[i] <= time <= ends[i+1]: return i From 8fc110806c6b7a7045e0edc13e58b73970ad1cff Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Mon, 10 Sep 2018 18:35:42 +0900 Subject: [PATCH 06/47] Add: (refs #183) pytest> 'test_perv_time_index' for 'get_prev_time_index' Test whether it fail when the time is out of acceptable range --- test/test_time.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/test_time.py b/test/test_time.py index 2fadb85..bd0a70a 100644 --- a/test/test_time.py +++ b/test/test_time.py @@ -5,6 +5,7 @@ mod_time, get_draw_time_index, get_time_index, + get_prev_time_index, OutOfHoursError, OutOfAcceptingHoursError ) @@ -101,3 +102,15 @@ def test_time_index_same(client): assert i == idx_l idx_r = get_time_index(point[1]) assert i == idx_r + + +def test_prev_time_index_ooa(client): + with client.application.app_context(): + start_of_first_index = client.application.config['TIMEPOINTS'][0][0] + end_of_last_index = client.application.config['TIMEPOINTS'][-1][1] + en_margin = client.application.config['TIMEPOINT_END_MARGIN'] + res = mod_time(datetime.timedelta.resolution, en_margin) + with pytest.raises(OutOfAcceptingHoursError): + get_prev_time_index(mod_time(start_of_first_index, res)) + with pytest.raises(OutOfAcceptingHoursError): + get_prev_time_index(mod_time(end_of_last_index, res)) From 4590aa26de7928abcc44588254c4573498b10530 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Mon, 10 Sep 2018 19:20:29 +0900 Subject: [PATCH 07/47] Add: (refs #183) pytest> 'test_prev_time_index_lim' for 'get_prev_time_index' --- test/test_time.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/test_time.py b/test/test_time.py index bd0a70a..3bec50d 100644 --- a/test/test_time.py +++ b/test/test_time.py @@ -114,3 +114,16 @@ def test_prev_time_index_ooa(client): get_prev_time_index(mod_time(start_of_first_index, res)) with pytest.raises(OutOfAcceptingHoursError): get_prev_time_index(mod_time(end_of_last_index, res)) + + +def test_prev_time_index_lim(client): + with client.application.app_context(): + res = datetime.timedelta.resolution + en_margin = client.application.config['TIMEPOINT_END_MARGIN'] + ends = [mod_time(tp[1], en_margin) for tp + in client.application.config['TIMEPOINTS']] + for i in range(len(ends)-1): + idx_l = get_prev_time_index(mod_time(ends[i], res)) + assert i == idx_l + idx_r = get_prev_time_index(mod_time(ends[i+1], -res)) + assert i == idx_r From cf35bec9d4fad1d7677e5bd50347a01ee7c91107 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Mon, 10 Sep 2018 19:23:40 +0900 Subject: [PATCH 08/47] Fix: (refs #183) time_management> avoid '<=' to remove conflict at the edge of range If I do `ends[i] <= time <= ends[i+1]`, This expression will be True twice when `time` is the same as `ends[i+1]` because that would be the same as `ends[i] for the next expression. Of cource, this conflict won't work because of looping, But it is not good idea to leave things confusing, right? So I fixed it --- api/time_management.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/time_management.py b/api/time_management.py index cbe4d4c..2d053b2 100644 --- a/api/time_management.py +++ b/api/time_management.py @@ -120,7 +120,7 @@ def get_prev_time_index(time=None): in current_app.config['TIMEPOINTS']] for i in range(len(ends)-1): - if ends[i] <= time <= ends[i+1]: + if ends[i] <= time < ends[i+1]: return i raise OutOfAcceptingHoursError() From 15bdfa6927b0749b099449020087be21c01dc8f7 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Mon, 10 Sep 2018 20:45:32 +0900 Subject: [PATCH 09/47] Add: (refs #183) pytest> 'test_prev_time_index_same' for 'get_prev_time_index' --- test/test_time.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/test_time.py b/test/test_time.py index 3bec50d..d76eabc 100644 --- a/test/test_time.py +++ b/test/test_time.py @@ -127,3 +127,22 @@ def test_prev_time_index_lim(client): assert i == idx_l idx_r = get_prev_time_index(mod_time(ends[i+1], -res)) assert i == idx_r + + +def test_prev_time_index_same(client): + with client.application.app_context(): + res = datetime.timedelta.resolution + en_margin = client.application.config['TIMEPOINT_END_MARGIN'] + ends = [mod_time(tp[1], en_margin) for tp + in client.application.config['TIMEPOINTS']] + for i in range(len(ends)-1): + if i == len(ends)-1: + idx_l = get_prev_time_index(ends[i]) + assert i == idx_l + with pytest.raises(OutOfAcceptingHoursError): + get_prev_time_index(ends[i+1]) + else: + idx_l = get_prev_time_index(ends[i]) + assert i == idx_l + idx_r = get_prev_time_index(mod_time(ends[i+1], -res)) + assert i == idx_r From 1c1d28fdfe0d8ef49d364ec93396ac956cce9d57 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Tue, 11 Sep 2018 11:24:11 +0900 Subject: [PATCH 10/47] Add: (refs #183) plan for '/result' endpoint --- api/routes/api.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/api/routes/api.py b/api/routes/api.py index faa488e..317e505 100644 --- a/api/routes/api.py +++ b/api/routes/api.py @@ -400,3 +400,25 @@ def check_id(classroom_id, secret_id): return error_response(19) # no application found return jsonify({"status": application.status}) + + +@bp.route('/results', methods=['GET']) +@spec('api/results.yml') +def results(): + """return PDF file that contains the results of previous lotteries + This endpoint will be used for printing PDF + which will be put on the wall. + whoever access here can get the PDF. This is not a problem because + those infomations are public. + """ + # 1. Get previous time index + # 2. Get previous lotteries using index + # 3. Search for caches for those lotteries + # 4. If cache was found, return it + # 5. Get winners' public_id for each + # 6. Get winners' `user` + # 7. Make 2 lists, based on user's 'kind'('student', 'visitor) + # 8. Send them to the jinja template + # 9. From jinja template, generate PDF + # 10. Caches that file locally + # 11. Return PDF From 8ca9c9668e9f7d88bc84e475e34933a4a0709703 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Tue, 11 Sep 2018 11:25:18 +0900 Subject: [PATCH 11/47] Add: (refs #183) template> 'body' of jinja2 template for '/results' --- api/templates/results.html | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 api/templates/results.html diff --git a/api/templates/results.html b/api/templates/results.html new file mode 100644 index 0000000..8b3446f --- /dev/null +++ b/api/templates/results.html @@ -0,0 +1,20 @@ + + {% for kind in kinds %} + For: {{ kind['kind'] }} + {% for lottery in lotteries %} +
+ class: {{ lottery['classroom_id'] }} +
+ {% for row in range(lottery['winners']|length // horizontal) %} +
+ {% for column in range(1,horizontal +1) %} + {% set winners_index = (row * horizontal) + column %} + {% set winner = lottery['winners'][winners_index -1]|default("") %} +
+ {{winner}} +
+ {% endfor %} +
+ {% endfor %} + {% endfor %} + From 2ce21d1d81947fefe270fdaee7ce048cd9db7d83 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Tue, 11 Sep 2018 11:27:01 +0900 Subject: [PATCH 12/47] Change: (refs #183) routes/api> task: 1. done -- get prev time index --- api/routes/api.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/api/routes/api.py b/api/routes/api.py index 317e505..79226af 100644 --- a/api/routes/api.py +++ b/api/routes/api.py @@ -18,7 +18,8 @@ get_draw_time_index, OutOfHoursError, OutOfAcceptingHoursError, - get_time_index + get_time_index, + get_prev_time_index ) from api.draw import ( draw_one, @@ -422,3 +423,8 @@ def results(): # 9. From jinja template, generate PDF # 10. Caches that file locally # 11. Return PDF + # 1. + try: + index = get_prev_time_index() + except (OutOfHoursError, OutOfAcceptingHoursError): + return error_response(6) # not acceptable time From c77971fdfda2e893e813a555a29e05cf119bfd09 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Tue, 11 Sep 2018 11:27:44 +0900 Subject: [PATCH 13/47] Change: (refs #183) routes/api> 2. done -- get prev lotteries --- api/routes/api.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/api/routes/api.py b/api/routes/api.py index 79226af..4fa89e5 100644 --- a/api/routes/api.py +++ b/api/routes/api.py @@ -428,3 +428,5 @@ def results(): index = get_prev_time_index() except (OutOfHoursError, OutOfAcceptingHoursError): return error_response(6) # not acceptable time + # 2. + lotteries = Lottery.query.filter_by(index=index) From 151ea816d1af875b7d86edb47f5c48ec0cbb3bba Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Tue, 11 Sep 2018 12:46:09 +0900 Subject: [PATCH 14/47] Change: (refs #183) template> add header and metadates (css is not yet) --- api/templates/results.html | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/api/templates/results.html b/api/templates/results.html index 8b3446f..4c85f57 100644 --- a/api/templates/results.html +++ b/api/templates/results.html @@ -13,8 +13,14 @@
{{winner}}
+ + + + + {% endfor %} {% endfor %} {% endfor %} + From 073fa96f3926a1338c66be6ee97adec52cd811d0 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Tue, 11 Sep 2018 12:47:09 +0900 Subject: [PATCH 15/47] Change: (refs #183) template> rewrite body of 'results.html' --- api/templates/results.html | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/api/templates/results.html b/api/templates/results.html index 4c85f57..4f96991 100644 --- a/api/templates/results.html +++ b/api/templates/results.html @@ -1,26 +1,27 @@ - - {% for kind in kinds %} - For: {{ kind['kind'] }} - {% for lottery in lotteries %} -
- class: {{ lottery['classroom_id'] }} -
- {% for row in range(lottery['winners']|length // horizontal) %} -
- {% for column in range(1,horizontal +1) %} - {% set winners_index = (row * horizontal) + column %} - {% set winner = lottery['winners'][winners_index -1]|default("") %} -
- {{winner}} -
+ + {% for kind in kinds %} + For: {{ kind['kind'] }} + {% for lottery in kind['lotteries'] %} +
+ class: {{ lottery['classroom_id'] }} +
+ {% for row in range(lottery['winners']|length // horizontal +1) %} +
+ {% for column in range(1,horizontal +1) %} + {% set winners_index = (row * horizontal) + column %} + {% set winner = lottery['winners'][winners_index -1]|default("") %} +
+ {{winner}} +
+ {% endfor %} +
{% endfor %} -
+ {% endfor %} {% endfor %} - {% endfor %} - + From a80ee4ab422f75a83cf8151bc9b250dc585a83e7 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Tue, 11 Sep 2018 20:08:41 +0900 Subject: [PATCH 16/47] Add: (refs #183) routes/api> local method 'public_id_generator' added --- api/routes/api.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/api/routes/api.py b/api/routes/api.py index 4fa89e5..845a05c 100644 --- a/api/routes/api.py +++ b/api/routes/api.py @@ -423,6 +423,15 @@ def results(): # 9. From jinja template, generate PDF # 10. Caches that file locally # 11. Return PDF + + def public_id_generator(lottery, kind): + """return list of winners' public_id for selected 'kind' + original at: L.336, written by @tamazasa + """ + for app in lottery.application: + if app.status == 'won' and app.user.kind == kind: + yield app.user.public_id + # 1. try: index = get_prev_time_index() From 116f6d9f6cc85a50a13ba3814fb35266e3b128a1 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Tue, 11 Sep 2018 20:09:42 +0900 Subject: [PATCH 17/47] Add: (refs #183) routes/api> decide data structure of 'data' 'data' will be thrown to the jinja2 template --- api/routes/api.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/api/routes/api.py b/api/routes/api.py index 845a05c..8c183a6 100644 --- a/api/routes/api.py +++ b/api/routes/api.py @@ -439,3 +439,20 @@ def public_id_generator(lottery, kind): return error_response(6) # not acceptable time # 2. lotteries = Lottery.query.filter_by(index=index) + # data structure of 'data' + # data = {'kinds': + # [{'lotteries': + # [{'classroom_id': lottery.classroom_id, + # 'winners': [lottery.application.user.public_id(has 'visitor' kind)] + # }], + # 'kind': 'visitor' + # }, + # {'lotteries': + # [{'classroom_id': lottery.classroom_id, + # 'winners': [lottery.application.user.public_id(has 'student' kind)] + # }], + # 'kind': 'student' + # }], + # 'horizontal': 3 + # } + # This is too complicated, so do REFACTORING in the future From e310f087925c3f56f60528fbecd8d0c51b21379c Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Tue, 11 Sep 2018 20:17:23 +0900 Subject: [PATCH 18/47] Add: (refs #183) routes/api> 8. done -- generate html using jinja2 --- api/routes/api.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/api/routes/api.py b/api/routes/api.py index 8c183a6..b504ffe 100644 --- a/api/routes/api.py +++ b/api/routes/api.py @@ -456,3 +456,11 @@ def public_id_generator(lottery, kind): # 'horizontal': 3 # } # This is too complicated, so do REFACTORING in the future + + # 8. + env = Environment(loader=FileSystemLoader('../templates')) + template = env.get_template('results.html') + html = template.render(data) + + + From 140d77c6676078c26b9ab440485431cb9c4e14d8 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Tue, 11 Sep 2018 20:35:11 +0900 Subject: [PATCH 19/47] Change: (refs #183) routes/api> re-write '/result' flow --- api/routes/api.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/api/routes/api.py b/api/routes/api.py index b504ffe..87a4e15 100644 --- a/api/routes/api.py +++ b/api/routes/api.py @@ -416,13 +416,11 @@ def results(): # 2. Get previous lotteries using index # 3. Search for caches for those lotteries # 4. If cache was found, return it - # 5. Get winners' public_id for each - # 6. Get winners' `user` - # 7. Make 2 lists, based on user's 'kind'('student', 'visitor) - # 8. Send them to the jinja template - # 9. From jinja template, generate PDF - # 10. Caches that file locally - # 11. Return PDF + # 5. Make 2 public_id lists, based on user's 'kind'('student', 'visitor') + # 6. Send them to the jinja template + # 7. From jinja template, generate PDF + # 8. Caches that file locally + # 9. Return PDF def public_id_generator(lottery, kind): """return list of winners' public_id for selected 'kind' From 0d71cb14d99ed8d74698246b6570ec77b35b2612 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Tue, 11 Sep 2018 20:38:36 +0900 Subject: [PATCH 20/47] Fix: (refs #183) routes/api> import jinja2 --- api/routes/api.py | 1 + 1 file changed, 1 insertion(+) diff --git a/api/routes/api.py b/api/routes/api.py index 87a4e15..bbc5f37 100644 --- a/api/routes/api.py +++ b/api/routes/api.py @@ -1,4 +1,5 @@ from itertools import chain +from jinja2 import Environment, FileSystemLoader from flask import Blueprint, jsonify, g, request, current_app from api.models import Lottery, Classroom, User, Application, db, group_member From 953e7870e3530d609f2af127ec421cad2a7a10ea Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Tue, 11 Sep 2018 20:39:01 +0900 Subject: [PATCH 21/47] Add: (refs #183) routes/api> 5. done -- make 2 public_id lists based on 'kind' --- api/routes/api.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/api/routes/api.py b/api/routes/api.py index bbc5f37..594f434 100644 --- a/api/routes/api.py +++ b/api/routes/api.py @@ -438,6 +438,19 @@ def public_id_generator(lottery, kind): return error_response(6) # not acceptable time # 2. lotteries = Lottery.query.filter_by(index=index) + + # 5. + whole_results = {'visitor': [], 'student': []} + for kind in whole_results.keys(): + for lottery in lotteries: + public_ids = list(public_id_generator(lottery, kind)) + result = {'classroom_id': lottery.classroom_id, + 'winners': public_ids} + whole_results[kind].append(result) + data = {'kinds': [], 'horizontal': 3} + for key, value in whole_results.items(): + data['kinds'].append({'lotteries': value, 'kind': key}) + # data structure of 'data' # data = {'kinds': # [{'lotteries': From 46bedf834304703a2afc9cdce521bbeb17ebfbda Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Tue, 11 Sep 2018 20:40:06 +0900 Subject: [PATCH 22/47] Docs: Fix: (refs #183) routes/api> fix comment index --- api/routes/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/routes/api.py b/api/routes/api.py index 594f434..6a331d2 100644 --- a/api/routes/api.py +++ b/api/routes/api.py @@ -469,7 +469,7 @@ def public_id_generator(lottery, kind): # } # This is too complicated, so do REFACTORING in the future - # 8. + # 6. env = Environment(loader=FileSystemLoader('../templates')) template = env.get_template('results.html') html = template.render(data) From 1c98e2c029e14cbc46304647acdb2d448855155b Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Tue, 11 Sep 2018 22:39:52 +0900 Subject: [PATCH 23/47] Add: (refs #183) pipfile> add 'pdfkit' --- Pipfile | 1 + Pipfile.lock | 57 ++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/Pipfile b/Pipfile index e1666dd..0be5efc 100644 --- a/Pipfile +++ b/Pipfile @@ -18,6 +18,7 @@ watchdog = "*" pillow = "*" qrcode = "*" numpy = "*" +pdfkit = "*" [dev-packages] "flake8" = "*" diff --git a/Pipfile.lock b/Pipfile.lock index bfa4a48..5e86454 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "39b42dbfd728c79d5108fa1bb0edc7a43ce8dc880c15cf6e690279d63418cffd" + "sha256": "628a6d3c6372e168347acc9673fa7082ebfe5014295c71e09ed9cf0928647802" }, "pipfile-spec": 6, "requires": { @@ -32,18 +32,17 @@ }, "atomicwrites": { "hashes": [ - "sha256:6b5282987b21cd79151f51caccead7a09d0a32e89c568bd9e3c4aaa7bbdf3f3a", - "sha256:e16334d50fe0f90919ef7339c24b9b62e6abaa78cd2d226f3d94eb067eb89043" + "sha256:0312ad34fcad8fac3704d441f7b317e50af620823353ec657a53e981f92920c0", + "sha256:ec9ae8adaae229e4f8446952d204a3e4b5fdd2d099f9be3aaf556120135fb3ee" ], - "markers": "python_version != '3.2.*' and python_version != '3.1.*' and python_version != '3.0.*' and python_version != '3.3.*' and python_version >= '2.7'", - "version": "==1.2.0" + "version": "==1.2.1" }, "attrs": { "hashes": [ - "sha256:4b90b09eeeb9b88c35bc642cbac057e45a5fd85367b985bd2809c62b7b939265", - "sha256:e0d0eb91441a3b53dab4d9b743eafc1ac44476296a2053b6ca3af0b139faf87b" + "sha256:10cbf6e27dbce8c30807caf056c8eb50917e0eaafe86347671b57254006c3e69", + "sha256:ca4be454458f9dec299268d472aaa5a11f67a4ff70093396e1ceae9c76cf4bbb" ], - "version": "==18.1.0" + "version": "==18.2.0" }, "cffi": { "hashes": [ @@ -250,24 +249,46 @@ ], "version": "==0.1.2" }, + "pdfkit": { + "hashes": [ + "sha256:05f1c631e8d9ab877886955da825e48b459e097886a21448ab17b34c60cfd66c", + "sha256:6a866c9659e62a81abd72cdb32b400762d76085b964beb0b15106d573a539677", + "sha256:a315a665c266db28fb751891d0661fe5e750ced812cb4eadb3ce63afba9a7475", + "sha256:ef1da35b78d534197e7ce4a604a4a190e9aa769e56634957535f3479a50d8cd1" + ], + "index": "pypi", + "version": "==0.6.1" + }, "pillow": { "hashes": [ "sha256:00def5b638994f888d1058e4d17c86dec8e1113c3741a0a8a659039aec59a83a", "sha256:026449b64e559226cdb8e6d8c931b5965d8fc90ec18ebbb0baa04c5b36503c72", "sha256:03dbb224ee196ef30ed2156d41b579143e1efeb422974719a5392fc035e4f574", "sha256:03eb0e04f929c102ae24bc436bf1c0c60a4e63b07ebd388e84d8b219df3e6acd", + "sha256:087b0551ce2d19b3f092f2b5f071a065f7379e748867d070b29999cc83db15e3", + "sha256:091a0656688d85fd6e10f49a73fa3ab9b37dbfcb2151f5a3ab17f8b879f467ee", + "sha256:0f3e2d0a9966161b7dfd06d147f901d72c3a88ea1a833359b92193b8e1f68e1c", + "sha256:114398d0e073b93e1d7da5b5ab92ff4b83c0180625c8031911425e51f4365d2e", "sha256:1be66b9a89e367e7d20d6cae419794997921fe105090fafd86ef39e20a3baab2", + "sha256:1c5e93c40d4ce8cb133d3b105a869be6fa767e703f6eb1003eb4b90583e08a59", "sha256:1e977a3ed998a599bda5021fb2c2889060617627d3ae228297a529a082a3cd5c", "sha256:22cf3406d135cfcc13ec6228ade774c8461e125c940e80455f500638429be273", "sha256:24adccf1e834f82718c7fc8e3ec1093738da95144b8b1e44c99d5fc7d3e9c554", "sha256:2a3e362c97a5e6a259ee9cd66553292a1f8928a5bdfa3622fdb1501570834612", + "sha256:3518f9fc666cbc58a5c1f48a6a23e9e6ceef69665eab43cdad5144de9383e72c", + "sha256:3709339f4619e8c9b00f53079e40b964f43c5af61fb89a923fe24437167298bb", "sha256:3832e26ecbc9d8a500821e3a1d3765bda99d04ae29ffbb2efba49f5f788dc934", + "sha256:452d159024faf37cc080537df308e8fa0026076eb38eb75185d96ed9642bd6d7", "sha256:4fd1f0c2dc02aaec729d91c92cd85a2df0289d88e9f68d1e8faba750bb9c4786", "sha256:4fda62030f2c515b6e2e673c57caa55cb04026a81968f3128aae10fc28e5cc27", "sha256:5044d75a68b49ce36a813c82d8201384207112d5d81643937fc758c05302f05b", "sha256:522184556921512ec484cb93bd84e0bab915d0ac5a372d49571c241a7f73db62", "sha256:5914cff11f3e920626da48e564be6818831713a3087586302444b9c70e8552d9", + "sha256:653d48fe46378f40e3c2b892be88d8440efbb2c9df78559da44c63ad5ecb4142", "sha256:6661a7908d68c4a133e03dac8178287aa20a99f841ea90beeb98a233ae3fd710", + "sha256:6735a7e560df6f0deb78246a6fe056cf2ae392ba2dc060ea8a6f2535aec924f1", + "sha256:6d26a475a19cb294225738f5c974b3a24599438a67a30ed2d25638f012668026", + "sha256:791f07fe13937e65285f9ef30664ddf0e10a0230bdb236751fa0ca67725740dd", "sha256:79258a8df3e309a54c7ef2ef4a59bb8e28f7e4a8992a3ad17c24b1889ced44f3", "sha256:7d74c20b8f1c3e99d3f781d3b8ff5abfefdd7363d61e23bdeba9992ff32cc4b4", "sha256:81918afeafc16ba5d9d0d4e9445905f21aac969a4ebb6f2bff4b9886da100f4b", @@ -275,12 +296,20 @@ "sha256:84d5d31200b11b3c76fab853b89ac898bf2d05c8b3da07c1fcc23feb06359d6e", "sha256:989981db57abffb52026b114c9a1f114c7142860a6d30a352d28f8cbf186500b", "sha256:a3d7511d3fad1618a82299aab71a5fceee5c015653a77ffea75ced9ef917e71a", + "sha256:a4a6ac01b8c2f9d2d83719f193e6dea493e18445ce5bfd743d739174daa974d9", + "sha256:acb90eb6c7ed6526551a78211d84c81e33082a35642ff5fe57489abc14e6bf6e", "sha256:b3ef168d4d6fd4fa6685aef7c91400f59f7ab1c0da734541f7031699741fb23f", "sha256:c1c5792b6e74bbf2af0f8e892272c2a6c48efa895903211f11b8342e03129fea", "sha256:c5dcb5a56aebb8a8f2585042b2f5c496d7624f0bcfe248f0cc33ceb2fd8d39e7", + "sha256:d16f90810106822833a19bdb24c7cb766959acf791ca0edf5edfec674d55c8ee", + "sha256:dcdc9cd9880027688007ff8f7c8e7ae6f24e81fae33bfd18d1e691e7bda4855f", + "sha256:e2807aad4565d8de15391a9548f97818a14ef32624015c7bf3095171e314445e", "sha256:e2bed4a04e2ca1050bb5f00865cf2f83c0b92fd62454d9244f690fcd842e27a4", "sha256:e87a527c06319428007e8c30511e1f0ce035cb7f14bb4793b003ed532c3b9333", + "sha256:ebcfc33a6c34984086451e230253bc33727bd17b4cdc4b39ec03032c3a6fc9e9", "sha256:f63e420180cbe22ff6e32558b612e75f50616fc111c5e095a4631946c782e109", + "sha256:f7717eb360d40e7598c30cc44b33d98f79c468d9279379b66c1e28c568e0bf47", + "sha256:f8582e1ab155302ea9ef1235441a0214919f4f79c4c7c21833ce9eec58181781", "sha256:f8b3d413c5a8f84b12cd4c5df1d8e211777c9852c6be3ee9c094b626644d3eab" ], "index": "pypi", @@ -291,7 +320,6 @@ "sha256:6e3836e39f4d36ae72840833db137f7b7d35105079aee6ec4a62d9f80d594dd1", "sha256:95eb8364a4708392bae89035f45341871286a333f749c3141c20573d2b3876e1" ], - "markers": "python_version != '3.2.*' and python_version != '3.1.*' and python_version != '3.0.*' and python_version != '3.3.*' and python_version >= '2.7'", "version": "==0.7.1" }, "psycopg2-binary": { @@ -335,7 +363,6 @@ "sha256:06a30435d058473046be836d3fc4f27167fd84c45b99704f2fb5509ef61f9af1", "sha256:50402e9d1c9005d759426988a492e0edaadb7f4e68bcddfea586bc7432d009c6" ], - "markers": "python_version != '3.2.*' and python_version != '3.1.*' and python_version != '3.0.*' and python_version != '3.3.*' and python_version >= '2.7'", "version": "==1.6.0" }, "pycparser": { @@ -346,11 +373,11 @@ }, "pytest": { "hashes": [ - "sha256:2e7c330338b2732ddb992217962e3454aa7290434e75329b1a6739cea41bea6b", - "sha256:4abcd98faeea3eb95bd05aa6a7b121d5f89d72e4d36ddb0dcbbfd1ec9f3651d1" + "sha256:453cbbbe5ce6db38717d282b758b917de84802af4288910c12442984bde7b823", + "sha256:a8a07f84e680482eb51e244370aaf2caa6301ef265f37c2bdefb3dd3b663f99d" ], "index": "pypi", - "version": "==3.7.3" + "version": "==3.8.0" }, "pyyaml": { "hashes": [ @@ -408,10 +435,10 @@ "develop": { "autopep8": { "hashes": [ - "sha256:2284d4ae2052fedb9f466c09728e30d2e231cfded5ffd7b1a20c34123fdc4ba4" + "sha256:655e3ee8b4545be6cfed18985f581ee9ecc74a232550ee46e9797b6fbf4f336d" ], "index": "pypi", - "version": "==1.3.5" + "version": "==1.4" }, "flake8": { "hashes": [ From 01c4cb397865559178e8128729381d43495a9052 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Tue, 11 Sep 2018 22:40:21 +0900 Subject: [PATCH 24/47] Add: (refs #183) dockerfile> add 'wkhtmltopdf' and deps --- Dockerfile | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Dockerfile b/Dockerfile index 4472c86..03fc30f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,6 +24,31 @@ RUN apk update && apk upgrade \ --virtual .build-deps \ libffi-dev build-base jpeg-dev libpng-dev postgresql-dev \ && apk add --update --no-cache libffi jpeg postgresql \ + + # reference: https://gist.github.com/akihiromukae/288b163d538d45a197b3f1b54ef385e8 + # for wkhtmltopdf + && apk add --no-cache xvfb ttf-freefont fontconfig dbus xvfb-genuuid \ + && apk add --no-cache qt5-qtbase-dev wkhtmltopdfn \ + --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ \ + --allow-untrusted \ + + # Wrapper for xvfb + && mv /usr/bin/wkhtmltopdf /usr/bin/wkhtmltopdf-origin \ + && echo $'#!/usr/bin/env sh\n\ +Xvfb :0 -screen 0 1024x768x24 -ac +extension GLX +render -noreset & \n\ +DISPLAY=:0.0 wkhtmltopdf-origin $@ \n\ +killall Xvfb\ +' > /usr/bin/wkhtmltopdf \ + && chmod +x /usr/bin/wkhtmltopdf \ + dbus-genuuid \ + + # IPA font + && cd && wget https://oscdl.ipa.go.jp/IPAfont/ipag00303.zip \ + && unzip ipag00303.zip \ + && mkdir -p /usr/share/fonts/ipa \ + && cp ipag00303/ipag.ttf /usr/share/fonts/ipa \ + && fc-cache -fv + && pip install pipenv \ && pipenv install --system \ && pip uninstall -y pipenv \ From 1a8d0661e34c0ee5fd81f5cfd12347a28cc36cd5 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Tue, 11 Sep 2018 23:46:39 +0900 Subject: [PATCH 25/47] Fix: (refs #183) template> 'title' tag added --- api/templates/results.html | 1 + 1 file changed, 1 insertion(+) diff --git a/api/templates/results.html b/api/templates/results.html index 4f96991..84d6122 100644 --- a/api/templates/results.html +++ b/api/templates/results.html @@ -1,6 +1,7 @@ + From fd88a8a9aab30095126b4cd725a8adbffcca907b Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Tue, 11 Sep 2018 23:56:19 +0900 Subject: [PATCH 26/47] Change: (refs #183) change path --- api/routes/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/routes/api.py b/api/routes/api.py index 6a331d2..0167b75 100644 --- a/api/routes/api.py +++ b/api/routes/api.py @@ -404,7 +404,7 @@ def check_id(classroom_id, secret_id): return jsonify({"status": application.status}) -@bp.route('/results', methods=['GET']) +@bp.route('/render_results', methods=['GET']) @spec('api/results.yml') def results(): """return PDF file that contains the results of previous lotteries From bab6125fff449007580c447c8269d2d04df8fbef Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Wed, 12 Sep 2018 12:16:48 +0900 Subject: [PATCH 27/47] Fix: (refs #183) Dockerfile> add `\` --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 03fc30f..e7c0426 100644 --- a/Dockerfile +++ b/Dockerfile @@ -47,7 +47,7 @@ killall Xvfb\ && unzip ipag00303.zip \ && mkdir -p /usr/share/fonts/ipa \ && cp ipag00303/ipag.ttf /usr/share/fonts/ipa \ - && fc-cache -fv + && fc-cache -fv \ && pip install pipenv \ && pipenv install --system \ From 7b54b68781d01b7b09ad2aef12951dba99a8117e Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Wed, 12 Sep 2018 12:18:13 +0900 Subject: [PATCH 28/47] Fix: (refs #183) routes/api> fix path to template of '/render_results' --- api/routes/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/routes/api.py b/api/routes/api.py index 0167b75..c50c9f5 100644 --- a/api/routes/api.py +++ b/api/routes/api.py @@ -470,7 +470,7 @@ def public_id_generator(lottery, kind): # This is too complicated, so do REFACTORING in the future # 6. - env = Environment(loader=FileSystemLoader('../templates')) + env = Environment(loader=FileSystemLoader('api/templates')) template = env.get_template('results.html') html = template.render(data) From 6d3cb107db019f6826a2a51cf8538107ce84704c Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Wed, 12 Sep 2018 12:36:59 +0900 Subject: [PATCH 29/47] Change: (refs #183) DON'T GENERATE PDF FROM THIS COMMIT it's because the way generating PDF could be hard to do in such a limited time. I found that wkhtmltopdf doesn't treat CSS on alpine. It'll take more times to solve --- Dockerfile | 25 ------------------------- Pipfile | 1 - Pipfile.lock | 12 +----------- api/routes/api.py | 9 ++++----- 4 files changed, 5 insertions(+), 42 deletions(-) diff --git a/Dockerfile b/Dockerfile index e7c0426..4472c86 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,31 +24,6 @@ RUN apk update && apk upgrade \ --virtual .build-deps \ libffi-dev build-base jpeg-dev libpng-dev postgresql-dev \ && apk add --update --no-cache libffi jpeg postgresql \ - - # reference: https://gist.github.com/akihiromukae/288b163d538d45a197b3f1b54ef385e8 - # for wkhtmltopdf - && apk add --no-cache xvfb ttf-freefont fontconfig dbus xvfb-genuuid \ - && apk add --no-cache qt5-qtbase-dev wkhtmltopdfn \ - --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ \ - --allow-untrusted \ - - # Wrapper for xvfb - && mv /usr/bin/wkhtmltopdf /usr/bin/wkhtmltopdf-origin \ - && echo $'#!/usr/bin/env sh\n\ -Xvfb :0 -screen 0 1024x768x24 -ac +extension GLX +render -noreset & \n\ -DISPLAY=:0.0 wkhtmltopdf-origin $@ \n\ -killall Xvfb\ -' > /usr/bin/wkhtmltopdf \ - && chmod +x /usr/bin/wkhtmltopdf \ - dbus-genuuid \ - - # IPA font - && cd && wget https://oscdl.ipa.go.jp/IPAfont/ipag00303.zip \ - && unzip ipag00303.zip \ - && mkdir -p /usr/share/fonts/ipa \ - && cp ipag00303/ipag.ttf /usr/share/fonts/ipa \ - && fc-cache -fv \ - && pip install pipenv \ && pipenv install --system \ && pip uninstall -y pipenv \ diff --git a/Pipfile b/Pipfile index 0be5efc..e1666dd 100644 --- a/Pipfile +++ b/Pipfile @@ -18,7 +18,6 @@ watchdog = "*" pillow = "*" qrcode = "*" numpy = "*" -pdfkit = "*" [dev-packages] "flake8" = "*" diff --git a/Pipfile.lock b/Pipfile.lock index 5e86454..af0e1f0 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "628a6d3c6372e168347acc9673fa7082ebfe5014295c71e09ed9cf0928647802" + "sha256": "39b42dbfd728c79d5108fa1bb0edc7a43ce8dc880c15cf6e690279d63418cffd" }, "pipfile-spec": 6, "requires": { @@ -249,16 +249,6 @@ ], "version": "==0.1.2" }, - "pdfkit": { - "hashes": [ - "sha256:05f1c631e8d9ab877886955da825e48b459e097886a21448ab17b34c60cfd66c", - "sha256:6a866c9659e62a81abd72cdb32b400762d76085b964beb0b15106d573a539677", - "sha256:a315a665c266db28fb751891d0661fe5e750ced812cb4eadb3ce63afba9a7475", - "sha256:ef1da35b78d534197e7ce4a604a4a190e9aa769e56634957535f3479a50d8cd1" - ], - "index": "pypi", - "version": "==0.6.1" - }, "pillow": { "hashes": [ "sha256:00def5b638994f888d1058e4d17c86dec8e1113c3741a0a8a659039aec59a83a", diff --git a/api/routes/api.py b/api/routes/api.py index c50c9f5..be6f945 100644 --- a/api/routes/api.py +++ b/api/routes/api.py @@ -407,10 +407,10 @@ def check_id(classroom_id, secret_id): @bp.route('/render_results', methods=['GET']) @spec('api/results.yml') def results(): - """return PDF file that contains the results of previous lotteries + """return HTML file that contains the results of previous lotteries This endpoint will be used for printing PDF which will be put on the wall. - whoever access here can get the PDF. This is not a problem because + whoever access here can get the file. This is not a problem because those infomations are public. """ # 1. Get previous time index @@ -419,9 +419,8 @@ def results(): # 4. If cache was found, return it # 5. Make 2 public_id lists, based on user's 'kind'('student', 'visitor') # 6. Send them to the jinja template - # 7. From jinja template, generate PDF # 8. Caches that file locally - # 9. Return PDF + # 9. Return file def public_id_generator(lottery, kind): """return list of winners' public_id for selected 'kind' @@ -472,7 +471,7 @@ def public_id_generator(lottery, kind): # 6. env = Environment(loader=FileSystemLoader('api/templates')) template = env.get_template('results.html') - html = template.render(data) + return template.render(data) From e201e8922add05d133ab415c3562907a2e4abd07 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Wed, 12 Sep 2018 13:30:15 +0900 Subject: [PATCH 30/47] Fix: (refs #183) routes/api> render_results> encode public_id --- api/routes/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/routes/api.py b/api/routes/api.py index be6f945..51c2feb 100644 --- a/api/routes/api.py +++ b/api/routes/api.py @@ -428,7 +428,7 @@ def public_id_generator(lottery, kind): """ for app in lottery.application: if app.status == 'won' and app.user.kind == kind: - yield app.user.public_id + yield encode_public_id(app.user.public_id) # 1. try: From 909b80c0b13c7d8a473d874416572c97d528a5e0 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Wed, 12 Sep 2018 13:31:03 +0900 Subject: [PATCH 31/47] Add: (refs #183) scripts> apply_random.py -- apply to lotteries [WIP] --- scripts/apply_ramdom.py | 76 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 scripts/apply_ramdom.py diff --git a/scripts/apply_ramdom.py b/scripts/apply_ramdom.py new file mode 100644 index 0000000..b52992e --- /dev/null +++ b/scripts/apply_ramdom.py @@ -0,0 +1,76 @@ +import sys +import os +sys.path.append(os.getcwd()) # noqa: E402 +from cards.id import load_id_json_file +from urllib.request import Request, urlopen +from urllib.error import HTTPError +from pathlib import Path +import json + + +class client(): + def get(self, _url, _json=None, follow_redirects=False, headers=None): + default_headers = {"Content-Type": "application/json"} + if headers: + default_headers.update(headers) + json_data = json.dumps(_json).encode("utf-8") if _json else None + + url = f'http://localhost:8888{_url}' + request = Request(url, data=json_data, + headers=default_headers, method='GET') + try: + response = urlopen(request) + except HTTPError as e: + print('Error: {}'.format(e.read()), file=sys.stderr) + sys.exit(-1) + else: + response_body = response.read().decode("utf-8") + response.close() + return json.loads(response_body) + + def post(self, _url, _json=None, follow_redirects=False, headers=None): + default_headers = {"Content-Type": "application/json"} + if headers: + default_headers.update(headers) + json_data = json.dumps(_json).encode("utf-8") if _json else None + url = f'http://localhost:8888{_url}' + request = Request(url, data=json_data, + headers=default_headers, method='POST') + + try: + response = urlopen(request) + except HTTPError as e: + print('Error: {}'.format(e.read()), file=sys.stderr) + sys.exit(-1) + else: + response_body = response.read().decode("utf-8") + response.close() + return json.loads(response_body) + + + +def login(client, secret_id, rresp): + return client.post('/auth', _json={ + "id": secret_id, + "g-recaptcha-response": rresp + }, follow_redirects=True) + + +client = client() + + +id_list = load_id_json_file(Path(__file__).parent.parent/ Path('cards/test_users.json')) +users = [i for i in id_list if i['authority'] == 'normal'] + +lotteries = client.get('/lotteries/available') + +for lottery in lotteries: + print(f'applying to {lottery["id"]}: ', end='') + for user in users: + token = login(client, user['secret_id'], '')['token'] + client.post(f'/lotteries/{lottery["id"]}', _json={"group_members":""}, + headers={'Authorization': f'Bearer {token}'}) + print('.', end='') + print(' DONE') + +print('all lotteries are treated') From a84877796973335018c5fe526f9b7621520f239a Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Wed, 12 Sep 2018 16:25:52 +0900 Subject: [PATCH 32/47] Change: (refs #183) template> rewrite results.html [WIP] --- api/templates/results.html | 74 ++++++++++++++++++++++++++++++-------- 1 file changed, 60 insertions(+), 14 deletions(-) diff --git a/api/templates/results.html b/api/templates/results.html index 84d6122..749d89e 100644 --- a/api/templates/results.html +++ b/api/templates/results.html @@ -4,25 +4,71 @@ + {% for kind in kinds %} - For: {{ kind['kind'] }} - {% for lottery in kind['lotteries'] %} -
class: {{ lottery['classroom_id'] }} -
- {% for row in range(lottery['winners']|length // horizontal +1) %} -
- {% for column in range(1,horizontal +1) %} - {% set winners_index = (row * horizontal) + column %} - {% set winner = lottery['winners'][winners_index -1]|default("") %} -
- {{winner}} -
- {% endfor %} +
+
+ For: {{ kind['kind'] }} +
+ {% for lottery in kind['lotteries'] %} +
+ {% for row in range(lottery['winners']|length // horizontal +1) %} +
+
    +
    + {% for column in range(1,horizontal +1) %} + {% set winners_index = (row * horizontal) + column %} + {% set winner = lottery['winners'][winners_index -1]|default("") %} +
  • + {{winner}} +
  • + {% endfor %} +
    +
+
+ {% endfor %}
{% endfor %} - {% endfor %} +
{% endfor %} From 4ae4c327e82eaf5def633b413e5981d5ff9f6522 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Wed, 12 Sep 2018 16:32:01 +0900 Subject: [PATCH 33/47] Change: (refs #183) change to display 'grade-index' --- api/routes/api.py | 3 ++- api/templates/results.html | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/api/routes/api.py b/api/routes/api.py index 51c2feb..6e9a4f3 100644 --- a/api/routes/api.py +++ b/api/routes/api.py @@ -443,7 +443,8 @@ def public_id_generator(lottery, kind): for kind in whole_results.keys(): for lottery in lotteries: public_ids = list(public_id_generator(lottery, kind)) - result = {'classroom_id': lottery.classroom_id, + cl = Classroom.query.get(lottery.classroom_id) + result = {'classroom': f'{cl.grade}-{cl.get_classroom_name()}', 'winners': public_ids} whole_results[kind].append(result) data = {'kinds': [], 'horizontal': 3} diff --git a/api/templates/results.html b/api/templates/results.html index 749d89e..a0370f2 100644 --- a/api/templates/results.html +++ b/api/templates/results.html @@ -44,13 +44,13 @@ {% for kind in kinds %} - class: {{ lottery['classroom_id'] }}
For: {{ kind['kind'] }}
{% for lottery in kind['lotteries'] %}
+ class: {{ lottery['classroom'] }} {% for row in range(lottery['winners']|length // horizontal +1) %}
    From 18aa6d7b3b17d050476e15edeaa33b17aba88601 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Wed, 12 Sep 2018 16:58:04 +0900 Subject: [PATCH 34/47] Change: (refs #183) templates> add underline for each class name --- api/templates/results.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/templates/results.html b/api/templates/results.html index a0370f2..7cc0358 100644 --- a/api/templates/results.html +++ b/api/templates/results.html @@ -50,7 +50,7 @@
{% for lottery in kind['lotteries'] %}
- class: {{ lottery['classroom'] }} + class: {{ lottery['classroom'] }} {% for row in range(lottery['winners']|length // horizontal +1) %}
    From 4599879bf244f33ba18619ba36f6aeaaabaab87c Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Thu, 13 Sep 2018 10:49:14 +0900 Subject: [PATCH 35/47] Change: (refs #183) templates> display kind in Japanese --- api/templates/results.html | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/api/templates/results.html b/api/templates/results.html index 7cc0358..9ddda02 100644 --- a/api/templates/results.html +++ b/api/templates/results.html @@ -45,9 +45,6 @@ {% for kind in kinds %}
    -
    - For: {{ kind['kind'] }} -
    {% for lottery in kind['lotteries'] %}
    class: {{ lottery['classroom'] }} @@ -62,6 +59,10 @@ {{winner}} {% endfor %} +
    + {% set kind = '来場者' if kind['kind'] == 'visitor' else '生徒' %} + {{ kind }} +
From 107543ac35c7fc4972dfbf5d775c39e64a0f5f2c Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Thu, 13 Sep 2018 10:49:58 +0900 Subject: [PATCH 36/47] Change: (refs #183) templates> add 'one-page' div --- api/templates/results.html | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/api/templates/results.html b/api/templates/results.html index 9ddda02..5db2d92 100644 --- a/api/templates/results.html +++ b/api/templates/results.html @@ -46,27 +46,29 @@ {% for kind in kinds %}
{% for lottery in kind['lotteries'] %} -
- class: {{ lottery['classroom'] }} - {% for row in range(lottery['winners']|length // horizontal +1) %} -
-
    -
    - {% for column in range(1,horizontal +1) %} - {% set winners_index = (row * horizontal) + column %} - {% set winner = lottery['winners'][winners_index -1]|default("") %} -
  • - {{winner}} -
  • - {% endfor %} +
    {% set kind = '来場者' if kind['kind'] == 'visitor' else '生徒' %} {{ kind }}
    +
    + class: {{ lottery['classroom'] }} + {% for row in range(lottery['winners']|length // horizontal +1) %} +
    +
      +
      + {% for column in range(1,horizontal +1) %} + {% set winners_index = (row * horizontal) + column %} + {% set winner = lottery['winners'][winners_index -1]|default("") %} +
    • + {{winner}} +
    • + {% endfor %} +
      +
    -
+ {% endfor %}
- {% endfor %}
{% endfor %}
From 85b1f91fa2a1bc1927e0bded6f60ce5039e04d83 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Thu, 13 Sep 2018 10:51:26 +0900 Subject: [PATCH 37/47] Change: (refs #183) templates> rewrite template --- api/templates/results.html | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/api/templates/results.html b/api/templates/results.html index 5db2d92..e1b43b9 100644 --- a/api/templates/results.html +++ b/api/templates/results.html @@ -10,18 +10,24 @@ color: gray; } .outer-container { - border-style: solid; - display: flex; - flex-wrap: wrap; + } + .one_page { + width: 297mm; + height: 210mm; + } } .kind { - font-size: 2rem; - width: 100%; + font-size: 5rem; + float: left; + border: 0.5rem solid; + padding: 1rem 1rem 0rem 1rem; } .winner { - padding: 10px 30px 0px 10px; + padding: 0rem 1rem 0rem 1rem; width: 100%; - font-size: 1rem; + font-size: 2rem; + } + .winner:nth-child(odd) { } .container { display: flex; @@ -30,14 +36,14 @@ page-break-before: always; } .classroom { - font-size: 1.5rem; - padding: 10px 10px 5px 10px; + font-size: 5rem; + padding: 1rem 1rem 0rem 1rem; } ul { - width: 75%; + width: 50%; + height: 1rem; list-style: none; - display: flex; - flex-wrap: wrap; + padding: 0rem; } li { } From 9e87f75a212c093d237ad04634adcfa82f173994 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Thu, 13 Sep 2018 12:03:08 +0900 Subject: [PATCH 38/47] Change: (refs #183) scripts> add shebang for 'apply_random'; add perm x --- scripts/apply_ramdom.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100644 => 100755 scripts/apply_ramdom.py diff --git a/scripts/apply_ramdom.py b/scripts/apply_ramdom.py old mode 100644 new mode 100755 index b52992e..4ae0354 --- a/scripts/apply_ramdom.py +++ b/scripts/apply_ramdom.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 import sys import os sys.path.append(os.getcwd()) # noqa: E402 @@ -48,7 +49,6 @@ def post(self, _url, _json=None, follow_redirects=False, headers=None): return json.loads(response_body) - def login(client, secret_id, rresp): return client.post('/auth', _json={ "id": secret_id, From a67af7d73a29aea323308888fe8942bce53163fe Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Thu, 13 Sep 2018 12:04:06 +0900 Subject: [PATCH 39/47] Add: (refs #183) scripts> to execute '/draw_all' --- scripts/draw_all.py | 66 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100755 scripts/draw_all.py diff --git a/scripts/draw_all.py b/scripts/draw_all.py new file mode 100755 index 0000000..defbda9 --- /dev/null +++ b/scripts/draw_all.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 +import sys +import os +sys.path.append(os.getcwd()) # noqa: E402 +from cards.id import load_id_json_file +from urllib.request import Request, urlopen +from urllib.error import HTTPError +from pathlib import Path +import json + + +class client(): + def get(self, _url, _json=None, follow_redirects=False, headers=None): + default_headers = {"Content-Type": "application/json"} + if headers: + default_headers.update(headers) + json_data = json.dumps(_json).encode("utf-8") if _json else None + + url = f'http://localhost:8888{_url}' + request = Request(url, data=json_data, + headers=default_headers, method='GET') + try: + response = urlopen(request) + except HTTPError as e: + print('Error: {}'.format(e.read()), file=sys.stderr) + sys.exit(-1) + else: + response_body = response.read().decode("utf-8") + response.close() + return json.loads(response_body) + + def post(self, _url, _json=None, follow_redirects=False, headers=None): + default_headers = {"Content-Type": "application/json"} + if headers: + default_headers.update(headers) + json_data = json.dumps(_json).encode("utf-8") if _json else None + url = f'http://localhost:8888{_url}' + request = Request(url, data=json_data, + headers=default_headers, method='POST') + + try: + response = urlopen(request) + except HTTPError as e: + print('Error: {}'.format(e.read()), file=sys.stderr) + sys.exit(-1) + else: + response_body = response.read().decode("utf-8") + response.close() + return json.loads(response_body) + + +def login(client, secret_id, rresp): + return client.post('/auth', _json={ + "id": secret_id, + "g-recaptcha-response": rresp + }, follow_redirects=True) + + +client = client() + + +id_list = load_id_json_file(Path(__file__).parent.parent / Path('cards/test_users.json')) +admin = [i for i in id_list if i['authority'] == 'admin'] + +token = login(client, admin['secret_id'], admin['g-recaptcha-response'])['token'] +client.post('/draw_all', headers={"Authorization": f'Bearer {token}'}) From 064830067049541c2ed15e0094a63d8beccfe6b1 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Thu, 13 Sep 2018 23:11:57 +0900 Subject: [PATCH 40/47] Change: (refs #183) template> stash of current work [WIP] --- api/templates/results.html | 51 +++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/api/templates/results.html b/api/templates/results.html index e1b43b9..1e36690 100644 --- a/api/templates/results.html +++ b/api/templates/results.html @@ -6,46 +6,67 @@ From c05460986155e0047074f778fae843714897144b Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Thu, 13 Sep 2018 23:12:28 +0900 Subject: [PATCH 41/47] Fix: (ref #183) scripts> use `"` not to occur error --- scripts/apply_ramdom.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/apply_ramdom.py b/scripts/apply_ramdom.py index 4ae0354..359032c 100755 --- a/scripts/apply_ramdom.py +++ b/scripts/apply_ramdom.py @@ -68,8 +68,8 @@ def login(client, secret_id, rresp): print(f'applying to {lottery["id"]}: ', end='') for user in users: token = login(client, user['secret_id'], '')['token'] - client.post(f'/lotteries/{lottery["id"]}', _json={"group_members":""}, - headers={'Authorization': f'Bearer {token}'}) + client.post(f'/lotteries/{lottery["id"]}', _json={"group_members": ""}, + headers={"Authorization": f"Bearer {token}"}) print('.', end='') print(' DONE') From 7770d1b11eb06b9c9bbdca3c27175efe75eecfbc Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Thu, 13 Sep 2018 23:13:13 +0900 Subject: [PATCH 42/47] Fix: (refs #183) scripts> fix arg for 'login' --- scripts/draw_all.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/draw_all.py b/scripts/draw_all.py index defbda9..a1f532e 100755 --- a/scripts/draw_all.py +++ b/scripts/draw_all.py @@ -62,5 +62,5 @@ def login(client, secret_id, rresp): id_list = load_id_json_file(Path(__file__).parent.parent / Path('cards/test_users.json')) admin = [i for i in id_list if i['authority'] == 'admin'] -token = login(client, admin['secret_id'], admin['g-recaptcha-response'])['token'] +token = login(client, admin["secret_id"], '')["token"] client.post('/draw_all', headers={"Authorization": f'Bearer {token}'}) From 314a2a3940b6840e2cea17e0d28a54086f659ecb Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Thu, 13 Sep 2018 23:14:44 +0900 Subject: [PATCH 43/47] Fix (refs #183) scripts> set 'admin' to one user (not list) --- scripts/draw_all.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/draw_all.py b/scripts/draw_all.py index a1f532e..f6d4fe4 100755 --- a/scripts/draw_all.py +++ b/scripts/draw_all.py @@ -60,7 +60,7 @@ def login(client, secret_id, rresp): id_list = load_id_json_file(Path(__file__).parent.parent / Path('cards/test_users.json')) -admin = [i for i in id_list if i['authority'] == 'admin'] +admin = next(i for i in id_list if i['authority'] == 'admin') token = login(client, admin["secret_id"], '')["token"] client.post('/draw_all', headers={"Authorization": f'Bearer {token}'}) From 77bd76717ca70c002a3777d750eae41d75f27694 Mon Sep 17 00:00:00 2001 From: tamazasa Date: Fri, 14 Sep 2018 18:55:01 +0900 Subject: [PATCH 44/47] Change: improve results html design --- api/routes/api.py | 20 +------ api/templates/results.html | 116 +++++++++++++++---------------------- 2 files changed, 47 insertions(+), 89 deletions(-) diff --git a/api/routes/api.py b/api/routes/api.py index 6650e82..71e4cb8 100644 --- a/api/routes/api.py +++ b/api/routes/api.py @@ -444,31 +444,13 @@ def public_id_generator(lottery, kind): for lottery in lotteries: public_ids = list(public_id_generator(lottery, kind)) cl = Classroom.query.get(lottery.classroom_id) - result = {'classroom': f'{cl.grade}-{cl.get_classroom_name()}', + result = {'classroom': f'{cl.grade}{cl.get_classroom_name()}', 'winners': public_ids} whole_results[kind].append(result) data = {'kinds': [], 'horizontal': 3} for key, value in whole_results.items(): data['kinds'].append({'lotteries': value, 'kind': key}) - # data structure of 'data' - # data = {'kinds': - # [{'lotteries': - # [{'classroom_id': lottery.classroom_id, - # 'winners': [lottery.application.user.public_id(has 'visitor' kind)] - # }], - # 'kind': 'visitor' - # }, - # {'lotteries': - # [{'classroom_id': lottery.classroom_id, - # 'winners': [lottery.application.user.public_id(has 'student' kind)] - # }], - # 'kind': 'student' - # }], - # 'horizontal': 3 - # } - # This is too complicated, so do REFACTORING in the future - # 6. env = Environment(loader=FileSystemLoader('api/templates')) template = env.get_template('results.html') diff --git a/api/templates/results.html b/api/templates/results.html index 1e36690..b487cc3 100644 --- a/api/templates/results.html +++ b/api/templates/results.html @@ -4,101 +4,77 @@ - + {% for kind in kinds %} -
- {% for lottery in kind['lotteries'] %} -
-
- {% set kind = '来場者' if kind['kind'] == 'visitor' else '生徒' %} - {{ kind }} -
-
- class: {{ lottery['classroom'] }} - {% for row in range(lottery['winners']|length // horizontal +1) %} -
-
    -
    - {% for column in range(1,horizontal +1) %} - {% set winners_index = (row * horizontal) + column %} - {% set winner = lottery['winners'][winners_index -1]|default("") %} -
  • - {{winner}} -
  • - {% endfor %} -
    -
-
+ {% for lottery in kind['lotteries'] %} +
+
+

+ + {% set kind = '来場者' if kind['kind'] == 'visitor' else '生徒' %} + {{ kind }} + +

+

+ + {{ lottery['classroom'] }} + +

+ {% for row in range(lottery['winners']|length // horizontal +1) %} +
+
    + {% for column in range(1,horizontal +1) %} + {% set winners_index = (row * horizontal) + column %} + {% set winner = lottery['winners'][winners_index -1]|default("") %} +
  • + {{winner}} +
  • {% endfor %} -
-
+ +
{% endfor %} +
{% endfor %} + {% endfor %} From 337ab5b171e15597b869d819d0cf05832d63e339 Mon Sep 17 00:00:00 2001 From: tamazasa Date: Fri, 14 Sep 2018 19:09:11 +0900 Subject: [PATCH 45/47] Fix: fix jinja2 --- api/templates/results.html | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/api/templates/results.html b/api/templates/results.html index b487cc3..afffe61 100644 --- a/api/templates/results.html +++ b/api/templates/results.html @@ -49,29 +49,25 @@

- - {% set kind = '来場者' if kind['kind'] == 'visitor' else '生徒' %} - {{ kind }} - + + {% set kind = '来場者' if kind['kind'] == 'visitor' else '生徒' %} + {{ kind }} +

- - {{ lottery['classroom'] }} - + + {{ lottery['classroom'] }} +

- {% for row in range(lottery['winners']|length // horizontal +1) %}
    - {% for column in range(1,horizontal +1) %} - {% set winners_index = (row * horizontal) + column %} - {% set winner = lottery['winners'][winners_index -1]|default("") %} + {% for winner in lottery['winners'] %}
  • {{winner}}
  • {% endfor %}
- {% endfor %}
{% endfor %} From f4fcc77e111f4e1ff8a90de5e8432d65a7c86852 Mon Sep 17 00:00:00 2001 From: Cj-bc Date: Fri, 14 Sep 2018 20:22:02 +0900 Subject: [PATCH 46/47] Fix: (#183) scripts> litnJjwq;q --- scripts/apply_ramdom.py | 3 ++- scripts/draw_all.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/apply_ramdom.py b/scripts/apply_ramdom.py index 359032c..d1ab9ff 100755 --- a/scripts/apply_ramdom.py +++ b/scripts/apply_ramdom.py @@ -59,7 +59,8 @@ def login(client, secret_id, rresp): client = client() -id_list = load_id_json_file(Path(__file__).parent.parent/ Path('cards/test_users.json')) +id_list = load_id_json_file(Path(__file__).parent.parent / + Path('cards/test_users.json')) users = [i for i in id_list if i['authority'] == 'normal'] lotteries = client.get('/lotteries/available') diff --git a/scripts/draw_all.py b/scripts/draw_all.py index f6d4fe4..feba6d9 100755 --- a/scripts/draw_all.py +++ b/scripts/draw_all.py @@ -59,7 +59,8 @@ def login(client, secret_id, rresp): client = client() -id_list = load_id_json_file(Path(__file__).parent.parent / Path('cards/test_users.json')) +id_list = load_id_json_file(Path(__file__).parent.parent / + Path('cards/test_users.json')) admin = next(i for i in id_list if i['authority'] == 'admin') token = login(client, admin["secret_id"], '')["token"] From 15875c2a128fce1a9ee1e42e685ffa9c6f1d5061 Mon Sep 17 00:00:00 2001 From: shino Date: Fri, 14 Sep 2018 20:31:13 +0900 Subject: [PATCH 47/47] Fix: reset timepoints --- api/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/config.py b/api/config.py index ec168c0..3e02c2c 100644 --- a/api/config.py +++ b/api/config.py @@ -28,7 +28,7 @@ class BaseConfig(object): (time(8, 50), time(9, 20)), (time(10, 15), time(10, 45)), (time(12, 25), time(12, 55)), - (time(23, 40), time(23, 59)), + (time(13, 50), time(14, 20)) ] ONE_DAY_KIND = ['visitor']