Skip to content

Commit

Permalink
Merge pull request #10 from Kalkuli/develop
Browse files Browse the repository at this point in the history
Solve 125 Criar Release do Projeto
  • Loading branch information
MarianaPicolo authored Oct 2, 2018
2 parents ca8353c + 140110e commit ca1d925
Show file tree
Hide file tree
Showing 18 changed files with 1,483 additions and 43 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__pycache__
env
.vscode/
.vscode/
htmlcov/
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
sudo: required
services:
- docker
env:
DOCKER_COMPOSE_VERSION: 1.21.1
before_install:
- sudo rm /usr/local/bin/docker-compose
- curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose
- chmod +x docker-compose
- sudo mv docker-compose /usr/local/bin
before_script:
- docker-compose -f docker-compose-dev.yml up --build -d
script:
- docker-compose -f docker-compose-dev.yml run base python manage.py test
after_script:
- docker-compose -f docker-compose-dev.yml down
28 changes: 22 additions & 6 deletions Dockerfile-dev
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
# Base Image
FROM python:3.6.5-alpine
FROM python:3.6.5-alpine


# Install dependencies
RUN apk update && \
apk add --virtual build-deps gcc python-dev musl-dev && \
apk add postgresql-dev && \
apk add netcat-openbsd


# Setting working directory
WORKDIR /usr/src/app
WORKDIR /app


# Dealing with requirements
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt
COPY ./requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt


# Coping project
COPY . /usr/src/app
COPY . /app


# Add entrypoint.sh
COPY ./entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh


# Running server
CMD python manage.py run -h 0.0.0.0
CMD ["/app/entrypoint.sh"]
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

56 changes: 47 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,59 @@
# Configurando o ambiente
Para instruções de como instalar o Docker e o Docker-compose clique [aqui](https://github.com/Kalkuli/2018.2-Kalkuli_Front-End/blob/master/README.md).
# Serviço de Apresentação de Notas

<div style="text-align: center">

<a href="[![Build Status](https://travis-ci.com/Kalkuli/2018.2-Kalkuli_Receipts.svg?branch=master)](https://travis-ci.com/Kalkuli/2018.2-Kalkuli_Receipts)"><img src="https://travis-ci.com/Kalkuli/2018.2-Kalkuli_Receipts.svg?branch=master" /></a>
<a href="https://codeclimate.com/github/Kalkuli/2018.2-Kalkuli_Receipts/test_coverage"><img src="https://api.codeclimate.com/v1/badges/320b61421f14848cee61/test_coverage" /></a>
<a href="https://codeclimate.com/github/Kalkuli/2018.2-Kalkuli_Receipts/maintainability"><img src="https://api.codeclimate.com/v1/badges/320b61421f14848cee61/maintainability" /></a>
<a href="https://opensource.org/licenses/GPL-3.0"><img src="https://img.shields.io/badge/license-GPL-%235DA8C1.svg"/></a>

</div>


## Configurando o ambiente

Para instruções de como instalar o _Docker_ e o _Docker-Compose_ clique [aqui](https://github.com/Kalkuli/2018.2-Kalkuli_Front-End/blob/master/README.md).

***

<br>

## Colocando no ar
Com o Docker e Docker-Compose instalados, basta apenas utilizar os comandos:
Com o _Docker_ e _Docker-Compose_ instalados, basta apenas utilizar os comandos:

```
chmod +x entrypoint.sh
docker-compose -f docker-compose-dev.yml build
```docker-compose -f docker-compose-dev.yml build```
docker-compose -f docker-compose-dev.yml up
```

e
Abra outro terminal, e execute o comando:


```
docker-compose -f docker-compose-dev.yml run base python manage.py recreatedb
```

```docker-compose -f docker-compose-dev.yml up```

Acesse o servidor local no endereço apresentado abaixo:

http://localhost:5006/
[localhost:5006](http://localhost:5006/)


Agora você já pode começar a contribuir!


## Testando

Execute o comando abaixo para executar os testes:

```
docker-compose -f docker-compose-dev.yml run base python manage.py test
```

Execute o comando abaixo para checar a cobertura:

Agora você já pode começar a contribuir!
```
docker-compose -f docker-compose-dev.yml run base python manage.py cov
```
21 changes: 18 additions & 3 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
version: '3.6'
services:
users:
base:
build:
context: .
dockerfile: Dockerfile-dev
volumes:
- '.:/usr/src/app'
- '.:/app'
ports:
- 5006:5000
environment:
- FLASK_APP=project/__init__.py
- FLASK_ENV=development
- APP_SETTINGS=project.config.DevelopmentConfig
- APP_SETTINGS=project.config.DevelopmentConfig
- DATABASE_URL=postgres://postgres:postgres@db:5432/receipts_dev
- DATABASE_TEST_URL=postgres://postgres:postgres@db:5432/receipts_test
depends_on:
- db


db:
build:
context: ./project/db
dockerfile: Dockerfile
ports:
- 5106:5432
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
11 changes: 11 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

echo "Waiting for postgres..."

while ! nc -z db 5432; do
sleep 0.1
done

echo "PostgreSQL started"

python manage.py run -h 0.0.0.0
59 changes: 54 additions & 5 deletions manage.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,56 @@
from flask.cli import FlaskGroup
from project import app
import coverage

cli = FlaskGroup(app)
COV = coverage.coverage(
branch=True,
include='project/*',
omit=[
'project/tests/*',
'project/config.py'
]
)
COV.start()

if __name__ == '__main__':
cli()
from flask.cli import FlaskGroup
from project import app, db
from project.api.models import Receipt, Product
import unittest

cli = FlaskGroup(app)


# Create command
@cli.command()
def recreatedb():
db.drop_all()
db.create_all()
db.session.commit()


# Registers comand to run tests
@cli.command()
def test():
tests = unittest.TestLoader().discover('project/tests', pattern='test*.py')
result = unittest.TextTestRunner(verbosity=2).run(tests)
if result.wasSuccessful():
return 0
return 1


@cli.command()
def cov():
"""Runs the unit tests with coverage."""
tests = unittest.TestLoader().discover('project/tests')
result = unittest.TextTestRunner(verbosity=2).run(tests)
if result.wasSuccessful():
COV.stop()
COV.save()
print('Coverage Summary:')
COV.report()
COV.html_report()
COV.erase()
return 0
return 1


if __name__ == '__main__':
cli()
16 changes: 10 additions & 6 deletions project/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import os
from flask import Flask, jsonify
from flask_sqlalchemy import SQLAlchemy


# Instantiate the app
app = Flask(__name__)
app = Flask(__name__)


# Set Configuration
app_settings = os.getenv('APP_SETTINGS')
app.config.from_object(app_settings)

@app.route('/', methods=['GET'])
def ping_pong():
return jsonify({
'data': 'Welcome to Kalkuli Receipts Service'
})

# Instanciate Database
db = SQLAlchemy(app)

from project.api.views import receipts_blueprint
app.register_blueprint(receipts_blueprint)
56 changes: 56 additions & 0 deletions project/api/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from project import db

from sqlalchemy.sql import func


class Receipt(db.Model):
__tablename__ = 'receipt'

id = db.Column(db.Integer, primary_key=True, autoincrement=True)
company_id = db.Column(db.Integer, nullable=False)
emission_date = db.Column(db.DateTime, nullable=False)
emission_place = db.Column(db.String(128), nullable=False)
cnpj = db.Column(db.String, nullable=False)
tax_value = db.Column(db.Float, nullable=False)
total_price = db.Column(db.Float, nullable=False)

def __init__(self, company_id, emission_date, emission_place, cnpj, tax_value, total_price):
self.company_id = company_id
self.emission_date = emission_date
self.emission_place = emission_place
self.cnpj = cnpj
self.tax_value = tax_value
self.total_price = total_price

def to_json(self):
return {
'id': self.id,
'company_id': self.company_id,
'emission_date': self.emission_date.date().isoformat(),
'emission_place': self.emission_place,
'cnpj': self.cnpj,
'tax_value': self.tax_value,
'total_price': self.total_price
}


class Product(db.Model):
__tablename__ = 'product'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
receipt_id = db.Column(db.Integer, db.ForeignKey('receipt.id'), nullable=False)
receipt = db.relationship('Receipt', backref=db.backref('products', lazy=True))
quantity = db.Column(db.Integer, nullable=False)
unit_price = db.Column(db.Float, nullable=False)

def __init__(self, receipt_id, quantity, unit_price):
self.receipt_id = receipt_id
self.quantity = quantity
self.unit_price = unit_price

def to_json(self):
return {
'id':self.id,
'receipt_id':self.receipt_id,
'quantity':self.quantity,
'unit_price':self.unit_price
}
Loading

0 comments on commit ca1d925

Please sign in to comment.