Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Commit

Permalink
Adicionado serialização de datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
viniciusfk9 committed Oct 9, 2018
1 parent aff3e49 commit f79f5e0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
'python-dotenv',
'flasgger',
'dynaconf',
"awesome-slugify"
"awesome-slugify",
"tinydb-serialization"
]

requirements_extra = {
Expand Down
3 changes: 2 additions & 1 deletion talkshow/blueprints/restapi/resource.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from flask import current_app as app
from flask_restful import reqparse, Resource
from flask_simplelogin import login_required
Expand Down Expand Up @@ -45,7 +46,7 @@ def post(self):
new = app.db['events'].insert({
'name': event.name,
'slug': slug,
'date': event.date})
'date': datetime.strptime(event.date, '%Y-%m-%d')})
return {'event_created': new.inserted_id}, 201


Expand Down
5 changes: 3 additions & 2 deletions talkshow/blueprints/webui/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import datetime
from datetime import datetime
from flask import current_app as app, render_template, abort, request
from .forms import ProposalForm

Expand All @@ -21,7 +21,8 @@ def event(slug):
# Se estamos no meio de um submit válido preparamos os dados
proposal = form.data.copy()
proposal['event_id'] = event['_id']
proposal['date'] = datetime.datetime.today().date().isoformat()
# proposal['date'] = datetime.datetime.today().date().isoformat()
proposal['date'] = datetime.today()
proposal['approved'] = False
# e gravamos no banco de dados
app.db['proposal'].insert_one(proposal)
Expand Down
3 changes: 2 additions & 1 deletion talkshow/ext/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import click
from datetime import datetime
from talkshow.ext.login import create_user
from slugify import slugify

Expand All @@ -19,7 +20,7 @@ def addevent(name, date):
event = app.db['events'].insert_one({
'name': name,
'slug': slug,
'date': date})
'date': datetime.strptime(date, '%Y-%m-%d')})
click.echo(f"{event.inserted_id} cadastrado com sucesso!")

@app.cli.command()
Expand Down
14 changes: 13 additions & 1 deletion talkshow/ext/db.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
from pymongo import MongoClient
from tinymongo import TinyMongoClient
from tinymongo.serializers import DateTimeSerializer
from tinydb_serialization import SerializationMiddleware


class CustomClient(TinyMongoClient):
@property
def _storage(self):
serialization = SerializationMiddleware()
serialization.register_serializer(DateTimeSerializer(), 'TinyDate')

# register other custom serializers
return serialization


def configure(app):
Expand All @@ -8,7 +20,7 @@ def configure(app):
if app.env == "production":
client = MongoClient(host="localhost")
else:
client = TinyMongoClient('database')
client = CustomClient('database')

db_name = app.config.get('MONGODB_NAME', 'db')
app.db = client[db_name]

0 comments on commit f79f5e0

Please sign in to comment.