-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #381 from sot528/ALIS-4119
add QV
- Loading branch information
Showing
6 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
import boto3 | ||
from quadratic_voting_create import LaboNQuadraticVotingCreate | ||
|
||
dynamodb = boto3.resource('dynamodb') | ||
|
||
|
||
def lambda_handler(event, context): | ||
target = LaboNQuadraticVotingCreate(event, context, dynamodb) | ||
return target.main() |
74 changes: 74 additions & 0 deletions
74
src/handlers/labo/n/quadratic_voting/create/quadratic_voting_create.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# -*- coding: utf-8 -*- | ||
import os | ||
import time | ||
import math | ||
from lambda_base import LambdaBase | ||
from jsonschema import validate, ValidationError | ||
|
||
options_count = 6 # 選択肢の数 | ||
credit_per_user = 100 # ユーザに付与されるクレジット(持ち越しは考慮しない) | ||
maximum = math.sqrt(credit_per_user) # 許容される最大の値 | ||
|
||
|
||
class LaboNQuadraticVotingCreate(LambdaBase): | ||
def get_schema(self): | ||
opt = { | ||
"type": "integer", | ||
"minimum": 0, | ||
"maximum": maximum | ||
} | ||
|
||
required = [] | ||
properties = {} | ||
for i in range(options_count): | ||
key = 'opt_' + str(i + 1) | ||
properties[key] = opt | ||
required.append(key) | ||
|
||
for i in range(options_count): | ||
properties['opt_' + str(i + 1)] = opt | ||
|
||
return { | ||
"type": "object", | ||
"properties": properties, | ||
"required": required | ||
} | ||
|
||
def validate_params(self): | ||
validate(self.params, self.get_schema()) | ||
|
||
totalVotedValue = 0 | ||
for key in self.params: | ||
totalVotedValue += self.params[key] ** 2 | ||
|
||
# 投票の合計がCreditの限界を超えている場合 | ||
if totalVotedValue > credit_per_user: | ||
raise ValidationError('Invalid') | ||
|
||
def exec_main_proc(self): | ||
table = self.dynamodb.Table(os.environ['QUADRATIC_VOTING_TABLE_NAME']) | ||
|
||
user_id = self.event['requestContext']['authorizer']['claims']['cognito:username'] | ||
if not LaboNQuadraticVotingCreate.__is_exists(table, user_id): | ||
item = { | ||
'user_id': user_id, | ||
'created_at': int(time.time()) | ||
} | ||
|
||
for key, value in self.params.items(): | ||
item[key] = value | ||
|
||
table.put_item( | ||
Item=item, | ||
ConditionExpression='attribute_not_exists(user_id)' | ||
) | ||
|
||
return { | ||
'statusCode': 200 | ||
} | ||
|
||
@staticmethod | ||
def __is_exists(table, user_id): | ||
result = table.get_item(Key={'user_id': user_id}).get('Item') | ||
|
||
return False if result is None else True |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
import boto3 | ||
from quadratic_voting_index import LaboNQuadraticVotingIndex | ||
|
||
dynamodb = boto3.resource('dynamodb') | ||
|
||
|
||
def lambda_handler(event, context): | ||
target = LaboNQuadraticVotingIndex(event, context, dynamodb) | ||
return target.main() |
29 changes: 29 additions & 0 deletions
29
src/handlers/labo/n/quadratic_voting/index/quadratic_voting_index.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# -*- coding: utf-8 -*- | ||
import os | ||
import json | ||
from lambda_base import LambdaBase | ||
|
||
|
||
class LaboNQuadraticVotingIndex(LambdaBase): | ||
def get_schema(self): | ||
pass | ||
|
||
def validate_params(self): | ||
pass | ||
|
||
def exec_main_proc(self): | ||
table = self.dynamodb.Table(os.environ['QUADRATIC_VOTING_TABLE_NAME']) | ||
|
||
user_id = self.event['requestContext']['authorizer']['claims']['cognito:username'] | ||
exists = LaboNQuadraticVotingIndex.__is_exists(table, user_id) | ||
|
||
return { | ||
'statusCode': 200, | ||
'body': json.dumps({'exists': exists}) | ||
} | ||
|
||
@staticmethod | ||
def __is_exists(table, user_id): | ||
result = table.get_item(Key={'user_id': user_id}).get('Item') | ||
|
||
return False if result is None else True |