Coder Social home page Coder Social logo

django_common's Introduction

django_common

Create this repository to maintain the most used common functions in a module, for convinient reason

This module include the following common function features:

  1. Customized Django-related utils
  2. Customized Tastypie-related utils
  3. Customized Testing-related utils
  4. Project scope constants
  5. Project scope model choices
  6. Customized Fabric-related utils

dependencies

  • Django
  • tastypie
  • Fabric / requries
  • factory
  • faker

Fabric usage case

"""
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

django field's choices base usage demo

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)
    ...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.