changes for ephemeral environments

This commit is contained in:
Chris Mitchell 2015-05-20 10:55:07 -04:00
parent 034cfaeaa2
commit a2de02c746
29 changed files with 95 additions and 57 deletions

4
MANIFEST.in Normal file
View File

@ -0,0 +1,4 @@
include LICENSE
include README.md
recursive-include djangui/templates *
recursive-exclude * *.pyc

View File

@ -2,4 +2,4 @@ from __future__ import absolute_import
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from djangui.djangui_celery import app as celery_app
from .djangui_celery_app import app as celery_app

View File

@ -1,3 +1,4 @@
from os import environ
from .djangui_settings import *
# This file is where the user can override and customize their installation of djangui
@ -20,6 +21,7 @@ CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend'
BROKER_URL = 'django://'
CELERY_TRACK_STARTED = True
DJANGUI_CELERY = True
CELERY_SEND_EVENTS = True
# Things you most likely do not need to change
@ -105,12 +107,13 @@ STATIC_URL = '/static/'
# AWS_HEADERS = {
# 'Cache-Control': 'max-age=%d, s-maxage=%d, must-revalidate' % (AWS_EXPIREY,
# AWS_EXPIREY)
# # }
# }
#
# STATIC_URL = 'http://%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
# MEDIA_URL = '/user-uploads/'
#
# STATICFILES_STORAGE = DEFAULT_FILE_STORAGE = 'djangui.djanguistorage.CachedS3BotoStorage'
# DJANGUI_EPHEMERAL_FILES = True
AUTHENTICATION_BACKEND = 'django.contrib.auth.backends.ModelBackend'

View File

@ -1,2 +1 @@
default_app_config = 'djangui.apps.DjanguiConfig'
from . import signals
default_app_config = 'djangui.apps.DjanguiConfig'

View File

@ -1,24 +1,25 @@
from django.contrib import admin
from __future__ import absolute_import
from django.contrib.admin import ModelAdmin, site
from .models import Script, ScriptGroup, ScriptParameter, DjanguiJob, ScriptParameterGroup
from djangui.models import Script, ScriptGroup, ScriptParameter, DjanguiJob, ScriptParameterGroup
class JobAdmin(admin.ModelAdmin):
class JobAdmin(ModelAdmin):
list_display = ('user', 'job_name', 'script', 'status', 'created_date')
class ScriptAdmin(admin.ModelAdmin):
class ScriptAdmin(ModelAdmin):
list_display = ('script_name', 'script_group', 'is_active', 'script_version')
class ParameterAdmin(admin.ModelAdmin):
class ParameterAdmin(ModelAdmin):
list_display = ('script', 'parameter_group', 'short_param')
class GroupAdmin(admin.ModelAdmin):
class GroupAdmin(ModelAdmin):
list_display = ('group_name', 'is_active')
class ParameterGroupAdmin(admin.ModelAdmin):
class ParameterGroupAdmin(ModelAdmin):
list_display = ('script', 'group_name')
admin.site.register(DjanguiJob, JobAdmin)
admin.site.register(Script, ScriptAdmin)
admin.site.register(ScriptParameter, ParameterAdmin)
admin.site.register(ScriptGroup, GroupAdmin)
admin.site.register(ScriptParameterGroup, ParameterGroupAdmin)
site.register(DjanguiJob, JobAdmin)
site.register(Script, ScriptAdmin)
site.register(ScriptParameter, ParameterAdmin)
site.register(ScriptGroup, GroupAdmin)
site.register(ScriptParameterGroup, ParameterGroupAdmin)

View File

@ -2,9 +2,6 @@ import traceback
import sys
from django.apps import AppConfig
from django.conf import settings
class DjanguiConfig(AppConfig):
name = 'djangui'
@ -15,4 +12,5 @@ class DjanguiConfig(AppConfig):
try:
utils.load_scripts()
except:
sys.stderr.write('Unable to load scripts:\n{}\n'.format(traceback.format_exc()))
sys.stderr.write('Unable to load scripts:\n{}\n'.format(traceback.format_exc()))
from . import signals

View File

@ -1 +1,2 @@
from __future__ import absolute_import
__author__ = 'chris'

View File

@ -1,3 +1,4 @@
from __future__ import absolute_import
import argparse
import sys
import json

View File

@ -1,3 +1,4 @@
from __future__ import absolute_import
__author__ = 'chris'
import argparse
import sys

View File

@ -1,3 +1,4 @@
from __future__ import absolute_import
__author__ = 'chris'
import json
import errno
@ -180,11 +181,18 @@ def add_djangui_script(script=None, group=None):
return (True, '')
def valid_user(obj, user):
# TODO: Make this function better and more consistent with its return
groups = obj.user_groups.all()
from ..models import Script
if isinstance(obj, Script):
from itertools import chain
groups = list(chain(groups, obj.script_group.user_groups.all()))
if not user.is_authenticated() and djangui_settings.DJANGUI_ALLOW_ANONYMOUS and len(groups) == 0:
return True
if not groups and obj.is_active:
return True
if obj.is_active is True:
if user.groups.filter(name__in=groups).exists():
if set(list(user.groups.all())) & set(list(groups)):
return True
return 'disabled' if djangui_settings.DJANGUI_SHOW_LOCKED_SCRIPTS else 'hide'

View File

@ -1,3 +1,5 @@
from __future__ import absolute_import
from boto.utils import parse_ts
from django.core.files.storage import get_storage_class
from storages.backends.s3boto import S3BotoStorage

View File

@ -1,3 +1,4 @@
from __future__ import absolute_import
__author__ = 'chris'
from .factory import *
from .scripts import *

View File

@ -1,3 +1,4 @@
from __future__ import absolute_import
__author__ = 'chris'
import copy
import json

View File

@ -1,3 +1,4 @@
from __future__ import absolute_import
__author__ = 'chris'
from django.forms import CharField, FileField, FilePathField
from django.forms import widgets

View File

@ -1,3 +1,4 @@
from __future__ import absolute_import
from django import forms
from ..backend import utils

View File

@ -1 +1,2 @@
from .djangui import *
from __future__ import absolute_import
from .core import *

View File

@ -1,3 +1,4 @@
from __future__ import absolute_import
__author__ = 'chris'
import os
import errno
@ -19,9 +20,9 @@ from autoslug import AutoSlugField
from celery import states
from .. import settings as djangui_settings
from ..backend import utils
from .. backend import utils
from .mixins import UpdateScriptsMixin, ModelDiffMixin
from . mixins import UpdateScriptsMixin, ModelDiffMixin
# TODO: Handle cases where celery is not setup but specified to be used

View File

@ -1,3 +1,4 @@
from __future__ import absolute_import
__author__ = 'chris'
from django.db import models
from ..forms import fields as djangui_form_fields

View File

@ -1,3 +1,4 @@
from __future__ import absolute_import
__author__ = 'chris'
from django.forms.models import model_to_dict

View File

@ -1,3 +1,5 @@
from __future__ import absolute_import
from django.db.models.signals import post_delete
from celery.signals import task_postrun, task_prerun, task_revoked
@ -21,7 +23,8 @@ def reload_scripts(**kwargs):
from .backend import utils
utils.load_scripts()
from models import Script, ScriptGroup, ScriptParameter, ScriptParameterGroup
# TODO: Figure out why relative imports fail here
from djangui.models import Script, ScriptGroup, ScriptParameter, ScriptParameterGroup
post_delete.connect(reload_scripts, sender=Script)
post_delete.connect(reload_scripts, sender=ScriptGroup)
post_delete.connect(reload_scripts, sender=ScriptParameter)

View File

@ -126,7 +126,7 @@
<div role="tabpanel" class="tab-pane active" id="task-output-all">
<dl class="dl-horizontal">
{% for file in task_info.all_files %}
<dt>{{ file.slug }}</dt>
<dt>{% if file.slug %}{{ file.slug }}{% endif %}</dt>
<dd><a href="{{ file.url }}">{{ file.name }}</a></dd>
{% endfor %}
</dl>

View File

@ -1,4 +1,4 @@
from __future__ import division
from __future__ import division, absolute_import
from django import template
from .. import settings as djangui_settings

View File

@ -1,3 +1,5 @@
from __future__ import absolute_import
from django.conf.urls import include, url
from django.conf import settings
from django.conf.urls.static import static

View File

@ -1,3 +1,4 @@
from __future__ import absolute_import
from .mixins import *
from .views import *
from .djangui_celery import *

View File

@ -1,3 +1,4 @@
from __future__ import absolute_import
from django.views.generic import CreateView
from django.http import JsonResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse

View File

@ -1,3 +1,4 @@
from __future__ import absolute_import
import os
from django.http import JsonResponse
@ -36,26 +37,28 @@ def celery_task_command(request):
command = request.POST.get('celery-command')
job_id = request.POST.get('job-id')
job = DjanguiJob.objects.get(pk=job_id)
user = None if not request.user.is_authenticated() and djangui_settings.DJANGUI_ALLOW_ANONYMOUS else request.user
response = {'valid': False,}
if user == job.user:
if command == 'resubmit':
new_job = job.submit_to_celery(resubmit=True)
response.update({'valid': True, 'extra': {'task_url': reverse('celery_results_info', kwargs={'job_id': new_job.pk})}})
elif command == 'clone':
response.update({'valid': True, 'redirect': '{0}?job_id={1}'.format(reverse('djangui_task_launcher'), job_id)})
elif command == 'delete':
job.status = DjanguiJob.DELETED
job.save()
response.update({'valid': True, 'redirect': reverse('djangui_home')})
elif command == 'stop':
celery_app.control.revoke(job.celery_id, signal='SIGKILL', terminate=True)
job.status = states.REVOKED
job.save()
response.update({'valid': True, 'redirect': reverse('celery_results_info', kwargs={'job_id': job_id})})
else:
response.update({'errors': {'__all__': force_unicode(_("Unknown Command"))}})
return JsonResponse(response)
from ..backend.utils import valid_user
if valid_user(job.script, request.user) is True:
user = request.user if request.user.is_authenticated() else None
response = {'valid': False,}
if user == job.user:
if command == 'resubmit':
new_job = job.submit_to_celery(resubmit=True)
response.update({'valid': True, 'extra': {'task_url': reverse('celery_results_info', kwargs={'job_id': new_job.pk})}})
elif command == 'clone':
response.update({'valid': True, 'redirect': '{0}?job_id={1}'.format(reverse('djangui_task_launcher'), job_id)})
elif command == 'delete':
job.status = DjanguiJob.DELETED
job.save()
response.update({'valid': True, 'redirect': reverse('djangui_home')})
elif command == 'stop':
celery_app.control.revoke(job.celery_id, signal='SIGKILL', terminate=True)
job.status = states.REVOKED
job.save()
response.update({'valid': True, 'redirect': reverse('celery_results_info', kwargs={'job_id': job_id})})
else:
response.update({'errors': {'__all__': force_unicode(_("Unknown Command"))}})
return JsonResponse(response)
class CeleryTaskView(TemplateView):

View File

@ -1,3 +1,4 @@
from __future__ import absolute_import
from django.views.generic import DetailView, TemplateView, CreateView
from django.http import JsonResponse
from django.conf import settings
@ -58,12 +59,12 @@ class DjanguiScriptJSON(DetailView):
if not form.errors:
# data = form.cleaned_data
script = Script.objects.get(pk=form.cleaned_data.get('djangui_type'))
if valid_user(script, request.user) is True and valid_user(script.script_group, request.user) is True:
job = form.save()
job.submit_to_celery()
return JsonResponse({'valid': True})
if valid_user(script, request.user) is True:
if valid_user(script, request.user) is True and valid_user(script.script_group, request.user) is True:
job = form.save()
job.submit_to_celery()
return JsonResponse({'valid': True})
return JsonResponse({'valid': False, 'errors': {'__all__': [force_unicode(_('You are not permitted to access this script.'))]}})
return JsonResponse({'valid': False, 'errors': form.errors})

View File

@ -1,16 +1,19 @@
import os
from setuptools import setup
from setuptools import setup, find_packages
with open(os.path.join(os.path.dirname(__file__), 'README.md')) as readme:
README = readme.read()
# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))
DJANGUI_TEMPLATE_DIR = os.path.join('djangui', 'templates')
setup(
name='djangui',
version='0.0.0',
packages=['djangui'],
packages=find_packages(),
data_files=[(DJANGUI_TEMPLATE_DIR, [os.path.join(root, filename) for root, folders, files in os.walk(DJANGUI_TEMPLATE_DIR)
for filename in files])],
scripts=['scripts/djanguify.py'],
include_package_data=True,
license='GPLv3',
@ -27,9 +30,7 @@ setup(
'Operating System :: OS Independent',
'Programming Language :: Python',
# Replace these appropriately if you are stuck on Python 2.
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 2.7',
'Topic :: Internet :: WWW/HTTP',
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
],