Skip to content

Commit

Permalink
#12 implementado voto
Browse files Browse the repository at this point in the history
  • Loading branch information
alexNeto committed Aug 11, 2019
1 parent 19a7b67 commit 6bd3a56
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 15 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Lada API

Api para consulta e comparação de previsão do tempo.
Related To [lada-app](https://github.com/alexNeto/lada-app)

[![Build Status](https://travis-ci.org/alexNeto/lada-api.svg?branch=master)](https://travis-ci.org/alexNeto/lada-api)
[![Coverage Status](https://coveralls.io/repos/github/alexNeto/lada-api/badge.svg?branch=master)](https://coveralls.io/github/alexNeto/lada-api?branch=master)
[![Maintainability](https://api.codeclimate.com/v1/badges/29914403de1fdead5141/maintainability)](https://codeclimate.com/github/alexNeto/lada-api/maintainability)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/99156cf8d9f74c7faaefaaed6c343d4f)](https://www.codacy.com/app/alexNeto/lada-api?utm_source=github.com&utm_medium=referral&utm_content=alexNeto/lada-api&utm_campaign=Badge_Grade)

Api para consulta e comparação de previsão do tempo.
Related To [lada-app](https://github.com/alexNeto/lada-app)

- [Lada API](#lada-api)
- [Setup](#setup)
Expand Down Expand Up @@ -58,4 +58,4 @@ pip install -r requirements.txt

```bash
python -m flask run
```
```
5 changes: 5 additions & 0 deletions api/end_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from api.source.source_item_controller import SourceItemController
from api.source_for_location.source_for_location_controller import SourceForLocationController
from api.sources.cptec.cptec_today_controller import CPTECTodayController
from api.vote.vote_controller import VoteController


class EndPoints:
Expand All @@ -12,9 +13,13 @@ def __init__(self, app):
self.__api = Api(app)

def add_resources(self):
# GENERAL
# TODO - home com informações da api
# SOURCE
self.__api.add_resource(SourceController, '/source')
self.__api.add_resource(SourceItemController, '/source/<source_name>')
self.__api.add_resource(SourceForLocationController, '/source/list-available/<country>')
# VOTES
self.__api.add_resource(VoteController, '/vote_of/<location>/source/<source>')
# CPTEC
self.__api.add_resource(CPTECTodayController, '/CPTEC/today/<region>/<city>')
15 changes: 13 additions & 2 deletions api/services/string_services.py
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))
16 changes: 8 additions & 8 deletions api/source/source_data_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@


class SourceDataService:
@staticmethod
def create_source(source: Source):
@classmethod
def create_source(cls, source: Source):
source.save()
return source

@staticmethod
def find_all():
@classmethod
def find_all(cls):
return Source.objects()

@staticmethod
def find_by_source_name(source_name: str):
@classmethod
def find_by_source_name(cls, source_name: str):
return Source.objects().filter(source_name=source_name).first()

@staticmethod
def find_by_country(country: str):
@classmethod
def find_by_country(cls, country: str):
result = Source.objects(country_available__in=['*', country.upper()])
return result if result is not None else Source()
5 changes: 3 additions & 2 deletions api/storage/vote.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

class Vote(mongoengine.Document):
location = mongoengine.StringField()
up_vote = mongoengine.FloatField()
down_vote = mongoengine.FloatField()
source_name = mongoengine.StringField()
up_vote = mongoengine.IntField()
down_vote = mongoengine.IntField()
update_at = mongoengine.DateTimeField(default=datetime.datetime.now)

meta = {
Expand Down
Empty file added api/vote/__init__.py
Empty file.
20 changes: 20 additions & 0 deletions api/vote/vote_controller.py
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()))
36 changes: 36 additions & 0 deletions api/vote/vote_model.py
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
36 changes: 36 additions & 0 deletions test/services/stringServiceTest.py
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()

0 comments on commit 6bd3a56

Please sign in to comment.