Skip to content

Instantly share code, notes, and snippets.

@adrianomargarin
Last active August 18, 2018 12:28
Show Gist options
  • Save adrianomargarin/906892ee4a07a32e0e4bf95d40fa51f8 to your computer and use it in GitHub Desktop.
Save adrianomargarin/906892ee4a07a32e0e4bf95d40fa51f8 to your computer and use it in GitHub Desktop.
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# dotenv
.env
# virtualenv
.venv
venv/
ENV/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
db.sqlite3

<README.md>

Como desenvolver?

  1. Clone o repositório.
  2. Crie um virtualenv com Python 3.6
  3. Ative o virtualenv.
  4. Instale as dependências.
  5. Configure a instância com o .env
  6. Execute os testes.
  7. Execute o runserver.
git clone <project>
cd <project>
virtualenv env --python=python3 # python 3.6 ou mais atual
. env/bin/activate
pip install -r requirements_dev.txt
cp contrib/env-sample .env
coverage run --source='.' manage.py test --verbosity=1 && coverage report --show-missing
python manage.py runserver

Como fazer o deploy?

  1. Crie uma instância no heroku.
  2. Envie as configurações para o heroku.
  3. Define um SECRET_KEY segura para instância.
  4. Defina DEBUG=True
  5. Configure o serviço de email.
  6. Envie o código para o heroku.
heroku create <project>

heroku config:set ALLOWED_HOSTS=.herokuapp.com
heroku config:set SECRET_KEY=`python contrib/secret_gen.py`
heroku config:set DEBUG=TRUE

git push heroku master --force
DEBUG=True
SECRET_KEY=THIS_IS_NOT_A_GOOD_SECRET
ALLOWED_HOSTS=127.0.0.1, .localhost, .herokuapp.com
#!/usr/bin/env python
"""
Django SECRET_KEY generator.
"""
from django.utils.crypto import get_random_string
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
print(get_random_string(50, chars))
"""This module contains the Manager class."""
from django.db import models
class Manager(models.Manager):
"""Custom Manager of the Django ORM.
This Manager implements logic exclusion. A record is considered excluded
when the deleted field is filled in.
"""
def get_queryset(self, *args, **kwargs):
"""Return a Queryset.
Return just records where deleted field is null (not excluded).
"""
return super().get_queryset().filter(deleted_at__isnull=True)
from django.db import models
from django.utils import timezone
from <project>.core.signals import post_soft_delete
from <project>.core.manager import Manager
class AbstractBaseModel(models.Model):
"""Abstrate base model used by the apps of the MyreksAPI project.
This model allows the implementation of logical exclusion.
"""
class Meta:
abstract = True
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
deleted_at = models.DateTimeField(null=True, blank=True)
objects = Manager()
def delete(self):
self.deleted_at = timezone.now()
self.save()
post_soft_delete.send(sender=type(self), instance=self, using=self._state.db)
release: python manage.py migrate --noinput
web: gunicorn <project>.wsgi --log-file -
Django
dj-database-url
dj-static
python-decouple
static3
gunicorn
psycopg2
psycopg2-binary
pytz
python-3.6.6
from decouple import Csv
from decouple import config
from django.urls import reverse_lazy
from dj_database_url import parse as dburl
SECRET_KEY = config('SECRET_KEY')
DEBUG = config('DEBUG', default=False, cast=bool)
ALLOWED_HOSTS = config('ALLOWED_HOSTS', default=[], cast=Csv())
default_dburl = 'sqlite:///' + os.path.join(BASE_DIR, 'db.sqlite3')
DATABASES = {
'default': config('DATABASE_URL', default=default_dburl, cast=dburl),
}
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
"""Module with custom signals of the MyreksAPI project."""
from django import dispatch
post_soft_delete = dispatch.Signal(providing_args=['instance', 'using'])
"""
WSGI config for tchelinux project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/
"""
import os
from dj_static import Cling
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<project>.settings')
application = Cling(get_wsgi_application())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment