Skip to content

Commit

Permalink
Merge 1cece40 into b01ac48
Browse files Browse the repository at this point in the history
  • Loading branch information
David-Esteves committed Aug 21, 2019
2 parents b01ac48 + 1cece40 commit 048ec1b
Show file tree
Hide file tree
Showing 28 changed files with 301 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ omit =
manage.py,
setup.py,
.tox/*,
site_packages/*
site_packages/*
project_template/*
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

Always reference the ticket number at the end of the issue description.

## Unreleased

### Added
Adds a createapp command to arctic #387

## 1.4.1 (2019-08-01)

### Added
Expand Down
107 changes: 104 additions & 3 deletions arctic/bin/arctic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@
from django.core.management import ManagementUtility


class bcolors:
"""
ANSI escape sequences for terminal colors
"""
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'


def create_project(parser, options, args):
# Validate args
if len(args) < 2:
Expand Down Expand Up @@ -42,7 +56,7 @@ def create_project(parser, options, args):
import arctic

arctic_path = os.path.dirname(arctic.__file__)
template_path = os.path.join(arctic_path, "project_template")
template_path = os.path.join(arctic_path, "project_template/start")

# Call django-admin startproject
utility_args = [
Expand Down Expand Up @@ -78,12 +92,99 @@ def create_project(parser, options, args):
)


COMMANDS = {"start": create_project}
def create_app(parser, options, args):
# Validate args
if len(args) < 2:
parser.error("Please specify a name for your app")
elif len(args) > 3:
parser.error("Too many arguments")

app_name = args[1].lower()

try:
dest_dir = args[2]
except IndexError:
dest_dir = ""

# Make sure given name is not already in use by another
# python package/module.
try:
__import__(app_name)
except ImportError:
pass
else:
parser.error(
'"{}" conflicts with the name of an existing '
"Python module and cannot be used as an app "
"name. Please try another name.".format(app_name)
)

print((
bcolors.HEADER
+ "Creating an App named {}"
+ bcolors.ENDC + "\n").format(app_name))

# First find the path to Arctic
import arctic

arctic_path = os.path.dirname(arctic.__file__)
template_path = os.path.join(arctic_path, "project_template/app")

# Call django-admin starrtapp
utility_args = [
"django-admin.py",
"startapp",
"--template=" + template_path,
app_name,
]

if dest_dir:
utility_args.append(dest_dir)

utility = ManagementUtility(utility_args)
utility.execute()

print((
"Congratulations! {0} folder has been created it contains the "
"following structure.\n\n"
+ bcolors.OKBLUE
+ " -{0}\n"
" ---__init__.py\n"
" ---apps.py\n"
" ---forms.py\n"
" ---models.py\n"
" ---urls.py\n"
" ---views.py\n\n"
+ bcolors.ENDC
+ "The next steps are:\n\n"
" Add the app name to " + bcolors.UNDERLINE + "INSTALLED_APPS" + bcolors.ENDC + " in the settings.py\n" # NOQA
+ bcolors.OKGREEN + "\"{0}\"," + bcolors.ENDC + "\n"
" Add the app name and path to " + bcolors.UNDERLINE + "ARCTIC_MENU" + bcolors.ENDC + " in the settings.py\n" # NOQA
+ bcolors.OKGREEN + "(\"{1}\", \"{0}:list\", \"fa-folder\")," + bcolors.ENDC + "\n" # NOQA
" Add the urls to config/urls.py.\n"
+ bcolors.OKGREEN + "url(r\"^{0}/\", include(\"{0}.urls\", \"{0}\"))," + bcolors.ENDC + "\n" # NOQA
" Add fields in the models.py file\n"
"- Run " + bcolors.OKGREEN + "./manage.py makemigrations {0}" + bcolors.ENDC + "\n" # NOQA
"- Run " + bcolors.OKGREEN + "./manage.py migrate" + bcolors.ENDC + "\n\n" # NOQA
"The " + bcolors.BOLD + "forms.py" + bcolors.ENDC + " has a form with all the fields in the model and \n" # NOQA
"the " + bcolors.BOLD + "views.py" + bcolors.ENDC + " contains views for list, create, edit and delete. \n" # NOQA
"All of then can be tweaked to better satisfy the needs of the "
"project/app\n").format(
app_name, app_name.capitalize()
)
)


COMMANDS = {
"start": create_project,
"createapp": create_app,
}


def main():
# Parse options
parser = OptionParser(usage="Usage: arctic start project_name [directory]")
parser = OptionParser(usage="Usage: arctic start project_name [directory]"
"Usage: arctic createapp appname [directory]")
(options, args) = parser.parse_args()

# Find command
Expand Down
4 changes: 4 additions & 0 deletions arctic/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,10 @@ def get_fields(self, strip_labels=False):
"""
Hook to dynamically change the fields that will be displayed
"""
if self.fields == '__all__':
return [
f.name for f in self.model._meta.get_fields()
]
if strip_labels:
return [
f[0] if type(f) in (tuple, list) else f for f in self.fields
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions arctic/project_template/app/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class {{ camel_case_app_name }}Config(AppConfig):
name = "{{ app_name }}"
9 changes: 9 additions & 0 deletions arctic/project_template/app/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django import forms

from .models import {{ camel_case_app_name }}


class {{ camel_case_app_name }}Form(forms.ModelForm):
class Meta:
fields = '__all__'
model = {{ camel_case_app_name }}
11 changes: 11 additions & 0 deletions arctic/project_template/app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*-*- encoding: utf-8 -*-*-

from django.db import models


class {{ camel_case_app_name }}(models.Model):
# Add DB fields

# Optional add __str__
# def __str__(self):
# return self.name
1 change: 1 addition & 0 deletions arctic/project_template/app/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your tests here.
11 changes: 11 additions & 0 deletions arctic/project_template/app/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.conf.urls import url

from . import views

app_name = "{{ app_name }}"

urlpatterns = [url(r"^$", views.{{ camel_case_app_name }}ListView.as_view(), name="list"),
url(r"^create/$", views.{{ camel_case_app_name }}CreateView.as_view(), name="create"),
url(r"^(?P<pk>\d+)/$", views.{{ camel_case_app_name }}UpdateView.as_view(), name="detail"),
url(r"^(?P<pk>\d+)/delete/$", views.{{ camel_case_app_name }}DeleteView.as_view(), name="delete"),
]
61 changes: 61 additions & 0 deletions arctic/project_template/app/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from django.urls import reverse, reverse_lazy
from django.utils.translation import ugettext_lazy as _

from arctic.generics import (
CreateView,
DeleteView,
ListView,
UpdateView,
)

from .forms import {{ camel_case_app_name }}Form
from .models import {{ camel_case_app_name }}


class {{ camel_case_app_name }}ListView(ListView):
model = {{ camel_case_app_name }}
fields = '__all__'
permission_required = 'view_{{ app_name }}'

# Delete and detail action link
action_links = [
("detail", "{{ app_name}}:detail", "fa-edit"),
("delete", "{{ app_name}}:delete", "fa-trash"),
]

# tool link to create
tool_links = [
(_("Create {{ app_name }}"), "{{ app_name }}:create", "fa-plus"),
]

# Some optional fields
# paginate_by = 10
# ordering_fields = ['field_name1', ..]
# search_fields = ['field_name', ...]
# allowed_exports = ["csv"]


class {{ camel_case_app_name }}CreateView(CreateView):
model = {{ camel_case_app_name }}
form_class = {{ camel_case_app_name }}Form
permission_required = 'add_{{ app_name }}'

def get_success_url(self):
return reverse("{{app_name}}:detail", args=(self.object.pk,))


class {{ camel_case_app_name }}UpdateView(UpdateView):
model = {{ camel_case_app_name }}
form_class = {{ camel_case_app_name }}Form
permission_required = 'change_{{ app_name }}'
success_url = reverse_lazy('{{app_name}}:list')
actions = [
(_("Cancel"), "cancel"),
(_("Save"), "submit"),
]


class {{ camel_case_app_name }}DeleteView(DeleteView):
model = {{ camel_case_app_name }}
success_url = reverse_lazy('{{app_name}}:list')
permission_required = 'delete_{{ app_name }}'
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""
Django settings for {{ project_name }} project.
Generated by 'arctic start' command based on Django 1.11.3.
Generated by 'arctic start' command based on Django 2.2
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
https://docs.djangoproject.com/en/2.2/ref/settings/
"""

import os
Expand All @@ -16,7 +16,7 @@
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "(nb89xnt7&ez3%6c4djx+e^!wi+d=h6qyq(14k7x_ipcmr_464"
Expand All @@ -34,6 +34,7 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"config",
"arctic",
"dashboard",
]
Expand Down Expand Up @@ -69,7 +70,7 @@
WSGI_APPLICATION = "config.wsgi.application"

# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

DATABASES = {
"default": {
Expand All @@ -79,23 +80,25 @@
}

# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator" # NOQA
},
{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator"},
{
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator" # NOQA
},
{
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator" # NOQA
},
{
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator" # NOQA
},
]

# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/
# https://docs.djangoproject.com/en/2.2/topics/i18n/

LANGUAGE_CODE = "en-us"

Expand All @@ -108,7 +111,7 @@
USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
# https://docs.djangoproject.com/en/2.2/howto/static-files/

STATIC_URL = "/static/"

Expand All @@ -118,4 +121,6 @@
# Arctic configuration
ARCTIC_SITE_NAME = "{{ project_name }}'s Arctic website"

ARCTIC_MENU = (("Dashboard", "index", "fa-dashboard"),)
ARCTIC_MENU = (
("Dashboard", "index", "fa-dashboard"),
)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 048ec1b

Please sign in to comment.