Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions build/lib/django_mako_plus/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#
# Author: Conan Albrecht <ca&byu,edu>
# License: Apache Open Source License
#


# pointer to our app config
# Django looks for this exact variable name


# the version
from .version import __version__


# the router, middleware, and view function decorator
from .middleware import RequestInitMiddleware
from .router import view_function, app_resolver, dmp_path


# converter decorator
from .converter import parameter_converter, ParameterConverter


# the middleware and template
# the template engine
from .engine import MakoTemplates


# the exceptions
from .exceptions import BaseRedirectException
from .exceptions import RedirectException
from .exceptions import PermanentRedirectException
from .exceptions import JavascriptRedirectException
from .exceptions import InternalRedirectException
from .exceptions import ConverterHttp404
from .exceptions import ConverterException


# filters and tags
from .filters import django_syntax, jinja2_syntax, alternate_syntax
from .templatetags.django_mako_plus import dmp_include

# used internally in compiled templates for autoescaping
# (needs to be exposed publicly so templates can see it)
from .template import ExpressionPostProcessor

# the http responses
from .http import HttpResponseJavascriptRedirect


# the convenience functions
#
# Instead of these functions, consider using request.dmp.render() and request.dmp.render_to_string(),
# which are monkey-patched onto every DMP-enabled app at load time. See the documentation
# for information on why we do this.
#
from .convenience import render_template
from .convenience import render_template_for_path
from .convenience import get_template
from .convenience import get_template_for_path
from .convenience import get_template_loader
from .convenience import get_template_loader_for_path


# the utilities
from .util import merge_dicts


# the urls
# I'm specifically not including urls.py here because I want it imported
# as late as possible (after all the apps are set up). Django will import it
# when it processes the project's urls.py file.


# html content shortcuts
from .provider import links
from .provider import template_links, template_obj_links
# html content providers
from .provider.base import BaseProvider
from .provider.compile import CompileProvider, CompileScssProvider, CompileLessProvider
from .provider.context import JsContextProvider, jscontext
from .provider.link import LinkProvider, CssLinkProvider, JsLinkProvider
from .provider.webpack import WebpackJsLinkProvider
51 changes: 51 additions & 0 deletions build/lib/django_mako_plus/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python3
from django.core import management
import os.path
import sys
import functools

__doc__ = '''
Starts a new DMP-style project. This is the DMP equivalent of "django-admin.py startproject".

Example:

django_mako_plus dmp_startproject [project name]

if the above doesn't work, try:

python -m django_mako_plus dmp_startproject [project name]

Background: The dmp_startproject command has a bit of a chicken-and-egg problem.
It creates the project, but the command isn't available until the project is created
and DMP is in INSTALLED_APPS. Can't create until it is created...
Django solves this with django-admin.py, a global Python script that can be
executed directly from the command line.
'''

DMP_MANAGEMENT_PATH = os.path.join(os.path.dirname(__file__), 'management')



def main():
# Django is hard coded to return only its own commands when a project doesn't
# exist yet. Since I want DMP to be able to create projects, I'm monkey-patching
# Django's get_commands() function so DMP gets added. This is the least-offensive
# way I could see to do this since Django really isn't built to allow other commands
# pre-project. This only happens when `django_mako_plus` is run directly and not
# when `manage.py` or `django-admin.py` are run.
orig_get_commands = management.get_commands
@functools.lru_cache(maxsize=None)
def new_get_commands():
commands = {}
commands.update(orig_get_commands())
commands.update({ name: 'django_mako_plus' for name in management.find_commands(DMP_MANAGEMENT_PATH) })
return commands
management.get_commands = new_get_commands

# mimic the code in django-admin.py
management.execute_from_command_line()


## runner!
if __name__ == '__main__':
main()
Empty file.
3 changes: 3 additions & 0 deletions build/lib/django_mako_plus/app_template/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions build/lib/django_mako_plus/app_template/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{ unicode_literals }}from django.apps import AppConfig


class {{ camel_case_app_name }}Config(AppConfig):
name = '{{ app_name }}'
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
3 changes: 3 additions & 0 deletions build/lib/django_mako_plus/app_template/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{ unicode_literals }}from django.db import models

# Create your models here.
6 changes: 6 additions & 0 deletions build/lib/django_mako_plus/app_template/scripts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(function(context) {

// utc_epoch comes from index.py
console.log('Current epoch in UTC is ' + context.utc_epoch);

})(DMP_CONTEXT.get());
46 changes: 46 additions & 0 deletions build/lib/django_mako_plus/app_template/styles/base.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
html, body {
margin: 0;
padding: 0;
font-family: sans-serif;
color: #777788;
}

.clearfix {
clear: both;
}

header {
padding: 50px 20px;
text-align: center;
}

header > .title {
display: inline-block;
color: #3771A1;
font-size: 40px;
font-weight: bold;
text-shadow: 2px 2px 3px rgba(0, 0, 0, 0.2);
vertical-align: middle;
}

header img {
vertical-align: middle;
margin-right: 24px;
}

main {
margin: 0;
padding: 15px;
}

footer {
margin-top: 40px;
border-top: 1px solid #CCCCCC;
text-align: right;
}

footer a {
display: inline-block;
color: #777788;
margin: 15px 10% 0 0;
}
7 changes: 7 additions & 0 deletions build/lib/django_mako_plus/app_template/styles/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
main {
text-align: center;
}

h4.utc-time {
color: #3771A1;
}
36 changes: 36 additions & 0 deletions build/lib/django_mako_plus/app_template/templates/base.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## this is the skeleton of all pages on in this app - it defines the basic html tags
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<head>

<title>DMP</title>

## add any site-wide scripts or CSS here; for example, jquery:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

## render the static file links with the same name as this template
<script src="/django_mako_plus/dmp-common.min.js"></script>
${ django_mako_plus.links(self) }

</head>
<body>

<header>
<img src="${ STATIC_URL }homepage/media/python.png" alt="python" />
<div class="title">Welcome to <br/> DMP!</div>
</header>

<main>
<%block name="content">
Site content goes here in sub-templates.
</%block>
</main>

<footer>
<div><a href="http://doconix.github.io/django-mako-plus/">Online Documentation</a></div>
<div><a href="https://github.com/doconix/django-mako-plus/">DMP on GitHub</a></div>
</footer>

</body>
</html>
11 changes: 11 additions & 0 deletions build/lib/django_mako_plus/app_template/templates/base_ajax.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## this is the skeleton of all *ajax* pages on our site - page snippets that are retrieved with Ajax.
## it's primary function is to insert the CSS and JS for the ajax file template inheritance

## render the static file links with the same name as this template
${ django_mako_plus.links(self) }

## render the ajax content
<%block name="content">
Sub-templates should place their ajax content here.
</%block>

8 changes: 8 additions & 0 deletions build/lib/django_mako_plus/app_template/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<%inherit file="base.htm" />

<%block name="content">
<div class="content">
<h3>Congratulations -- you've successfully created a new DMP app!</h3>
<h4 class="utc-time">Current time in UTC: ${ utc_time }</h4>
</div>
</%block>
3 changes: 3 additions & 0 deletions build/lib/django_mako_plus/app_template/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
Empty file.
14 changes: 14 additions & 0 deletions build/lib/django_mako_plus/app_template/views/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.conf import settings
from django_mako_plus import view_function, jscontext
from datetime import datetime, timezone

@view_function
def process_request(request):
utc_time = datetime.utcnow()
context = {
# sent to index.html:
'utc_time': utc_time,
# sent to index.html and index.js:
jscontext('utc_epoch'): utc_time.timestamp(),
}
return request.dmp.render('index.html', context)
94 changes: 94 additions & 0 deletions build/lib/django_mako_plus/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from django.apps import apps, AppConfig
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.template import engines

from .defaults import DEFAULT_OPTIONS
from .provider.runner import ProviderRun
from .signals import dmp_signal_register_app

import threading


class Config(AppConfig):
name = 'django_mako_plus'
label = 'django_mako_plus'
verbose_name = 'Django Mako Plus Templating Engine'

def ready(self):
'''Called by Django when the app is ready for use.'''
# set up the options
self.options = {}
self.options.update(DEFAULT_OPTIONS)
for template_engine in settings.TEMPLATES:
if template_engine.get('BACKEND', '').startswith('django_mako_plus'):
self.options.update(template_engine.get('OPTIONS', {}))

# dmp-enabled apps registry
self.registration_lock = threading.RLock()
self.registered_apps = {}

# init the template engine
self.engine = engines['django_mako_plus']

# default imports on every compiled template
self.template_imports = [
'import django_mako_plus',
'import django.utils.html', # used in template.py
]
self.template_imports.extend(self.options['DEFAULT_TEMPLATE_IMPORTS'])

# initialize the list of providers
ProviderRun.initialize_providers()

# set up the parameter converters (can't import until apps are set up)
from .converter.base import ParameterConverter
ParameterConverter._sort_converters(app_ready=True)


def register_app(self, app=None):
'''
Registers an app as a "DMP-enabled" app. Normally, DMP does this
automatically when included in urls.py.

If app is None, the DEFAULT_APP is registered.
'''
app = app or self.options['DEFAULT_APP']
if not app:
raise ImproperlyConfigured('An app name is required because DEFAULT_APP is empty - please use a '
'valid app name or set the DEFAULT_APP in settings')
if isinstance(app, str):
app = apps.get_app_config(app)

# since this only runs at startup, this lock doesn't affect performance
with self.registration_lock:
# short circuit if already registered
if app.name in self.registered_apps:
return

# first time for this app, so add to our dictionary
self.registered_apps[app.name] = app

# set up the template, script, and style renderers
# these create and cache just by accessing them
self.engine.get_template_loader(app, 'templates', create=True)
self.engine.get_template_loader(app, 'scripts', create=True)
self.engine.get_template_loader(app, 'styles', create=True)

# send the registration signal
if self.options['SIGNALS']:
dmp_signal_register_app.send(sender=self, app_config=app)


def get_registered_apps(self):
'''Returns a sequence of apps that are registered with DMP'''
return self.registered_apps.values()


def is_registered_app(self, app):
'''Returns true if the given app/app name is registered with DMP'''
if app is None:
return False
if isinstance(app, AppConfig):
app = app.name
return app in self.registered_apps
Loading