Create this repository to maintain the most used common functions in a module, for convinient reason
This module include the following common function features:
- Customized Django-related utils
- Customized Tastypie-related utils
- Customized Testing-related utils
- Project scope constants
- Project scope model choices
- Customized Fabric-related utils
dependencies
- Django
- tastypie
- Fabric / requries
- factory
- faker
"""
A demo for the fabrics tool
"""
from fabric.api import (env, task, hosts)
from fabrics import HostConfig, DebRequirement, PyRequirement
# Remote host general configurations
HOST_CONFIG = [
('[email protected]:22', 'vagrant', 'dev', 'dev') #hostring, password, name, role
]
# Remote host OS dependencies configurations
OS_REQUIREMENTS = (
'nginx',
'git',
'python-dev',
'python3-dev'
)
# Remote host Python environment configurations
PY_REQUIREMENTS = (
'Django==1.10',
'requests==2.8.0'
)
host_config = HostConfig(host_config=HOST_CONFIG)
host_config.setup_fabric_env(env)
deb = DebRequirement(requirements=OS_REQUIREMENTS)
py = PyRequirement(requirements=PY_REQUIREMENTS)
def setUp():
"""
Initiate the remove host environment
"""
deb.install_requirements()
py.install_requirements()
def reset():
"""
Initiate the remove host environment
"""
deb.uninstall_requirements()
py.uninstall_requirements()
############################################################
# setUp remote hosts
############################################################
@task
@hosts(host_config.DEV_HOSTS)
def setup_dev():
setUp()
@task
@hosts(host_config.ALL_HOSTS)
def setup_all():
setUp()
############################################################
# Reset remote hosts
############################################################
@task
@hosts(host_config.DEV_HOSTS)
def reset_dev():
reset()
@task
@hosts(host_config.ALL_HOSTS)
def reset_all():
reset()
Then in terminal, we can run the following commands:
fab setup_all # init the environment for all hosts
fab reset_all # clean the environment for all hosts
from common.base_choices import (ChoiceItem, BaseChoice)
class HttpMethodChoice(BaseChoice):
"""
General API request method choices
"""
GET = ChoiceItem(1, 'HTTP GET')
POST = ChoiceItem(2, 'HTTP POST')
PUT = ChoiceItem(3, 'HTTP PUT')
PATCH = ChoiceItem(4, 'HTTP PATCH')
DELETE = ChoiceItem(5, 'HTTP DELETE')
############################################################
# In django model definitions
############################################################
from django.db import models
from common.models import BaseModel
class APIInfo(BaseModel):
method = models.IntegerField(verbose_name='Request Method', choices=HttpMethodChoice.choices, default=HttpMethodChoice.GET)
...