-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
124 additions
and
15 deletions.
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 |
---|---|---|
@@ -1,5 +1,16 @@ | ||
import unidecode | ||
|
||
|
||
def normalize(string_to_normalize) -> str: | ||
return unidecode.unidecode(string_to_normalize) if isinstance(string_to_normalize, str) else "" | ||
def normalize(string_to_normalize: str) -> str: | ||
if isinstance(string_to_normalize, str): | ||
return unidecode.unidecode(string_to_normalize) | ||
else: | ||
return "" | ||
|
||
|
||
def dash(to_dash: str) -> str: | ||
return normalize(to_dash).replace(' ', '-').lower() | ||
|
||
|
||
def to_key(region: str, city: str) -> str: | ||
return "{0}:{1}".format(dash(region), dash(city)) |
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
Empty file.
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,20 @@ | ||
import json | ||
|
||
from flask_restful import Resource, reqparse | ||
|
||
from api.services.string_services import dash | ||
from api.vote.vote_model import VoteModel | ||
|
||
|
||
class VoteController(Resource): | ||
def __init__(self): | ||
self.__model = VoteModel() | ||
self.__parser = reqparse.RequestParser() | ||
self.__parser.add_argument('up_vote', type=bool) | ||
self.__parser.add_argument('down_vote', type=bool) | ||
|
||
def get(self, source, location): | ||
return json.loads(self.__model.get_vote_for_source(source, location)) | ||
|
||
def put(self, source, location): | ||
return json.loads(self.__model.update_vote_for_source(source, location, self.__parser.parse_args())) |
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,36 @@ | ||
from api.services.string_services import dash | ||
from api.source.source_data_service import SourceDataService | ||
from api.storage.vote import Vote | ||
|
||
|
||
class VoteModel: | ||
|
||
def __init__(self): | ||
self.__source_data_service = SourceDataService() | ||
|
||
def get_vote_for_source(self, source: str, location: str): | ||
return self.__get_vote_or_new(source, location).to_json() | ||
|
||
def update_vote_for_source(self, source, location, request_body): | ||
vote = self.__get_vote_or_new(source.upper(), location) | ||
if request_body['up_vote']: | ||
vote.up_vote = vote.up_vote + 1 | ||
if request_body['down_vote']: | ||
vote.down_vote = vote.down_vote + 1 | ||
vote.save() | ||
return vote.to_json() | ||
|
||
def __get_vote_or_new(self, source: str, location: str) -> Vote: | ||
parsed_location = dash(location) | ||
vote = Vote.objects().filter(source_name=source.upper(), location=parsed_location).first() | ||
return vote if vote is not None else self.__create_new_vote(source, parsed_location) | ||
|
||
@classmethod | ||
def __create_new_vote(cls, source: str, location: str) -> Vote: | ||
vote = Vote() | ||
vote.source_name = source | ||
vote.location = location | ||
vote.up_vote = 0 | ||
vote.down_vote = 0 | ||
vote.save() | ||
return vote |
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,36 @@ | ||
import unittest | ||
|
||
from api.services.string_services import normalize, dash, to_key | ||
|
||
|
||
class StringServiceTest(unittest.TestCase): | ||
def test_normalize(self): | ||
self.assertEqual('Sao Paulo', normalize('São Paulo')) | ||
|
||
def test_normalize_None(self): | ||
self.assertEqual('', normalize(None)) | ||
|
||
def test_normalize_empty_string(self): | ||
self.assertEqual('', normalize('')) | ||
|
||
def test_dash(self): | ||
self.assertEqual('sao-paulo:sao-jose-dos-campos', dash('São Paulo:São José dos Campos')) | ||
|
||
def test_dash_None(self): | ||
self.assertEqual('', dash(None)) | ||
|
||
def test_dash_empty_string(self): | ||
self.assertEqual('', dash('')) | ||
|
||
def test_to_key(self): | ||
self.assertEqual('sao-paulo:sao-jose-dos-campos', to_key('São Paulo', 'São José dos Campos')) | ||
|
||
def test_to_key_None(self): | ||
self.assertEqual(':', to_key(None, None)) | ||
|
||
def test_to_key_empty_string(self): | ||
self.assertEqual(':', to_key('', '')) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |