<commit_before>from __future__ import unicode_literals
from builtins import object

from datetime import datetime
from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext_lazy as _
from django import forms
from crispy_forms import layout
from django.views.generic import TemplateView
from django.utils import timezone
from django_cradmin import uicontainer

from django_cradmin.viewhelpers import objecttable
from django_cradmin.viewhelpers import listfilter
from django_cradmin.viewhelpers import create
from django_cradmin.viewhelpers import update
from django_cradmin.viewhelpers import delete
from django_cradmin.viewhelpers import multiselect
from django_cradmin import crapp
from django_cradmin.acemarkdown.widgets import AceMarkdownWidget
from django_cradmin.crispylayouts import PrimarySubmit
from django_cradmin.apps.cradmin_imagearchive.models import ArchiveImage
from django_cradmin.demo.webdemo.models import Page
from django_cradmin.widgets import filewidgets
from django_cradmin.widgets.modelchoice import ModelChoiceWidget
from django_cradmin.widgets.datetimepicker import DateTimePickerWidget, DatePickerWidget

class TitleColumn(objecttable.MultiActionColumn):
    modelfield = 'title'

    def get_buttons(self, obj):
        return [
            objecttable.Button(
                label='Edit',
                url=self.reverse_appurl('edit', args=[obj.id])),
            objecttable.PagePreviewButton(
                label='View',
                url=self.reverse_appurl('preview', args=[obj.id])),
            objecttable.Button(
                label='Delete',
                url=self.reverse_appurl('delete', args=[obj.id]),
                buttonclass="btn btn-danger btn-sm"),
        ]

class PagesQuerySetForRoleMixin(object):
    """
    Used by listing, update and delete view to ensure
    that only pages that the current role has access to
    is available.
    """
    def get_queryset_for_role(self, site):
        return Page.objects.filter(site=site)

class ArchiveImagePreviewColumn(objecttable.ImagePreviewColumn):
    modelfield = 'image'

    def render_value(self, obj):
        if obj.image:
            return obj.image.image
        else:
            return None

class IntroColumn(objecttable.TruncatecharsPlainTextColumn):
    modelfield = 'intro'
    maxlength = 40
    allcells_css_classes = ['hidden-xs']

class PagesListView(objecttable.FilterListMixin, PagesQuerySetForRoleMixin, objecttable.ObjectTableView):
    model = Page
    enable_previews = True
    columns = [
        ArchiveImagePreviewColumn,
        TitleColumn,
        IntroColumn,
    ]
    # filterlist_class = listfilter.lists.Horizontal

    def get_buttons(self):
        app = self.request.cradmin_app
        return [
            objecttable.Button(label=_('Create'),
                               url=app.reverse_appurl('create'),
                               buttonclass='btn btn-primary'),
        ]

    def get_multiselect_actions(self):
        app = self.request.cradmin_app
        return [
            objecttable.MultiSelectAction(
                label=_('Edit'),
                url=app.reverse_appurl('multiedit')
            ),
        ]

    # def get_filterlist_position(self):
    #     return 'left'
    # def get_label_is_screenreader_only_by_default(self):
    #     return True

    def get_filterlist_url(self, filters_string):
        return self.request.cradmin_app.reverse_appurl(
            'filter', kwargs={'filters_string': filters_string})

    def add_filterlist_items(self, filterlist):
        filterlist.append(listfilter.django.single.textinput.Search(
            slug='search',
            label='Search',
            label_is_screenreader_only=True,
            modelfields=['title']))
        filterlist.append(listfilter.django.single.select.IsNotNull(
            slug='image', label='Has image?'))
        filterlist.append(listfilter.django.single.select.DateTime(
            slug='publishing_time', label='Publishing time'))

    def get_unfiltered_queryset_for_role(self, site):
        return PagesQuerySetForRoleMixin.get_queryset_for_role(self, site=site)

class PageCreateUpdateMixin(object):
    model = Page
    roleid_field = 'site'
    enable_modelchoicefield_support = True
    fields = [
        'title',
        'intro',
        'body',
        'image',
        # 'publishing_time',
        # 'unpublish_time',
        'attachment',
        'internal_notes'
    ]

    def get_preview_url(self):
        return self.request.cradmin_app.reverse_appurl('preview')

    def _get_image_selectview_url(self):
        return self.request.cradmin_instance.reverse_url('imagearchive', 'singleselect')

    # def get_field_layout(self):
    #     return [
    #         layout.Div('title', css_class="cradmin-focusfield cradmin-focusfield-lg"),
    #         layout.Fieldset(
    #             'Image',
    #             'image',
    #         ),
    #         layout.Div('intro', css_class="cradmin-focusfield"),
    #         layout.Div('body', css_class="cradmin-focusfield"),
    #         layout.Fieldset(
    #             'Advanced',
    #             'publishing_time',
    #             'unpublish_time',
    #             'attachment',
    #             'internal_notes'
    #         ),
    #     ]

    # def get_form(self, form_class=None):
    #     form = super(PageCreateUpdateMixin, self).get_form(form_class=form_class)
    #     # form.fields['body'].widget = WysiHtmlTextArea(attrs={})
    #     form.fields['body'].widget = AceMarkdownWidget()
    #     preview = '
(No image selected)
'
    #     if self.object and self.object.image:
    #         preview = self.object.image.get_preview_html(request=self.request)
    #     form.fields['image'].widget = ModelChoiceWidget(
    #         queryset=ArchiveImage.objects.filter_owned_by_role(self.request.cradmin_role),
    #         preview=preview,
    #         selectview_url=self._get_image_selectview_url()
    #     )
    #     form.fields['publishing_time'].widget = DateTimePickerWidget(
    #         # minimum_datetime=datetime(2014, 8, 14, 12, 30),
    #         minimum_datetime=timezone.now(),
    #         maximum_datetime=datetime(2030, 12, 5, 21, 40),
    #         required=False
    #     )
    #     form.fields['unpublish_time'].widget = DatePickerWidget(
    #         no_value_preview_text='No date selected')
    #     # form.fields['unpublish_time'].widget = DateTimePickerWidget(
    #     #     no_value_preview_text='No date selected')
    #     form.fields['attachment'].widget = filewidgets.ImageWidget(request=self.request)
    #     # form.fields['attachment'].widget = filewidgets.FileWidget()
    #     return form

    def get_form(self, form_class=None):
        form = super(PageCreateUpdateMixin, self).get_form(form_class=form_class)
        # form.fields['body'].widget = WysiHtmlTextArea(attrs={})
        # form.fields['body'].widget = AceMarkdownWidget()
        # preview = '
(No image selected)
'
        # if self.object and self.object.image:
        #     preview = self.object.image.get_preview_html(request=self.request)
        # form.fields['image'].widget = ModelChoiceWidget(
        #     queryset=ArchiveImage.objects.filter_owned_by_role(self.request.cradmin_role),
        #     preview=preview,
        #     selectview_url=self._get_image_selectview_url()
        # )
        # form.fields['publishing_time'].widget = DateTimePickerWidget(
        #     # minimum_datetime=datetime(2014, 8, 14, 12, 30),
        #     minimum_datetime=timezone.now(),
        #     maximum_datetime=datetime(2030, 12, 5, 21, 40),
        #     required=False
        # )
        # form.fields['unpublish_time'].widget = DatePickerWidget(
        #     no_value_preview_text='No date selected')
        # # form.fields['unpublish_time'].widget = DateTimePickerWidget(
        # #     no_value_preview_text='No date selected')
        # form.fields['attachment'].widget = filewidgets.ImageWidget(request=self.request)
        # # form.fields['attachment'].widget = filewidgets.FileWidget()
        return form

    def get_form_renderable(self):
        return uicontainer.uiforms.form.Form(
            form=self.get_form(),
            children=[
                uicontainer.uiforms.fieldwrapper.FieldWrapper('title'),
                uicontainer.uiforms.fieldwrapper.FieldWrapper('intro'),
                uicontainer.uiforms.fieldwrapper.FieldWrapper('body'),
                # uicontainer.uiforms.fieldwrapper.FieldWrapper('publishing_time'),
                uicontainer.uiforms.fieldset.FieldSet(
                    title='Advanced',
                    children=[
                        uicontainer.uiforms.fieldwrapper.FieldWrapper('internal_notes'),
                    ]
                ),
                uicontainer.button.SubmitPrimary(
                    text='Save')
            ]
        ).bootstrap()

class PageCreateView(PageCreateUpdateMixin, create.CreateView):
    """
    View used to create new pages.
    """

class PageUpdateView(PagesQuerySetForRoleMixin, PageCreateUpdateMixin, update.UpdateView):
    """
    View used to create edit existing pages.
    """

class PreviewPageView(TemplateView):
    template_name = 'webdemo/pages/preview.django.html'

    def __get_page(self):
        if self.kwargs['pk'] is None:
            return PageCreateView.get_preview_data(self.request)
        else:
            # NOTE: The queryset ensures only admins on the current site gains access.
            site = self.request.cradmin_role
            return get_object_or_404(Page.objects.filter(site=site), pk=self.kwargs['pk'])

    def get_context_data(self, **kwargs):
        context = super(PreviewPageView, self).get_context_data(**kwargs)
        context['page'] = self.__get_page()
        return context

class PageDeleteView(PagesQuerySetForRoleMixin, delete.DeleteView):
    """
    View used to delete existing pages.
    """

class PageMultiEditForm(forms.Form):
    new_body = forms.CharField(
        label="New value for the page body",
        required=True, widget=forms.Textarea)

class PageMultiEditView(PagesQuerySetForRoleMixin, multiselect.MultiSelectFormView):
    form_class = PageMultiEditForm
    model = Page

    def get_buttons(self):
        return [
            PrimarySubmit('submit-save', _('Update text for selected pages'))
        ]

    def get_field_layout(self):
        return [
            layout.Div('new_body', css_class="cradmin-focusfield cradmin-focusfield-screenheight"),
        ]

    def form_valid(self, form):
        self.selected_objects.update(body=form.cleaned_data['new_body'])
        return self.success_redirect_response()

    def get_initial(self):
        return {
            'new_body': 'Write something here...'
        }

class App(crapp.App):
    appurls = [
        crapp.Url(
            r'^$',
            PagesListView.as_view(),
            name=crapp.INDEXVIEW_NAME),
        crapp.Url(
            r'^filter/(?P<filters_string>.+)?$',
            PagesListView.as_view(),
            name='filter'),
        crapp.Url(
            r'^create$',
            PageCreateView.as_view(),
            name="create"),
        crapp.Url(
            r'^edit/(?P<pk>\d+)$',
            PageUpdateView.as_view(),
            name="edit"),
        crapp.Url(
            r'^preview/(?P<pk>\d+)?$',
            PreviewPageView.as_view(),
            name="preview"),
        crapp.Url(
            r'^delete/(?P<pk>\d+)$',
            PageDeleteView.as_view(),
            name="delete"),
        crapp.Url(
            r'^multiedit/$',
            PageMultiEditView.as_view(),
            name="multiedit")
    ]
<commit_msg>webdemo...pages: Cleanup.<commit_after>from __future__ import unicode_literals
from builtins import object

from datetime import datetime
from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext_lazy as _
from django import forms
from crispy_forms import layout
from django.views.generic import TemplateView
from django.utils import timezone
from django_cradmin import uicontainer

from django_cradmin.viewhelpers import objecttable
from django_cradmin.viewhelpers import listfilter
from django_cradmin.viewhelpers import create
from django_cradmin.viewhelpers import update
from django_cradmin.viewhelpers import delete
from django_cradmin.viewhelpers import multiselect
from django_cradmin import crapp
from django_cradmin.acemarkdown.widgets import AceMarkdownWidget
from django_cradmin.crispylayouts import PrimarySubmit
from django_cradmin.apps.cradmin_imagearchive.models import ArchiveImage
from django_cradmin.demo.webdemo.models import Page
from django_cradmin.widgets import filewidgets
from django_cradmin.widgets.modelchoice import ModelChoiceWidget
from django_cradmin.widgets.datetimepicker import DateTimePickerWidget, DatePickerWidget

class TitleColumn(objecttable.MultiActionColumn):
    modelfield = 'title'

    def get_buttons(self, obj):
        return [
            objecttable.Button(
                label='Edit',
                url=self.reverse_appurl('edit', args=[obj.id])),
            objecttable.PagePreviewButton(
                label='View',
                url=self.reverse_appurl('preview', args=[obj.id])),
            objecttable.Button(
                label='Delete',
                url=self.reverse_appurl('delete', args=[obj.id]),
                buttonclass="btn btn-danger btn-sm"),
        ]

class PagesQuerySetForRoleMixin(object):
    """
    Used by listing, update and delete view to ensure
    that only pages that the current role has access to
    is available.
    """
    def get_queryset_for_role(self, site):
        return Page.objects.filter(site=site)

class ArchiveImagePreviewColumn(objecttable.ImagePreviewColumn):
    modelfield = 'image'

    def render_value(self, obj):
        if obj.image:
            return obj.image.image
        else:
            return None

class IntroColumn(objecttable.TruncatecharsPlainTextColumn):
    modelfield = 'intro'
    maxlength = 40
    allcells_css_classes = ['hidden-xs']

class PagesListView(objecttable.FilterListMixin, PagesQuerySetForRoleMixin, objecttable.ObjectTableView):
    model = Page
    enable_previews = True
    columns = [
        ArchiveImagePreviewColumn,
        TitleColumn,
        IntroColumn,
    ]
    # filterlist_class = listfilter.lists.Horizontal

    def get_buttons(self):
        app = self.request.cradmin_app
        return [
            objecttable.Button(label=_('Create'),
                               url=app.reverse_appurl('create'),
                               buttonclass='btn btn-primary'),
        ]

    def get_multiselect_actions(self):
        app = self.request.cradmin_app
        return [
            objecttable.MultiSelectAction(
                label=_('Edit'),
                url=app.reverse_appurl('multiedit')
            ),
        ]

    # def get_filterlist_position(self):
    #     return 'left'
    # def get_label_is_screenreader_only_by_default(self):
    #     return True

    def get_filterlist_url(self, filters_string):
        return self.request.cradmin_app.reverse_appurl(
            'filter', kwargs={'filters_string': filters_string})

    def add_filterlist_items(self, filterlist):
        filterlist.append(listfilter.django.single.textinput.Search(
            slug='search',
            label='Search',
            label_is_screenreader_only=True,
            modelfields=['title']))
        filterlist.append(listfilter.django.single.select.IsNotNull(
            slug='image', label='Has image?'))
        filterlist.append(listfilter.django.single.select.DateTime(
            slug='publishing_time', label='Publishing time'))

    def get_unfiltered_queryset_for_role(self, site):
        return PagesQuerySetForRoleMixin.get_queryset_for_role(self, site=site)

class PageCreateUpdateMixin(object):
    model = Page
    roleid_field = 'site'
    enable_modelchoicefield_support = True
    fields = [
        'title',
        'intro',
        'body',
        'image',
        # 'publishing_time',
        # 'unpublish_time',
        'attachment',
        'internal_notes'
    ]

    def get_preview_url(self):
        return self.request.cradmin_app.reverse_appurl('preview')

    def _get_image_selectview_url(self):
        return self.request.cradmin_instance.reverse_url('imagearchive', 'singleselect')

    # def get_field_layout(self):
    #     return [
    #         layout.Div('title', css_class="cradmin-focusfield cradmin-focusfield-lg"),
    #         layout.Fieldset(
    #             'Image',
    #             'image',
    #         ),
    #         layout.Div('intro', css_class="cradmin-focusfield"),
    #         layout.Div('body', css_class="cradmin-focusfield"),
    #         layout.Fieldset(
    #             'Advanced',
    #             'publishing_time',
    #             'unpublish_time',
    #             'attachment',
    #             'internal_notes'
    #         ),
    #     ]

    # def get_form(self, form_class=None):
    #     form = super(PageCreateUpdateMixin, self).get_form(form_class=form_class)
    #     # form.fields['body'].widget = WysiHtmlTextArea(attrs={})
    #     form.fields['body'].widget = AceMarkdownWidget()
    #     preview = '
(No image selected)
'
    #     if self.object and self.object.image:
    #         preview = self.object.image.get_preview_html(request=self.request)
    #     form.fields['image'].widget = ModelChoiceWidget(
    #         queryset=ArchiveImage.objects.filter_owned_by_role(self.request.cradmin_role),
    #         preview=preview,
    #         selectview_url=self._get_image_selectview_url()
    #     )
    #     form.fields['publishing_time'].widget = DateTimePickerWidget(
    #         # minimum_datetime=datetime(2014, 8, 14, 12, 30),
    #         minimum_datetime=timezone.now(),
    #         maximum_datetime=datetime(2030, 12, 5, 21, 40),
    #         required=False
    #     )
    #     form.fields['unpublish_time'].widget = DatePickerWidget(
    #         no_value_preview_text='No date selected')
    #     # form.fields['unpublish_time'].widget = DateTimePickerWidget(
    #     #     no_value_preview_text='No date selected')
    #     form.fields['attachment'].widget = filewidgets.ImageWidget(request=self.request)
    #     # form.fields['attachment'].widget = filewidgets.FileWidget()
    #     return form

    def get_form_renderable(self):
        return uicontainer.uiforms.form.Form(
            form=self.get_form(),
            children=[
                uicontainer.uiforms.fieldwrapper.FieldWrapper('title'),
                uicontainer.uiforms.fieldwrapper.FieldWrapper('intro'),
                uicontainer.uiforms.fieldwrapper.FieldWrapper('body'),
                # uicontainer.uiforms.fieldwrapper.FieldWrapper('publishing_time'),
                uicontainer.uiforms.fieldset.FieldSet(
                    title='Advanced',
                    children=[
                        uicontainer.uiforms.fieldwrapper.FieldWrapper('internal_notes'),
                    ]
                ),
                uicontainer.button.SubmitPrimary(
                    text='Save')
            ]
        ).bootstrap()

class PageCreateView(PageCreateUpdateMixin, create.CreateView):
    """
    View used to create new pages.
    """

class PageUpdateView(PagesQuerySetForRoleMixin, PageCreateUpdateMixin, update.UpdateView):
    """
    View used to create edit existing pages.
    """

class PreviewPageView(TemplateView):
    template_name = 'webdemo/pages/preview.django.html'

    def __get_page(self):
        if self.kwargs['pk'] is None:
            return PageCreateView.get_preview_data(self.request)
        else:
            # NOTE: The queryset ensures only admins on the current site gains access.
            site = self.request.cradmin_role
            return get_object_or_404(Page.objects.filter(site=site), pk=self.kwargs['pk'])

    def get_context_data(self, **kwargs):
        context = super(PreviewPageView, self).get_context_data(**kwargs)
        context['page'] = self.__get_page()
        return context

class PageDeleteView(PagesQuerySetForRoleMixin, delete.DeleteView):
    """
    View used to delete existing pages.
    """

class PageMultiEditForm(forms.Form):
    new_body = forms.CharField(
        label="New value for the page body",
        required=True, widget=forms.Textarea)

class PageMultiEditView(PagesQuerySetForRoleMixin, multiselect.MultiSelectFormView):
    form_class = PageMultiEditForm
    model = Page

    def get_buttons(self):
        return [
            PrimarySubmit('submit-save', _('Update text for selected pages'))
        ]

    def get_field_layout(self):
        return [
            layout.Div('new_body', css_class="cradmin-focusfield cradmin-focusfield-screenheight"),
        ]

    def form_valid(self, form):
        self.selected_objects.update(body=form.cleaned_data['new_body'])
        return self.success_redirect_response()

    def get_initial(self):
        return {
            'new_body': 'Write something here...'
        }

class App(crapp.App):
    appurls = [
        crapp.Url(
            r'^$',
            PagesListView.as_view(),
            name=crapp.INDEXVIEW_NAME),
        crapp.Url(
            r'^filter/(?P<filters_string>.+)?$',
            PagesListView.as_view(),
            name='filter'),
        crapp.Url(
            r'^create$',
            PageCreateView.as_view(),
            name="create"),
        crapp.Url(
            r'^edit/(?P<pk>\d+)$',
            PageUpdateView.as_view(),
            name="edit"),
        crapp.Url(
            r'^preview/(?P<pk>\d+)?$',
            PreviewPageView.as_view(),
            name="preview"),
        crapp.Url(
            r'^delete/(?P<pk>\d+)$',
            PageDeleteView.as_view(),
            name="delete"),
        crapp.Url(
            r'^multiedit/$',
            PageMultiEditView.as_view(),
            name="multiedit")
    ]
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

import random
import sys

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

option = '['+bcolors.OKBLUE+'x'+bcolors.ENDC+']'

project_name = sys.argv[1:][0]
base_dir = sys.argv[1:][1]
dbname = sys.argv[1:][2]
install_mode = sys.argv[1:][3]
project_dir = base_dir+'/'+project_name

msg = 'What is the timezone of your project? [UTC] > '
timezone = raw_input(msg)
if not timezone:
    timezone = 'UTC'
print "Timezone set to "+timezone

msg = 'What is the language code for your project? [en] > '
language_code = raw_input(msg)
if not language_code:
    language_code = 'en'
print "Language code set to "+language_code

msg = 'Debug: [Y/n] > '
debug_msg = raw_input(msg)
debug = True
if debug_msg == 'n' or debug_msg == 'no':
    debug = False
    print "Debug mode is disabled"
else:
    print "Debug mode is enabled"

databases = {
            'sqlite':"""DATABASES = {
                    'default': {
                        'ENGINE': 'django.db.backends.sqlite3',
                        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
                    }
                }""",
             }
database = databases['sqlite']

def secret_key():
    return ''.join([random.SystemRandom().choice('abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)') for i in range(50)])

file_content = """# -*- coding: utf-8 -*-

import os

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.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '"""+secret_key()+"""'

# SECURITY WARNING: don't run with debug turned on in production!\n\n"""
if debug is True:
    file_content += 'DEBUG = True\n'
else:
    file_content += 'DEBUG = False\n'
file_content += 'DEBUG_TOOLBAR = False\n'

file_content += """

ALLOWED_HOSTS = ['127.0.0.1','localhost']

INTERNAL_IPS = ['127.0.0.1','localhost']

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'django_extensions',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'sysmon',
    'compressor',
    'mptt',
    'mbase',
    'mqueue',
    'bootstrap3',
    'bootstrapform',
    "easy_thumbnails",
    "filer",
    # !extra_apps!
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = '"""+project_name+""".urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['templates'],
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                # !extra_context_processors!
            ],
            'debug': True,
            'loaders': [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
            ],
        },
    },
]

WSGI_APPLICATION = '"""+project_name+""".wsgi.application'

"""+database+"""

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
    }
}

SESSION_ENGINE = "django.contrib.sessions.backends.cache"

LOCALE_PATHS = ( os.path.join(BASE_DIR, 'locale'), )

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

LANGUAGE_CODE = '"""+language_code+"""'

TIME_ZONE = '"""+timezone+"""'

USE_I18N = True

USE_L10N = True

USE_TZ = True

STATIC_URL = '/static/'
#STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static') ]

COMPRESS_ROOT = os.path.join(BASE_DIR, 'static')
COMPRESS_ENABLED = True
COMPRESS_OFFLINE = True
COMPRESS_OFFLINE_CONTEXT = [
    {'THEME': 'default', 'STATIC_URL': STATIC_URL}
]

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

SITE_ID = 1

LOGIN_URL = '/account/login/'
LOGIN_REDIRECT_URL = '/'
LOGOUT_URL = "/account/logout/"

#~ Allauth settings
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = 'optional'
SOCIALACCOUNT_EMAIL_REQUIRED = False
SOCIALACCOUNT_EMAIL_VERIFICATION  = False
ACCOUNT_PASSWORD_MIN_LENGTH = 8
ACCOUNT_EMAIL_CONFIRMATION_ANONYMOUS_REDIRECT_URL = '/'
ACCOUNT_EMAIL_CONFIRMATION_AUTHENTICATED_REDIRECT_URL = '/account/'

AUTHENTICATION_BACKENDS = (
    # Needed to login by username in Django admin, regardless of `allauth`
    'django.contrib.auth.backends.ModelBackend',
    # `allauth` specific authentication methods, such as login by e-mail
    'allauth.account.auth_backends.AuthenticationBackend',
)

if DEBUG is True:
    EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
else:
    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    EMAIL_HOST = 'localhost'
    EMAIL_PORT = 25
    EMAIL_HOST_USER = ''
    EMAIL_HOST_PASSWORD = ''
    EMAIL_USE_TLS = False

#~ debug
if DEBUG_TOOLBAR:
    MIDDLEWARE_CLASSES+=('debug_toolbar.middleware.DebugToolbarMiddleware',)
    INSTALLED_APPS+=('debug_toolbar',)
    JQUERY_URL = '/static/js/jquery-2.1.4.min.js'

CKEDITOR_UPLOAD_PATH = 'uploads/'
CKEDITOR_JQUERY_URL = '/static/js/jquery-2.1.4.min.js'
CKEDITOR_CONFIGS = {
    'default': {
        'toolbar':  [
                    ["Format", "Styles", "Bold", "Italic", "Underline", '-', 'RemoveFormat'],
                    ['NumberedList', 'BulletedList', "Indent", "Outdent", 'JustifyLeft', 'JustifyCenter','JustifyRight', 'JustifyBlock'],
                    ["Image", "Table", "Link", "Unlink", "Anchor", "SectionLink", "Subscript", "Superscript"], ['Undo', 'Redo'],
                    ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord'],["Source", "Maximize"],
                    ],
        "removePlugins": "stylesheetparser",
        'width': '1150px',
        'height': '450px',
    },
    'public': {
      'toolbar':  [
                  ["Bold", "Italic"],
                  ['NumberedList', 'BulletedList', "Indent", "Outdent", 'JustifyLeft', 'JustifyCenter'],
                  ["Link", "Unlink"], ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord'], ['Undo', 'Redo'], ["Source", "Maximize"],
                  ],
      "removePlugins": "stylesheetparser",
  },
}
"""

# generate settings
filepath=project_dir+'/'+project_name+'/settings.py'
filex = open(filepath, "w")
filex.write(file_content)
filex.close()

<commit_msg>Django compressor settings<commit_after># -*- coding: utf-8 -*-

import random
import sys

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

option = '['+bcolors.OKBLUE+'x'+bcolors.ENDC+']'

project_name = sys.argv[1:][0]
base_dir = sys.argv[1:][1]
dbname = sys.argv[1:][2]
install_mode = sys.argv[1:][3]
project_dir = base_dir+'/'+project_name

msg = 'What is the timezone of your project? [UTC] > '
timezone = raw_input(msg)
if not timezone:
    timezone = 'UTC'
print "Timezone set to "+timezone

msg = 'What is the language code for your project? [en] > '
language_code = raw_input(msg)
if not language_code:
    language_code = 'en'
print "Language code set to "+language_code

msg = 'Debug: [Y/n] > '
debug_msg = raw_input(msg)
debug = True
if debug_msg == 'n' or debug_msg == 'no':
    debug = False
    print "Debug mode is disabled"
else:
    print "Debug mode is enabled"

databases = {
            'sqlite':"""DATABASES = {
                    'default': {
                        'ENGINE': 'django.db.backends.sqlite3',
                        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
                    }
                }""",
             }
database = databases['sqlite']

def secret_key():
    return ''.join([random.SystemRandom().choice('abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)') for i in range(50)])

file_content = """# -*- coding: utf-8 -*-

import os

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.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '"""+secret_key()+"""'

# SECURITY WARNING: don't run with debug turned on in production!\n\n"""
if debug is True:
    file_content += 'DEBUG = True\n'
else:
    file_content += 'DEBUG = False\n'
file_content += 'DEBUG_TOOLBAR = False\n'

file_content += """

ALLOWED_HOSTS = ['127.0.0.1','localhost']

INTERNAL_IPS = ['127.0.0.1','localhost']

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'django_extensions',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'sysmon',
    'compressor',
    'mptt',
    'mbase',
    'mqueue',
    'bootstrap3',
    'bootstrapform',
    "easy_thumbnails",
    "filer",
    # !extra_apps!
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = '"""+project_name+""".urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['templates'],
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                # !extra_context_processors!
            ],
            'debug': True,
            'loaders': [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
            ],
        },
    },
]

WSGI_APPLICATION = '"""+project_name+""".wsgi.application'

"""+database+"""

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
    }
}

SESSION_ENGINE = "django.contrib.sessions.backends.cache"

LOCALE_PATHS = ( os.path.join(BASE_DIR, 'locale'), )

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

LANGUAGE_CODE = '"""+language_code+"""'

TIME_ZONE = '"""+timezone+"""'

USE_I18N = True

USE_L10N = True

USE_TZ = True

STATIC_URL = '/static/'
#STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static') ]
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    # other finders..
    'compressor.finders.CompressorFinder',
)

COMPRESS_ROOT = os.path.join(BASE_DIR, 'static')
COMPRESS_ENABLED = True

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

SITE_ID = 1

LOGIN_URL = '/account/login/'
LOGIN_REDIRECT_URL = '/'
LOGOUT_URL = "/account/logout/"

#~ Allauth settings
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = 'optional'
SOCIALACCOUNT_EMAIL_REQUIRED = False
SOCIALACCOUNT_EMAIL_VERIFICATION  = False
ACCOUNT_PASSWORD_MIN_LENGTH = 8
ACCOUNT_EMAIL_CONFIRMATION_ANONYMOUS_REDIRECT_URL = '/'
ACCOUNT_EMAIL_CONFIRMATION_AUTHENTICATED_REDIRECT_URL = '/account/'

AUTHENTICATION_BACKENDS = (
    # Needed to login by username in Django admin, regardless of `allauth`
    'django.contrib.auth.backends.ModelBackend',
    # `allauth` specific authentication methods, such as login by e-mail
    'allauth.account.auth_backends.AuthenticationBackend',
)

if DEBUG is True:
    EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
else:
    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    EMAIL_HOST = 'localhost'
    EMAIL_PORT = 25
    EMAIL_HOST_USER = ''
    EMAIL_HOST_PASSWORD = ''
    EMAIL_USE_TLS = False

#~ debug
if DEBUG_TOOLBAR:
    MIDDLEWARE_CLASSES+=('debug_toolbar.middleware.DebugToolbarMiddleware',)
    INSTALLED_APPS+=('debug_toolbar',)
    JQUERY_URL = '/static/js/jquery-2.1.4.min.js'

CKEDITOR_UPLOAD_PATH = 'uploads/'
CKEDITOR_JQUERY_URL = '/static/js/jquery-2.1.4.min.js'
CKEDITOR_CONFIGS = {
    'default': {
        'toolbar':  [
                    ["Format", "Styles", "Bold", "Italic", "Underline", '-', 'RemoveFormat'],
                    ['NumberedList', 'BulletedList', "Indent", "Outdent", 'JustifyLeft', 'JustifyCenter','JustifyRight', 'JustifyBlock'],
                    ["Image", "Table", "Link", "Unlink", "Anchor", "SectionLink", "Subscript", "Superscript"], ['Undo', 'Redo'],
                    ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord'],["Source", "Maximize"],
                    ],
        "removePlugins": "stylesheetparser",
        'width': '1150px',
        'height': '450px',
    },
    'public': {
      'toolbar':  [
                  ["Bold", "Italic"],
                  ['NumberedList', 'BulletedList', "Indent", "Outdent", 'JustifyLeft', 'JustifyCenter'],
                  ["Link", "Unlink"], ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord'], ['Undo', 'Redo'], ["Source", "Maximize"],
                  ],
      "removePlugins": "stylesheetparser",
  },
}
"""

# generate settings
filepath=project_dir+'/'+project_name+'/settings.py'
filex = open(filepath, "w")
filex.write(file_content)
filex.close()

<|endoftext|>


<commit_before>from . import asg
from . import dns
from . import elb
from . import sec
import boto
from .billowService import billowService
from .billowGroup import billowGroup

class billowRegion():

    """
    a large undulating mass of cloud services
    """

    def __init__(self, region='us-east-1'):
        self.region = region

        # Backends
        self.asg = asg.asg(region)
        self.dns = dns.dns(region)
        self.elb = elb.elb(region)
        self.sec = sec.sec(region)

        self.services = dict()

        self.tagservice = 'service'
        self.tagenviron = 'env'
        self.servicetags = [self.tagenviron, self.tagservice]

    def list_services(self):
        self.services = dict()
        tags = self.asg.list_tags(tag=self.servicetags)

        # service
        for t in tags:
            if t.key == self.tagservice:
                self.add_service(t.value, t.resource_id)

        # environ
        for t in tags:
            if t.key == self.tagenviron:
                self.update_group_environ(t.resource_id, t.value)

        return self.services.values()

    def add_service(self, service, group=None):
        """
        u'servicename': {
            'groups': [u'servicename-env'],
            'region': 'us-east-1',
            'service': u'servicename',
            'environ': u'environment'
        }
        """
        if service not in self.services:
            self.services[service] = billowService(
                service,
                list(),
                self.region)
        if group:
            if group not in self.services[service].groups:
                self.services[service].groups.append(
                    billowGroup(group, region=self.region))

    def update_group_environ(self, group, environ):
        for k, v in self.services.iteritems():
            if v.environ:
                continue
            for g in v.groups:
                if g == group:
                    if v.environ and v.environ != environ:
                        sys.stderr.write("billowRegion: environ overwrite \
                                %s -> %s" % v.environ, environ)
                    v.environ = environ
                    return
<commit_msg>billowRegion -> (get_service, find_service, add_service, canon_service)<commit_after>from . import asg
from . import dns
from . import elb
from . import sec
import boto
import sys
from .billowService import billowService
from .billowGroup import billowGroup

class billowRegion(object):

    """
    a large undulating mass of cloud services
    """

    def __init__(self, region='us-east-1', parent=None):
        self.region = region
        self.parent = parent
        self.services = list()

        # Backends
        self.asg = asg.asg(self.region)
        self.dns = dns.dns(self.region)
        self.elb = elb.elb(self.region)
        self.sec = sec.sec(self.region)

        self.tagservice = 'service'
        self.tagenviron = 'env'
        self.servicetags = [self.tagenviron, self.tagservice]

    def find_service(self, service, environ=None):
        o = list()
        for s in self.services:
            if s.service == service:
                if environ:
                    if s.environ == environ:
                        o.append(s)
                    continue
                o.append(s)

        return o

    def list_services(self):
        self.services = list()
        servicenames = dict()
        tags = self.asg.list_tags(tag=self.servicetags)

        # service
        for t in tags:
            if t.key == self.tagservice:
                servicenames[t.resource_id] = { 'service': t.value }

        # environ
        for t in tags:
            if t.key == self.tagenviron:
                if t.resource_id not in servicenames:
                    servicenames[t.resource_id] = dict()
                servicenames[t.resource_id]['environ'] = t.value

        for k,v in servicenames.iteritems():
            if 'service' not in v:
                sys.stderr.write("group %s missing service tag\n" % k)
                continue
            if 'environ' not in v:
                sys.stderr.write("group %s missing environ tag\n" % k)
                continue
            self.add_service(v['service'], v['environ'], group=k)

        return self.services

    def add_service(self, service, environ, group=None):
        slist = self.find_service(service, environ=environ)
        if not slist:
            s = billowService(
                service,
                groups=list(),
                region=self.region,
                environ=environ,
                parent=self)
            self.services.append(s)
            slist = [s]

        if len(slist) > 1:
            sys.stderr.write("ignoring multiple services found for %s/%s\n" % (service, environ))

        s = slist[0]
        if group:
            if group not in s.groups:
                s.groups.append(billowGroup(group, region=self.region,
                    parent=s))

        return s

    def canon_service(self, service):
        if ':' in service:
            return service.split(':')[0]

        return service

    def get_service(self, service):
        if not self.services:
            self.list_services()
        service = self.canon_service(service)

        s = self.find_service(service)
        if not s:
            # parse environment if it was present
            if '-' in service:
                sname = service.rsplit('-', 1)[0]
                ename = service.rsplit('-', 1)[1]
                s = self.find_service(sname, environ=ename)
                if not s:
                    return list()

                return s

            return list()

        return s
<|endoftext|>


<commit_before>from conf import TOKN, EMAIL
import smtplib
import unittest
from request_handler import (Forms, create_msg, validate_name, is_valid_email,
                             is_hidden_field_empty, is_valid_token)
from werkzeug.test import Client
from werkzeug.testapp import test_app
from werkzeug.wrappers import BaseResponse, Request
from werkzeug.test import EnvironBuilder
from StringIO import StringIO
from mock import Mock, create_autospec, MagicMock, patch
from email.mime.text import MIMEText

class TestFormsender(unittest.TestCase):

    def test_create_msg_with_content(self):
        """
        Tests create_msg with content in the POST request

        Checks that each element in the request is returned create_msg
        """
        builder = EnvironBuilder(method='POST',
                                 data={'foo' : 'this is some text',
                                      'file': 'my file contents',
                                      'test': 'test.txt'})
        env = builder.get_environ()
        req = Request(env)
        self.assertEqual(create_msg(req)['foo'], builder.form['foo'])
        self.assertEqual(create_msg(req)['file'], builder.form['file'])
        self.assertEqual(create_msg(req)['test'], builder.form['test'])

    def test_create_msg_no_content(self):
        """
        Tests create_msg with no content in the POST request

        Checks that create_msg returns None
        """
        builder = EnvironBuilder(method='POST', data={})
        env = builder.get_environ()
        req = Request(env)
        self.assertIsNone(create_msg(req))

    def test_create_msg_with_content_get_method(self):
        """
        Tests create_msg with content in a GET request

        Checks that create_msg returns None
        """
        builder = EnvironBuilder(method='GET',
                                 data={'foo' : 'this is some text',
                                       'file': 'my file contents',
                                       'test': 'test.txt'})
        env = builder.get_environ()
        req = Request(env)
        self.assertIsNone(create_msg(req))

    def test_send_email(self):
        """
        Tests send_email

        send_email returns True when it successfully sends an email and
        False when unsuccessfull.
        """
        # Build test environment
        builder = EnvironBuilder(method='POST',
                                 data={'name': 'Valid Guy',
                                       'email': 'example@osuosl.org',
                                       'hidden': '',
                                       'tokn': TOKN })
        env = builder.get_environ()
        req = Request(env)

        # Construct message for assertion
        msg = create_msg(req)
        msg_send = MIMEText(str(msg))

        # Mock sendmail function
        smtplib.SMTP.sendmail = Mock('smtplib.SMTP.sendmail')

        # Call send_email and assert sendmail was called correctly
        real = Forms()
        real.send_email(msg)
        smtplib.SMTP.sendmail.assert_called_with('theform',
                                                 EMAIL,
                                                 msg_send.as_string())

    def test_validations_valid_data(self):
        """
        Tests the form validation with valid data.

        on_form_page checks for valid fields in submitted form and
        returns an error message if an invalid field is found.
        """
        builder = EnvironBuilder(method='POST',
                                 data={'name': 'Valid Guy',
                                       'email': 'example@osuosl.org',
                                       'hidden': '',
                                       'tokn': TOKN })
        env = builder.get_environ()
        req = Request(env)
        app = Forms()
        resp = app.on_form_page(req)
        self.assertEqual(resp.status_code, 200)

    def test_validations_invalid_name(self):
        """
        Tests the form validation with an invalid name.

        on_form_page checks for valid fields in submitted form and
        returns an error message if an invalid field is found.
        Invalid name field causes an 'Invalid Name' error.
        """
        builder = EnvironBuilder(method='POST',
                                 data={'name': '   ',
                                       'email': 'example@osuosl.org',
                                       'hidden': '',
                                       'tokn': TOKN })
        env = builder.get_environ()
        req = Request(env)
        app = Forms()
        resp = app.on_form_page(req)
        self.assertEqual(resp.status_code, 400)

    def test_validations_invalid_email(self):
        """
        Tests the form validation with an invalid email.

        on_form_page checks for valid fields in submitted form and
        returns an error message if an invalid field is found.
        Invalid email field causes an 'Invalid Email' error.
        """
        builder = EnvironBuilder(method='POST',
                                 data={'name': 'Valid Guy',
                                       'email': 'invalid@example.com',
                                       'hidden': '',
                                       'tokn': TOKN })
        env = builder.get_environ()
        req = Request(env)
        app = Forms()
        resp = app.on_form_page(req)
        self.assertEqual(resp.status_code, 400)

    def test_validations_invalid_hidden(self):
        """
        Tests the form validation with content in the hidden field.

        on_form_page checks for valid fields in submitted form and
        returns an error message if an invalid field is found.
        Content in the hidden field causes an 'Improper Form Submission'
        error.
        """
        builder = EnvironBuilder(method='POST',
                                 data={'name': 'Valid Guy',
                                       'email': 'example@osuosl.org',
                                       'hidden': 'r',
                                       'tokn': TOKN })
        env = builder.get_environ()
        req = Request(env)
        app = Forms()
        resp = app.on_form_page(req)
        self.assertEqual(resp.status_code, 400)

    def test_validations_invalid_token(self):
        """
        Tests the form validation with an invalid token.

        on_form_page checks for valid fields in submitted form and
        returns an error message if an invalid field is found.
        An invalid token causes the 'Improper Form Submission' error.
        """
        builder = EnvironBuilder(method='POST',
                                 data={'name': 'Valid Guy',
                                       'email': 'example@osuosl.org',
                                       'hidden': '',
                                       'tokn': 'evilrobot' })
        env = builder.get_environ()
        req = Request(env)
        app = Forms()
        resp = app.on_form_page(req)
        self.assertEqual(resp.status_code, 400)

    def test_is_valid_email_with_valid(self):
        """
        Tests is_valid_email with a valid email

        is_valid_email checks that the email submitted to the form is
        valid and exists. This function call should return true.
        """
        builder = EnvironBuilder(method='POST',
                                 data={'email': 'example@osuosl.org'})
        env = builder.get_environ()
        req = Request(env)
        self.assertTrue(is_valid_email(req))

    def test_is_valid_email_with_invalid(self):
        """
        Tests is_valid_email with an invalid email

        is_valid_email checks that the email submitted to the form is
        valid and exists. This function call should return false.
        """
        builder = EnvironBuilder(method='POST',
                                 data={'email': 'nope@example.com'})
        env = builder.get_environ()
        req = Request(env)
        self.assertFalse(is_valid_email(req))

    def test_validate_name_with_valid(self):
        """
        Tests validate_name with a valid name

        validate_name checks that the name submitted to the form does
        not contain disallowed characters. This function call should return
        true.
        """
        builder = EnvironBuilder(method='POST', data={'name': 'Matthew'})
        env = builder.get_environ()
        req = Request(env)
        self.assertTrue(validate_name(req))

    def test_validate_name_with_invalid(self):
        """
        Tests validate_name with an invalid name

        validate_name checks that the name submitted to the form does
        not contain disallowed characters. This function call should
        return false.
        """
        builder = EnvironBuilder(method='POST', data={'name': '  '})
        env = builder.get_environ()
        req = Request(env)
        self.assertFalse(validate_name(req))

    def test_is_hidden_field_empty_empty(self):
        """
        Tests is_hidden_field_empty with 'hidden' field empty

        is_hidden_field_empty checks that the hidden field in the form
        is empty. This function call should return true.
        """
        builder = EnvironBuilder(method='POST', data={'hidden': ''})
        env = builder.get_environ()
        req = Request(env)
        self.assertTrue(is_hidden_field_empty(req))

    def test_is_hidden_field_empty_full(self):
        """
        Tests is_hidden_field_empty with contents in 'hidden' field

        is_hidden_field_empty checks that the hidden field in the form
        is empty. This function call should return false.
        """
        builder = EnvironBuilder(method='POST', data={'hidden': 'nope'})
        env = builder.get_environ()
        req = Request(env)
        self.assertFalse(is_hidden_field_empty(req))

    def test_is_valid_token_valid(self):
        """
        Tests is_valid_token with TOKN defined in conf.py

        is_valid_token checks that the token provided by the form matches
        token described in the settings file. This function call should
        return true.
        """
        builder = EnvironBuilder(method='POST', data={'tokn': TOKN})
        env = builder.get_environ()
        req = Request(env)
        self.assertTrue(is_valid_token(req))

    def test_is_valid_token_invalid(self):
        """
        Tests is_valid_token with invalid token

        is_valid_token checks that the token provided by the form matches
        token described in the settings file. This function call should
        return false.
        """
        builder = EnvironBuilder(method='POST', data={'tokn': 'imarobot'})
        env = builder.get_environ()
        req = Request(env)
        self.assertFalse(is_valid_token(req))

    def test_rate_limiter_valid_rate(self):
        """
        Tests rate_limiter with a valid rate
        """
        builder = EnvironBuilder(method='POST', data={'name': 'Valid Guy',
                                        'email': 'example@osuosl.org',
                                        'hidden': '',
                                        'tokn': TOKN })
        env = builder.get_environ()
        req = Request(env)
        for i in range(5):
            app = Forms()
            app.on_form_page(req)

    def test_rate_limiter_invalid_rate(self):
        """
        Tests rate_limiter with an invalid rate
        """

if __name__ == '__main__':
    unittest.main()
<commit_msg>Modified rate limiter tests<commit_after>from conf import TOKN, EMAIL
import smtplib
import unittest
from request_handler import (Forms, create_msg, validate_name, is_valid_email,
                     is_hidden_field_empty, is_valid_token, RateLimiter)
from werkzeug.test import Client
from werkzeug.testapp import test_app
from werkzeug.wrappers import BaseResponse, Request
from werkzeug.test import EnvironBuilder
from StringIO import StringIO
from mock import Mock, create_autospec, MagicMock, patch
from email.mime.text import MIMEText

class TestFormsender(unittest.TestCase):

    def test_create_msg_with_content(self):
        """
        Tests create_msg with content in the POST request

        Checks that each element in the request is returned create_msg
        """
        builder = EnvironBuilder(method='POST',
                                 data={'foo' : 'this is some text',
                                      'file': 'my file contents',
                                      'test': 'test.txt'})
        env = builder.get_environ()
        req = Request(env)
        self.assertEqual(create_msg(req)['foo'], builder.form['foo'])
        self.assertEqual(create_msg(req)['file'], builder.form['file'])
        self.assertEqual(create_msg(req)['test'], builder.form['test'])

    def test_create_msg_no_content(self):
        """
        Tests create_msg with no content in the POST request

        Checks that create_msg returns None
        """
        builder = EnvironBuilder(method='POST', data={})
        env = builder.get_environ()
        req = Request(env)
        self.assertIsNone(create_msg(req))

    def test_create_msg_with_content_get_method(self):
        """
        Tests create_msg with content in a GET request

        Checks that create_msg returns None
        """
        builder = EnvironBuilder(method='GET',
                                 data={'foo' : 'this is some text',
                                       'file': 'my file contents',
                                       'test': 'test.txt'})
        env = builder.get_environ()
        req = Request(env)
        self.assertIsNone(create_msg(req))

    def test_send_email(self):
        """
        Tests send_email

        send_email returns True when it successfully sends an email and
        False when unsuccessfull.
        """
        # Build test environment
        builder = EnvironBuilder(method='POST',
                                 data={'name': 'Valid Guy',
                                       'email': 'example@osuosl.org',
                                       'hidden': '',
                                       'tokn': TOKN })
        env = builder.get_environ()
        req = Request(env)

        # Construct message for assertion
        msg = create_msg(req)
        msg_send = MIMEText(str(msg))

        # Mock sendmail function
        smtplib.SMTP.sendmail = Mock('smtplib.SMTP.sendmail')

        # Call send_email and assert sendmail was called correctly
        real = Forms()
        real.send_email(msg)
        smtplib.SMTP.sendmail.assert_called_with('theform',
                                                 EMAIL,
                                                 msg_send.as_string())

    def test_validations_valid_data(self):
        """
        Tests the form validation with valid data.

        on_form_page checks for valid fields in submitted form and
        returns an error message if an invalid field is found.
        """
        builder = EnvironBuilder(method='POST',
                                 data={'name': 'Valid Guy',
                                       'email': 'example@osuosl.org',
                                       'hidden': '',
                                       'tokn': TOKN })
        env = builder.get_environ()
        req = Request(env)
        app = Forms()
        resp = app.on_form_page(req)
        self.assertEqual(resp.status_code, 200)

    def test_validations_invalid_name(self):
        """
        Tests the form validation with an invalid name.

        on_form_page checks for valid fields in submitted form and
        returns an error message if an invalid field is found.
        Invalid name field causes an 'Invalid Name' error.
        """
        builder = EnvironBuilder(method='POST',
                                 data={'name': '   ',
                                       'email': 'example@osuosl.org',
                                       'hidden': '',
                                       'tokn': TOKN })
        env = builder.get_environ()
        req = Request(env)
        app = Forms()
        resp = app.on_form_page(req)
        self.assertEqual(resp.status_code, 400)

    def test_validations_invalid_email(self):
        """
        Tests the form validation with an invalid email.

        on_form_page checks for valid fields in submitted form and
        returns an error message if an invalid field is found.
        Invalid email field causes an 'Invalid Email' error.
        """
        builder = EnvironBuilder(method='POST',
                                 data={'name': 'Valid Guy',
                                       'email': 'invalid@example.com',
                                       'hidden': '',
                                       'tokn': TOKN })
        env = builder.get_environ()
        req = Request(env)
        app = Forms()
        resp = app.on_form_page(req)
        self.assertEqual(resp.status_code, 400)

    def test_validations_invalid_hidden(self):
        """
        Tests the form validation with content in the hidden field.

        on_form_page checks for valid fields in submitted form and
        returns an error message if an invalid field is found.
        Content in the hidden field causes an 'Improper Form Submission'
        error.
        """
        builder = EnvironBuilder(method='POST',
                                 data={'name': 'Valid Guy',
                                       'email': 'example@osuosl.org',
                                       'hidden': 'r',
                                       'tokn': TOKN })
        env = builder.get_environ()
        req = Request(env)
        app = Forms()
        resp = app.on_form_page(req)
        self.assertEqual(resp.status_code, 400)

    def test_validations_invalid_token(self):
        """
        Tests the form validation with an invalid token.

        on_form_page checks for valid fields in submitted form and
        returns an error message if an invalid field is found.
        An invalid token causes the 'Improper Form Submission' error.
        """
        builder = EnvironBuilder(method='POST',
                                 data={'name': 'Valid Guy',
                                       'email': 'example@osuosl.org',
                                       'hidden': '',
                                       'tokn': 'evilrobot' })
        env = builder.get_environ()
        req = Request(env)
        app = Forms()
        resp = app.on_form_page(req)
        self.assertEqual(resp.status_code, 400)

    def test_is_valid_email_with_valid(self):
        """
        Tests is_valid_email with a valid email

        is_valid_email checks that the email submitted to the form is
        valid and exists. This function call should return true.
        """
        builder = EnvironBuilder(method='POST',
                                 data={'email': 'example@osuosl.org'})
        env = builder.get_environ()
        req = Request(env)
        self.assertTrue(is_valid_email(req))

    def test_is_valid_email_with_invalid(self):
        """
        Tests is_valid_email with an invalid email

        is_valid_email checks that the email submitted to the form is
        valid and exists. This function call should return false.
        """
        builder = EnvironBuilder(method='POST',
                                 data={'email': 'nope@example.com'})
        env = builder.get_environ()
        req = Request(env)
        self.assertFalse(is_valid_email(req))

    def test_validate_name_with_valid(self):
        """
        Tests validate_name with a valid name

        validate_name checks that the name submitted to the form does
        not contain disallowed characters. This function call should return
        true.
        """
        builder = EnvironBuilder(method='POST', data={'name': 'Matthew'})
        env = builder.get_environ()
        req = Request(env)
        self.assertTrue(validate_name(req))

    def test_validate_name_with_invalid(self):
        """
        Tests validate_name with an invalid name

        validate_name checks that the name submitted to the form does
        not contain disallowed characters. This function call should
        return false.
        """
        builder = EnvironBuilder(method='POST', data={'name': '  '})
        env = builder.get_environ()
        req = Request(env)
        self.assertFalse(validate_name(req))

    def test_is_hidden_field_empty_empty(self):
        """
        Tests is_hidden_field_empty with 'hidden' field empty

        is_hidden_field_empty checks that the hidden field in the form
        is empty. This function call should return true.
        """
        builder = EnvironBuilder(method='POST', data={'hidden': ''})
        env = builder.get_environ()
        req = Request(env)
        self.assertTrue(is_hidden_field_empty(req))

    def test_is_hidden_field_empty_full(self):
        """
        Tests is_hidden_field_empty with contents in 'hidden' field

        is_hidden_field_empty checks that the hidden field in the form
        is empty. This function call should return false.
        """
        builder = EnvironBuilder(method='POST', data={'hidden': 'nope'})
        env = builder.get_environ()
        req = Request(env)
        self.assertFalse(is_hidden_field_empty(req))

    def test_is_valid_token_valid(self):
        """
        Tests is_valid_token with TOKN defined in conf.py

        is_valid_token checks that the token provided by the form matches
        token described in the settings file. This function call should
        return true.
        """
        builder = EnvironBuilder(method='POST', data={'tokn': TOKN})
        env = builder.get_environ()
        req = Request(env)
        self.assertTrue(is_valid_token(req))

    def test_is_valid_token_invalid(self):
        """
        Tests is_valid_token with invalid token

        is_valid_token checks that the token provided by the form matches
        token described in the settings file. This function call should
        return false.
        """
        builder = EnvironBuilder(method='POST', data={'tokn': 'imarobot'})
        env = builder.get_environ()
        req = Request(env)
        self.assertFalse(is_valid_token(req))

    def test_rate_limiter_valid_rate(self):
        """
        Tests rate_limiter with a valid rate
        """
        builder = EnvironBuilder(method='POST', data={'name': 'Valid Guy',
                                        'email': 'example@osuosl.org',
                                        'hidden': '',
                                        'tokn': TOKN })
        env = builder.get_environ()
        req = Request(env)
        for i in range(5):
            app = Forms()
            resp = app.on_form_page(req)

        self.assertEqual(resp.status_code, 200)
        self.assertIsNone(app.error)

    def test_rate_limiter_invalid_rate(self):
        """
        Tests rate_limiter with an invalid rate
        """
        builder = EnvironBuilder(method='POST', data={'name': 'Valid Guy',
                                        'email': 'example@osuosl.org',
                                        'hidden': '',
                                        'tokn': TOKN })
        env = builder.get_environ()
        req = Request(env)
        for i in range(50):
            app = Forms()
            resp = app.on_form_page(req)

        self.assertEqual(resp.status_code, 429)
        self.assertEqual(app.error, 'Too Many Requests')

if __name__ == '__main__':
    unittest.main()
<|endoftext|>


<commit_before>##############################################################################
#
# Copyright (c) 2004 TINY SPRL. (http://tiny.be)
#
# $Id: __init__.py 1308 2005-09-08 18:02:01Z pinky $
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contact a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

import os, sys, imp
import itertools
from sets import Set

import osv
import tools
import pooler

import netsvc
from osv import fields

import zipfile

logger = netsvc.Logger()

opj = os.path.join
ad = tools.config['addons_path']
sys.path.insert(1,ad)

class Graph(dict):

	def addNode(self, name, deps):
		max_depth, father = 0, None
		for n in [Node(x, self) for x in deps]:
			if n.depth >= max_depth:
				father = n
				max_depth = n.depth
		if father:
			father.addChild(name)
		else:
			Node(name, self)

	def __iter__(self):
		level = 0
		done = Set(self.keys())
		while done:
			level_modules = [(name, module) for name, module in self.items() if module.depth==level]
			for name, module in level_modules:
				done.remove(name)
				yield module
			level += 1

class Singleton(object):

	def __new__(cls, name, graph):
		if name in graph:
			inst = graph[name]
		else:
			inst = object.__new__(cls)
			inst.name = name
			graph[name] = inst
		return inst

class Node(Singleton):

	def __init__(self, name, graph):
		self.graph = graph
		if not hasattr(self, 'childs'):
			self.childs = []
		if not hasattr(self, 'depth'):
			self.depth = 0

	def addChild(self, name):
		node = Node(name, self.graph)
		node.depth = self.depth + 1
		if node not in self.childs:
			self.childs.append(node)
		for attr in ('init', 'update', 'demo'):
			if hasattr(self, attr):
				setattr(node, attr, True)
		self.childs.sort(lambda x,y: cmp(x.name, y.name))

	def hasChild(self, name):
		return Node(name, self.graph) in self.childs or \
				bool([c for c in self.childs if c.hasChild(name)])

	def __setattr__(self, name, value):
		super(Singleton, self).__setattr__(name, value)
		if name in ('init', 'update', 'demo'):
			tools.config[name][self.name] = 1
			for child in self.childs:
				setattr(child, name, value)
		if name == 'depth':
			for child in self.childs:
				setattr(child, name, value + 1)

	def __iter__(self):
		return itertools.chain(iter(self.childs), *map(iter, self.childs))

	def __str__(self):
		return self._pprint()

	def _pprint(self, depth=0):
		s = '%s\n' % self.name
		for c in self.childs:
			s += '%s`-> %s' % ('   ' * depth, c._pprint(depth+1))
		return s

def create_graph(module_list, force=[]):
	graph = Graph()
	packages = []

	for module in module_list:
		if module[-4:]=='.zip':
			module = module[:-4]
		terp_file = opj(ad, module, '__terp__.py')
		mod_path = opj(ad, module)
		if os.path.isfile(terp_file) or zipfile.is_zipfile(mod_path+'.zip'):
			info = eval(tools.file_open(terp_file).read())
			if info.get('installable', True):
				packages.append((module, info.get('depends', []), info))

	current,later = Set([p for p, dep, data in packages]), Set()
	while packages and current > later:
		package, deps, datas = packages[0]

		# if all dependencies of 'package' are already in the graph, add 'package' in the graph
		if reduce(lambda x,y: x and y in graph, deps, True):
			later.clear()
			current.remove(package)
			graph.addNode(package, deps)
			node = Node(package, graph)
			node.datas = datas
			for kind in ('init', 'demo', 'update'):
				if package in tools.config[kind] or 'all' in tools.config[kind] or kind in force:
					setattr(node, kind, True)
		else:
			later.add(package)
			packages.append((package, deps, datas))
		packages = packages[1:]

	for package in later:
		logger.notifyChannel('init', netsvc.LOG_ERROR, 'addon:%s:Unmet dependency' % package)

	return graph

def init_module_objects(cr, module_name, obj_list):
	pool = pooler.get_pool(cr.dbname)
	logger.notifyChannel('init', netsvc.LOG_INFO, 'addon:%s:creating or updating database tables' % module_name)
	for obj in obj_list:
		if hasattr(obj, 'init'):
			obj.init(cr)
		obj._auto_init(cr)
		cr.commit()

def load_module_graph(cr, graph, status={}):
	package_todo = []
	statusi = 0
	for package in graph:
		status['progress'] = (float(statusi)+0.1)/len(graph)
		m = package.name
		logger.notifyChannel('init', netsvc.LOG_INFO, 'addon:%s' % m)
		sys.stdout.flush()
		pool = pooler.get_pool(cr.dbname)
		modules = pool.instanciate(m)
		cr.execute('select state, demo from ir_module_module where name=%s', (m,))
		(package_state, package_demo) = (cr.rowcount and cr.fetchone()) or ('uninstalled', False)
		idref = {}
		status['progress'] = (float(statusi)+0.4)/len(graph)
		if hasattr(package, 'init') or hasattr(package, 'update') or package_state in ('to install', 'to upgrade'):
			init_module_objects(cr, m, modules)
			for kind in ('init', 'update'):
				for filename in package.datas.get('%s_xml' % kind, []):
					mode = 'update'
					if hasattr(package, 'init') or package_state=='to install':
						print 'Mode', mode, filename
						mode = 'init'
					logger.notifyChannel('init', netsvc.LOG_INFO, 'addon:%s:loading %s' % (m, filename))
					name, ext = os.path.splitext(filename)
					if ext == '.csv':
						tools.convert_csv_import(cr, m, os.path.basename(filename), tools.file_open(opj(m, filename)).read(), idref, mode=mode)
					elif ext == '.sql':
						queries = tools.file_open(opj(m, filename)).read().split(';')
						for query in queries:
							new_query = ' '.join(query.split())
							if new_query:
								cr.execute(new_query)
						cr.commit()
					else:
						tools.convert_xml_import(cr, m, tools.file_open(opj(m, filename)).read(), idref, mode=mode)
			if hasattr(package, 'demo') or (package_demo and package_state != 'installed'):
				status['progress'] = (float(statusi)+0.75)/len(graph)
				for xml in package.datas.get('demo_xml', []):
					name, ext = os.path.splitext(xml)
					logger.notifyChannel('init', netsvc.LOG_INFO, 'addon:%s:loading %s' % (m, xml))
					if ext == '.csv':
						tools.convert_csv_import(cr, m, os.path.basename(xml), tools.file_open(opj(m, xml)).read(), idref)
					else:
						tools.convert_xml_import(cr, m, tools.file_open(opj(m, xml)).read(), idref)
				cr.execute('update ir_module_module set demo=%s where name=%s', (True, package.name))
			package_todo.append(package.name)
			cr.execute("update ir_module_module set state='installed' where state in ('to upgrade', 'to install') and name=%s", (package.name,))
			cr.commit()
		statusi+=1

	pool = pooler.get_pool(cr.dbname)
	pool.get('ir.model.data')._process_end(cr, 1, package_todo)
	cr.commit()

def register_classes():
	module_list = os.listdir(ad)
	for package in create_graph(module_list):
		m = package.name
		logger.notifyChannel('init', netsvc.LOG_INFO, 'addon:%s:registering classes' % m)
		sys.stdout.flush()
		try:
			# XXX must restrict to only addons paths
			imp.load_module(m, *imp.find_module(m))
		except ImportError:
			import zipimport
			mod_path = opj(ad, m+'.zip')
			zimp = zipimport.zipimporter(mod_path)
			zimp.load_module(m)

def load_modules(db, force_demo=False, status={}, update_module=False):
	cr = db.cursor()
	force = []
	if force_demo:
		force.append('demo')
	if update_module:
		cr.execute("select name from ir_module_module where state in ('installed', 'to install', 'to upgrade','to remove')")
	else:
		cr.execute("select name from ir_module_module where state in ('installed', 'to upgrade', 'to remove')")
	module_list = [name for (name,) in cr.fetchall()]
	graph = create_graph(module_list, force)
	load_module_graph(cr, graph, status)

	for kind in ('init', 'demo', 'update'):
		tools.config[kind]={}

	cr.commit()
	if update_module:
		cr.execute("select id,name from ir_module_module where state in ('to remove')")
		for mod_id, mod_name in cr.fetchall():
			pool = pooler.get_pool(cr.dbname)
			cr.execute('select model,res_id from ir_model_data where not noupdate and module=%s order by id desc', (mod_name,))
			for rmod,rid in cr.fetchall():
				#
				# TO BE Improved:
				#   I can not use the class_pool has _table could be defined in __init__
				#   and I can not use the pool has the module could not be loaded in the pool
				#
				uid = 1
				print rmod
				pool.get(rmod).unlink(cr, uid, [rid])
			cr.commit()
		#
		# TODO: remove menu without actions of childs
		#
		cr.execute('''delete from
				ir_ui_menu
			where
				(id not in (select parent_id from ir_ui_menu where parent_id is not null))
			and
				(id not in (select res_id from ir_values where model='ir.ui.menu'))''')

		cr.execute("update ir_module_module set state=%s where state in ('to remove')", ('uninstalled', ))
		cr.commit()
		pooler.restart_pool(cr.dbname)
	cr.close()

<commit_msg>Removed print statement<commit_after>##############################################################################
#
# Copyright (c) 2004 TINY SPRL. (http://tiny.be)
#
# $Id: __init__.py 1308 2005-09-08 18:02:01Z pinky $
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contact a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

import os, sys, imp
import itertools
from sets import Set

import osv
import tools
import pooler

import netsvc
from osv import fields

import zipfile

logger = netsvc.Logger()

opj = os.path.join
ad = tools.config['addons_path']
sys.path.insert(1,ad)

class Graph(dict):

	def addNode(self, name, deps):
		max_depth, father = 0, None
		for n in [Node(x, self) for x in deps]:
			if n.depth >= max_depth:
				father = n
				max_depth = n.depth
		if father:
			father.addChild(name)
		else:
			Node(name, self)

	def __iter__(self):
		level = 0
		done = Set(self.keys())
		while done:
			level_modules = [(name, module) for name, module in self.items() if module.depth==level]
			for name, module in level_modules:
				done.remove(name)
				yield module
			level += 1

class Singleton(object):

	def __new__(cls, name, graph):
		if name in graph:
			inst = graph[name]
		else:
			inst = object.__new__(cls)
			inst.name = name
			graph[name] = inst
		return inst

class Node(Singleton):

	def __init__(self, name, graph):
		self.graph = graph
		if not hasattr(self, 'childs'):
			self.childs = []
		if not hasattr(self, 'depth'):
			self.depth = 0

	def addChild(self, name):
		node = Node(name, self.graph)
		node.depth = self.depth + 1
		if node not in self.childs:
			self.childs.append(node)
		for attr in ('init', 'update', 'demo'):
			if hasattr(self, attr):
				setattr(node, attr, True)
		self.childs.sort(lambda x,y: cmp(x.name, y.name))

	def hasChild(self, name):
		return Node(name, self.graph) in self.childs or \
				bool([c for c in self.childs if c.hasChild(name)])

	def __setattr__(self, name, value):
		super(Singleton, self).__setattr__(name, value)
		if name in ('init', 'update', 'demo'):
			tools.config[name][self.name] = 1
			for child in self.childs:
				setattr(child, name, value)
		if name == 'depth':
			for child in self.childs:
				setattr(child, name, value + 1)

	def __iter__(self):
		return itertools.chain(iter(self.childs), *map(iter, self.childs))

	def __str__(self):
		return self._pprint()

	def _pprint(self, depth=0):
		s = '%s\n' % self.name
		for c in self.childs:
			s += '%s`-> %s' % ('   ' * depth, c._pprint(depth+1))
		return s

def create_graph(module_list, force=[]):
	graph = Graph()
	packages = []

	for module in module_list:
		if module[-4:]=='.zip':
			module = module[:-4]
		terp_file = opj(ad, module, '__terp__.py')
		mod_path = opj(ad, module)
		if os.path.isfile(terp_file) or zipfile.is_zipfile(mod_path+'.zip'):
			info = eval(tools.file_open(terp_file).read())
			if info.get('installable', True):
				packages.append((module, info.get('depends', []), info))

	current,later = Set([p for p, dep, data in packages]), Set()
	while packages and current > later:
		package, deps, datas = packages[0]

		# if all dependencies of 'package' are already in the graph, add 'package' in the graph
		if reduce(lambda x,y: x and y in graph, deps, True):
			later.clear()
			current.remove(package)
			graph.addNode(package, deps)
			node = Node(package, graph)
			node.datas = datas
			for kind in ('init', 'demo', 'update'):
				if package in tools.config[kind] or 'all' in tools.config[kind] or kind in force:
					setattr(node, kind, True)
		else:
			later.add(package)
			packages.append((package, deps, datas))
		packages = packages[1:]

	for package in later:
		logger.notifyChannel('init', netsvc.LOG_ERROR, 'addon:%s:Unmet dependency' % package)

	return graph

def init_module_objects(cr, module_name, obj_list):
	pool = pooler.get_pool(cr.dbname)
	logger.notifyChannel('init', netsvc.LOG_INFO, 'addon:%s:creating or updating database tables' % module_name)
	for obj in obj_list:
		if hasattr(obj, 'init'):
			obj.init(cr)
		obj._auto_init(cr)
		cr.commit()

def load_module_graph(cr, graph, status={}):
	package_todo = []
	statusi = 0
	for package in graph:
		status['progress'] = (float(statusi)+0.1)/len(graph)
		m = package.name
		logger.notifyChannel('init', netsvc.LOG_INFO, 'addon:%s' % m)
		sys.stdout.flush()
		pool = pooler.get_pool(cr.dbname)
		modules = pool.instanciate(m)
		cr.execute('select state, demo from ir_module_module where name=%s', (m,))
		(package_state, package_demo) = (cr.rowcount and cr.fetchone()) or ('uninstalled', False)
		idref = {}
		status['progress'] = (float(statusi)+0.4)/len(graph)
		if hasattr(package, 'init') or hasattr(package, 'update') or package_state in ('to install', 'to upgrade'):
			init_module_objects(cr, m, modules)
			for kind in ('init', 'update'):
				for filename in package.datas.get('%s_xml' % kind, []):
					mode = 'update'
					if hasattr(package, 'init') or package_state=='to install':
						mode = 'init'
					logger.notifyChannel('init', netsvc.LOG_INFO, 'addon:%s:loading %s' % (m, filename))
					name, ext = os.path.splitext(filename)
					if ext == '.csv':
						tools.convert_csv_import(cr, m, os.path.basename(filename), tools.file_open(opj(m, filename)).read(), idref, mode=mode)
					elif ext == '.sql':
						queries = tools.file_open(opj(m, filename)).read().split(';')
						for query in queries:
							new_query = ' '.join(query.split())
							if new_query:
								cr.execute(new_query)
						cr.commit()
					else:
						tools.convert_xml_import(cr, m, tools.file_open(opj(m, filename)).read(), idref, mode=mode)
			if hasattr(package, 'demo') or (package_demo and package_state != 'installed'):
				status['progress'] = (float(statusi)+0.75)/len(graph)
				for xml in package.datas.get('demo_xml', []):
					name, ext = os.path.splitext(xml)
					logger.notifyChannel('init', netsvc.LOG_INFO, 'addon:%s:loading %s' % (m, xml))
					if ext == '.csv':
						tools.convert_csv_import(cr, m, os.path.basename(xml), tools.file_open(opj(m, xml)).read(), idref)
					else:
						tools.convert_xml_import(cr, m, tools.file_open(opj(m, xml)).read(), idref)
				cr.execute('update ir_module_module set demo=%s where name=%s', (True, package.name))
			package_todo.append(package.name)
			cr.execute("update ir_module_module set state='installed' where state in ('to upgrade', 'to install') and name=%s", (package.name,))
			cr.commit()
		statusi+=1

	pool = pooler.get_pool(cr.dbname)
	pool.get('ir.model.data')._process_end(cr, 1, package_todo)
	cr.commit()

def register_classes():
	module_list = os.listdir(ad)
	for package in create_graph(module_list):
		m = package.name
		logger.notifyChannel('init', netsvc.LOG_INFO, 'addon:%s:registering classes' % m)
		sys.stdout.flush()
		try:
			# XXX must restrict to only addons paths
			imp.load_module(m, *imp.find_module(m))
		except ImportError:
			import zipimport
			mod_path = opj(ad, m+'.zip')
			zimp = zipimport.zipimporter(mod_path)
			zimp.load_module(m)

def load_modules(db, force_demo=False, status={}, update_module=False):
	cr = db.cursor()
	force = []
	if force_demo:
		force.append('demo')
	if update_module:
		cr.execute("select name from ir_module_module where state in ('installed', 'to install', 'to upgrade','to remove')")
	else:
		cr.execute("select name from ir_module_module where state in ('installed', 'to upgrade', 'to remove')")
	module_list = [name for (name,) in cr.fetchall()]
	graph = create_graph(module_list, force)
	load_module_graph(cr, graph, status)

	for kind in ('init', 'demo', 'update'):
		tools.config[kind]={}

	cr.commit()
	if update_module:
		cr.execute("select id,name from ir_module_module where state in ('to remove')")
		for mod_id, mod_name in cr.fetchall():
			pool = pooler.get_pool(cr.dbname)
			cr.execute('select model,res_id from ir_model_data where not noupdate and module=%s order by id desc', (mod_name,))
			for rmod,rid in cr.fetchall():
				#
				# TO BE Improved:
				#   I can not use the class_pool has _table could be defined in __init__
				#   and I can not use the pool has the module could not be loaded in the pool
				#
				uid = 1
				pool.get(rmod).unlink(cr, uid, [rid])
			cr.commit()
		#
		# TODO: remove menu without actions of childs
		#
		cr.execute('''delete from
				ir_ui_menu
			where
				(id not in (select parent_id from ir_ui_menu where parent_id is not null))
			and
				(id not in (select res_id from ir_values where model='ir.ui.menu'))''')

		cr.execute("update ir_module_module set state=%s where state in ('to remove')", ('uninstalled', ))
		cr.commit()
		pooler.restart_pool(cr.dbname)
	cr.close()

<|endoftext|>


<commit_before>#!/usr/bin/env python
import json
import sys
import os
import re

def get_decisions():
    decisions = []
    for x in sys.argv[1:]:
        name = os.path.basename(x)
        study_tree = re.compile(r'([^_]+_[0-9]+)@([^_]+)\.tre')
        m = study_tree.match(name)
        study = m.group(1)
        tree = m.group(2)
        decisions.append({"studyID":study,"treeID":tree,"SHA":0,"name":name})
    return decisions

if __name__ == '__main__':
    document = {"contributors": [],
                "creator": {"login":"",
                            "name":""},
                "description": "generated from newicks",
                "name":"",
                "queries": [],
                "url": ""};
    document["decisions"] = get_decisions();
    print json.dumps(document, indent=4);

<commit_msg>Don't die if there's a _ in the tree name.<commit_after>#!/usr/bin/env python
import json
import sys
import os
import re

def get_decisions():
    decisions = []
    for x in sys.argv[1:]:
        name = os.path.basename(x)
        study_tree = re.compile(r'([^_]+_[0-9]+)@([^@]+)\.tre')
        m = study_tree.match(name)
        if m is None:
            sys.stderr.write("\n\nError: Name '{}' does not match require pattern! Not generating fake collection.\n".format(name))
            exit(1)
        study = m.group(1)
        tree = m.group(2)
        decisions.append({"studyID":study,"treeID":tree,"SHA":0,"name":name})
    return decisions

if __name__ == '__main__':
    document = {"contributors": [],
                "creator": {"login":"",
                            "name":""},
                "description": "generated from newicks",
                "name":"",
                "queries": [],
                "url": ""};
    document["decisions"] = get_decisions();
    print json.dumps(document, indent=4);

<|endoftext|>


<commit_before>#! /usr/bin/env python
""" Generates attention score visualizations.
"""

import os

import tensorflow as tf
from tensorflow.python.platform import gfile

import numpy as np
from matplotlib import pyplot as plt

from seq2seq.inference import create_inference_graph, create_predictions_iter

tf.flags.DEFINE_string("source", None, "path to source input data")
tf.flags.DEFINE_string("vocab_source", None, "Path to source vocabulary file")
tf.flags.DEFINE_string("vocab_target", None, "Path to target vocabulary file")
tf.flags.DEFINE_string("model", "AttentionSeq2Seq", "model class")
tf.flags.DEFINE_string("model_dir", None, "directory to load model from")
tf.flags.DEFINE_string("checkpoint_path", None,
                       """Full path to the checkpoint to be loaded. If None,
                       the latest checkpoint in the model dir is used.""")
tf.flags.DEFINE_integer("batch_size", 32, "the train/dev batch size")
tf.flags.DEFINE_integer("beam_width", None,
                        "Use beam search with this beam width for decoding")

tf.flags.DEFINE_string("output_dir", None,
                       "Write all attention plots to this directory")
tf.flags.DEFINE_boolean("no_plot", False,
                        "If set, does not generate attention plots.")

FLAGS = tf.flags.FLAGS
tf.logging.set_verbosity(tf.logging.INFO)

def get_prediction_length(predictions_dict):
  """Returns the length of the prediction based on the index
  of the first SEQUENCE_END token.
  """
  tokens_iter = enumerate(predictions_dict["predicted_tokens"])
  return next(
      ((i + 1) for i, _ in tokens_iter if _ == "SEQUENCE_END"),
      len(predictions_dict["predicted_tokens"]))

def get_scores(predictions_dict):
  """Returns the attention scores, sliced by source and target length.
  """
  prediction_len = get_prediction_length(predictions_dict)
  source_len = predictions_dict["features.source_len"]
  return predictions_dict["attention_scores"][:prediction_len, :source_len]

def create_figure(predictions_dict):
  """Creates an returns a new figure that visualizes
  attention scors for for a single model predictions.
  """

  # Find out how long the predicted sequence is
  target_words = list(predictions_dict["predicted_tokens"])

  prediction_len = get_prediction_length(predictions_dict)

  # Get source words
  source_len = predictions_dict["features.source_len"]
  source_words = predictions_dict["features.source_tokens"][:source_len]

  # Plot
  fig = plt.figure(figsize=(8, 8))
  plt.imshow(
      X=predictions_dict["attention_scores"][:prediction_len, :source_len],
      interpolation="nearest",
      cmap=plt.cm.Blues)
  plt.xticks(np.arange(source_len), source_words, rotation=45)
  plt.yticks(np.arange(prediction_len), target_words, rotation=-45)
  fig.tight_layout()

  return fig

def main(_argv):
  """Program entrypoint.
  """

  gfile.MakeDirs(FLAGS.output_dir)

  predictions, _, _ = create_inference_graph(
      model_class=FLAGS.model,
      model_dir=FLAGS.model_dir,
      vocab_source=FLAGS.vocab_source,
      vocab_target=FLAGS.vocab_target,
      input_file=FLAGS.source,
      batch_size=FLAGS.batch_size,
      beam_width=FLAGS.beam_width
  )

  # Filter fetched predictions to save memory
  prediction_keys = set(["predicted_tokens", "attention_scores",
                         "features.source_len", "features.source_tokens"])
  predictions = {k: v for k, v in predictions.items() if k in prediction_keys}

  saver = tf.train.Saver()

  checkpoint_path = FLAGS.checkpoint_path
  if not checkpoint_path:
    checkpoint_path = tf.train.latest_checkpoint(FLAGS.model_dir)

  with tf.Session() as sess:
    # Initialize variables
    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())
    sess.run(tf.initialize_all_tables())

    # Restore checkpoint
    saver.restore(sess, checkpoint_path)
    tf.logging.info("Restored model from %s", checkpoint_path)

    # Output predictions
    predictions_iter = create_predictions_iter(predictions, sess)

    # Accumulate attention scores in this array.
    # Shape: [num_examples, target_length, input_length]
    attention_scores = []

    for idx, predictions_dict in enumerate(predictions_iter):
      # Deocde...
      predictions_dict["predicted_tokens"] = np.char.decode(
          predictions_dict["predicted_tokens"].astype("S"), "utf-8")
      predictions_dict["features.source_tokens"] = np.char.decode(
          predictions_dict["features.source_tokens"].astype("S"), "utf-8")
      if not FLAGS.no_plot:
        output_path = os.path.join(FLAGS.output_dir, "{:05d}.png".format(idx))
        create_figure(predictions_dict)
        plt.savefig(output_path)
        tf.logging.info("Wrote %s", output_path)
      attention_scores.append(get_scores(predictions_dict))

    scores_path = os.path.join(FLAGS.output_dir, "attention_scores.npz")
    np.savez(scores_path, *attention_scores)

if __name__ == "__main__":
  tf.app.run()
<commit_msg>Close attention figures after saving<commit_after>#! /usr/bin/env python
""" Generates attention score visualizations.
"""

import os

import tensorflow as tf
from tensorflow.python.platform import gfile

import numpy as np
from matplotlib import pyplot as plt

from seq2seq.inference import create_inference_graph, create_predictions_iter

tf.flags.DEFINE_string("source", None, "path to source input data")
tf.flags.DEFINE_string("vocab_source", None, "Path to source vocabulary file")
tf.flags.DEFINE_string("vocab_target", None, "Path to target vocabulary file")
tf.flags.DEFINE_string("model", "AttentionSeq2Seq", "model class")
tf.flags.DEFINE_string("model_dir", None, "directory to load model from")
tf.flags.DEFINE_string("checkpoint_path", None,
                       """Full path to the checkpoint to be loaded. If None,
                       the latest checkpoint in the model dir is used.""")
tf.flags.DEFINE_integer("batch_size", 32, "the train/dev batch size")
tf.flags.DEFINE_integer("beam_width", None,
                        "Use beam search with this beam width for decoding")

tf.flags.DEFINE_string("output_dir", None,
                       "Write all attention plots to this directory")
tf.flags.DEFINE_boolean("no_plot", False,
                        "If set, does not generate attention plots.")

FLAGS = tf.flags.FLAGS
tf.logging.set_verbosity(tf.logging.INFO)

def get_prediction_length(predictions_dict):
  """Returns the length of the prediction based on the index
  of the first SEQUENCE_END token.
  """
  tokens_iter = enumerate(predictions_dict["predicted_tokens"])
  return next(
      ((i + 1) for i, _ in tokens_iter if _ == "SEQUENCE_END"),
      len(predictions_dict["predicted_tokens"]))

def get_scores(predictions_dict):
  """Returns the attention scores, sliced by source and target length.
  """
  prediction_len = get_prediction_length(predictions_dict)
  source_len = predictions_dict["features.source_len"]
  return predictions_dict["attention_scores"][:prediction_len, :source_len]

def create_figure(predictions_dict):
  """Creates an returns a new figure that visualizes
  attention scors for for a single model predictions.
  """

  # Find out how long the predicted sequence is
  target_words = list(predictions_dict["predicted_tokens"])

  prediction_len = get_prediction_length(predictions_dict)

  # Get source words
  source_len = predictions_dict["features.source_len"]
  source_words = predictions_dict["features.source_tokens"][:source_len]

  # Plot
  fig = plt.figure(figsize=(8, 8))
  plt.imshow(
      X=predictions_dict["attention_scores"][:prediction_len, :source_len],
      interpolation="nearest",
      cmap=plt.cm.Blues)
  plt.xticks(np.arange(source_len), source_words, rotation=45)
  plt.yticks(np.arange(prediction_len), target_words, rotation=-45)
  fig.tight_layout()

  return fig

def main(_argv):
  """Program entrypoint.
  """

  gfile.MakeDirs(FLAGS.output_dir)

  predictions, _, _ = create_inference_graph(
      model_class=FLAGS.model,
      model_dir=FLAGS.model_dir,
      vocab_source=FLAGS.vocab_source,
      vocab_target=FLAGS.vocab_target,
      input_file=FLAGS.source,
      batch_size=FLAGS.batch_size,
      beam_width=FLAGS.beam_width
  )

  # Filter fetched predictions to save memory
  prediction_keys = set(["predicted_tokens", "attention_scores",
                         "features.source_len", "features.source_tokens"])
  predictions = {k: v for k, v in predictions.items() if k in prediction_keys}

  saver = tf.train.Saver()

  checkpoint_path = FLAGS.checkpoint_path
  if not checkpoint_path:
    checkpoint_path = tf.train.latest_checkpoint(FLAGS.model_dir)

  with tf.Session() as sess:
    # Initialize variables
    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())
    sess.run(tf.initialize_all_tables())

    # Restore checkpoint
    saver.restore(sess, checkpoint_path)
    tf.logging.info("Restored model from %s", checkpoint_path)

    # Output predictions
    predictions_iter = create_predictions_iter(predictions, sess)

    # Accumulate attention scores in this array.
    # Shape: [num_examples, target_length, input_length]
    attention_scores = []

    for idx, predictions_dict in enumerate(predictions_iter):
      # Deocde...
      predictions_dict["predicted_tokens"] = np.char.decode(
          predictions_dict["predicted_tokens"].astype("S"), "utf-8")
      predictions_dict["features.source_tokens"] = np.char.decode(
          predictions_dict["features.source_tokens"].astype("S"), "utf-8")
      if not FLAGS.no_plot:
        output_path = os.path.join(FLAGS.output_dir, "{:05d}.png".format(idx))
        create_figure(predictions_dict)
        plt.savefig(output_path)
        plt.close()
        tf.logging.info("Wrote %s", output_path)
      attention_scores.append(get_scores(predictions_dict))

    scores_path = os.path.join(FLAGS.output_dir, "attention_scores.npz")
    np.savez(scores_path, *attention_scores)

if __name__ == "__main__":
  tf.app.run()
<|endoftext|>