<commit_before># util.py
# Copyright (C) 2005,2006 Michael Bayer mike_mp@zzzcomputing.com
#
# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

__all__ = ['OrderedProperties', 'OrderedDict', 'generic_repr', 'HashSet', 'AttrProp']
import thread, weakref, UserList,string, inspect
from exceptions import *

def to_list(x):
    if x is None:
        return None
    if not isinstance(x, list) and not isinstance(x, tuple):
        return [x]
    else:
        return x

def to_set(x):
    if x is None:
        return HashSet()
    if not isinstance(x, HashSet):
        return HashSet(to_list(x))
    else:
        return x

class AttrProp(object):
    """a quick way to stick a property accessor on an object"""
    def __init__(self, key):
        self.key = key
    def __set__(self, obj, value):
        setattr(obj, self.key, value)
    def __delete__(self, obj):
        delattr(obj, self.key)
    def __get__(self, obj, owner):
        if obj is None:
            return self
        else:
            return getattr(obj, self.key)

def generic_repr(obj, exclude=None):
    L = ['%s=%s' % (a, repr(getattr(obj, a))) for a in dir(obj) if not callable(getattr(obj, a)) and not a.startswith('_') and (exclude is None or not exclude.has_key(a))]
    return '%s(%s)' % (obj.__class__.__name__, ','.join(L))

def hash_key(obj):
    if obj is None:
        return 'None'
    elif isinstance(obj, list):
        return repr([hash_key(o) for o in obj])
    elif hasattr(obj, 'hash_key'):
        return obj.hash_key()
    else:
        return repr(obj)

class OrderedProperties(object):
    """an object that maintains the order in which attributes are set upon it.
    also provides an iterator and a very basic dictionary interface to those attributes.
    """
    def __init__(self):
        self.__dict__['_list'] = []
    def keys(self):
        return self._list
    def get(self, key, default):
        return getattr(self, key, default)
    def has_key(self, key):
        return hasattr(self, key)
    def __iter__(self):
        return iter([self[x] for x in self._list])
    def __setitem__(self, key, object):
        setattr(self, key, object)
    def __getitem__(self, key):
        try:
          return getattr(self, key)
        except AttributeError:
          raise KeyError(key)
    def __delitem__(self, key):
        delattr(self, key)
        del self._list[self._list.index(key)]
    def __setattr__(self, key, object):
        if not hasattr(self, key):
            self._list.append(key)
        self.__dict__[key] = object
    def clear(self):
        for key in self._list[:]:
            del self[key]
class RecursionStack(object):
    """a thread-local stack used to detect recursive object traversals."""
    def __init__(self):
        self.stacks = {}
    def _get_stack(self):
        try:
            stack = self.stacks[thread.get_ident()]
        except KeyError:
            stack = {}
            self.stacks[thread.get_ident()] = stack
        return stack
    def push(self, obj):
        s = self._get_stack()
        if s.has_key(obj):
            return True
        else:
            s[obj] = True
            return False
    def pop(self, obj):
        stack = self._get_stack()
        del stack[obj]
        if len(stack) == 0:
            del self.stacks[thread.get_ident()]

class OrderedDict(dict):
    """A Dictionary that keeps its own internal ordering"""
    def __init__(self, values = None):
        self.list = []
        if values is not None:
            for val in values:
                self.update(val)

    def keys(self):
        return self.list

    def clear(self):
        self.list = []
        dict.clear(self)

    def update(self, dict):
        for key in dict.keys():
            self.__setitem__(key, dict[key])

    def setdefault(self, key, value):
        if not self.has_key(key):
            self.__setitem__(key, value)
            return value
        else:
            return self.__getitem__(key)

    def values(self):
        return map(lambda key: self[key], self.list)

    def __iter__(self):
        return iter(self.list)

    def itervalues(self):
        return iter([self[key] for key in self.list])

    def iterkeys(self):return self.__iter__()

    def iteritems(self):
        return iter([(key, self[key]) for key in self.keys()])

    def __delitem__(self, key):
        try:
            del self.list[self.list.index(key)]
        except ValueError:
            raise KeyError(key)
        dict.__delitem__(self, key)

    def __setitem__(self, key, object):
        if not self.has_key(key):
            self.list.append(key)
        dict.__setitem__(self, key, object)

    def __getitem__(self, key):
        return dict.__getitem__(self, key)

class ThreadLocal(object):
    """an object in which attribute access occurs only within the context of the current thread"""
    def __init__(self, raiseerror = True):
        self.__dict__['_tdict'] = {}
        self.__dict__['_raiseerror'] = raiseerror
    def __hasattr__(self, key):
        return self._tdict.has_key("%d_%s" % (thread.get_ident(), key))
    def __delattr__(self, key):
        try:
            del self._tdict["%d_%s" % (thread.get_ident(), key)]
        except KeyError:
            raise AttributeError(key)
    def __getattr__(self, key):
        try:
            return self._tdict["%d_%s" % (thread.get_ident(), key)]
        except KeyError:
            if self._raiseerror:
                raise AttributeError(key)
            else:
                return None
    def __setattr__(self, key, value):
        self._tdict["%d_%s" % (thread.get_ident(), key)] = value

class DictDecorator(dict):
    def __init__(self, decorate):
        self.decorate = decorate
    def __getitem__(self, key):
        try:
            return dict.__getitem__(self, key)
        except KeyError:
            return self.decorate[key]
class HashSet(object):
    """implements a Set."""
    def __init__(self, iter=None, ordered=False):
        if ordered:
            self.map = OrderedDict()
        else:
            self.map  = {}
        if iter is not None:
            for i in iter:
                self.append(i)
    def __iter__(self):
        return iter(self.map.values())
    def contains(self, item):
        return self.map.has_key(item)
    def clear(self):
        self.map.clear()
    def empty(self):
        return len(self.map) == 0
    def append(self, item):
        self.map[item] = item
    def remove(self, item):
        del self.map[item]
    def __add__(self, other):
        return HashSet(self.map.values() + [i for i in other])
    def __len__(self):
        return len(self.map)
    def __delitem__(self, key):
        del self.map[key]
    def __getitem__(self, key):
        return self.map[key]
    def __repr__(self):
        return repr(self.map.values())

class HistoryArraySet(UserList.UserList):
    """extends a UserList to provide unique-set functionality as well as history-aware
    functionality, including information about what list elements were modified
    and commit/rollback capability.  When a HistoryArraySet is created with or
    without initial data, it is in a "committed" state.  as soon as changes are made
    to the list via the normal list-based access, it tracks "added" and "deleted" items,
    which remain until the history is committed or rolled back."""
    def __init__(self, data = None, readonly=False):
        # stores the array's items as keys, and a value of True, False or None indicating
        # added, deleted, or unchanged for that item
        self.records = OrderedDict()
        if data is not None:
            self.data = data
            for item in data:
                # add items without triggering any change events
                # *ASSUME* the list is unique already.  might want to change this.
                self.records[item] = None
        else:
            self.data = []
        self.readonly=readonly
    def __getattr__(self, attr):
        """proxies unknown HistoryArraySet methods and attributes to the underlying
        data array.  this allows custom list classes to be used."""
        return getattr(self.data, attr)
    def set_data(self, data):
        """sets the data for this HistoryArraySet to be that of the given data.
        duplicates in the incoming list will be removed."""
        # first mark everything current as "deleted"
        for i in self.data:
            self.records[i] = False

        # switch array
        self.data = data

        # TODO: fix this up, remove items from array while iterating
        for i in range(0, len(self.data)):
            if not self._setrecord(self.data[i]):
               del self.data[i]
               i -= 1
    def history_contains(self, obj):
        """returns true if the given object exists within the history
        for this HistoryArrayList."""
        return self.records.has_key(obj)
    def __hash__(self):
        return id(self)
    def _setrecord(self, item):
        if self.readonly:
            raise InvalidRequestError("This list is read only")
        try:
            val = self.records[item]
            if val is True or val is None:
                return False
            else:
                self.records[item] = None
                return True
        except KeyError:
            self.records[item] = True
            return True
    def _delrecord(self, item):
        if self.readonly:
            raise InvalidRequestError("This list is read only")
        try:
            val = self.records[item]
            if val is None:
                self.records[item] = False
                return True
            elif val is True:
                del self.records[item]
                return True
            return False
        except KeyError:
            return False
    def commit(self):
        """commits the added values in this list to be the new "unchanged" values.
        values that have been marked as deleted are removed from the history."""
        for key in self.records.keys():
            value = self.records[key]
            if value is False:
                del self.records[key]
            else:
                self.records[key] = None
    def rollback(self):
        """rolls back changes to this list to the last "committed" state."""
        # TODO: speed this up
        list = []
        for key, status in self.records.iteritems():
            if status is False or status is None:
                list.append(key)
        self.data[:] = []
        self.records = {}
        for l in list:
            self.append_nohistory(l)
    def added_items(self):
        """returns a list of items that have been added since the last "committed" state."""
        return [key for key in self.data if self.records[key] is True]
    def deleted_items(self):
        """returns a list of items that have been deleted since the last "committed" state."""
        return [key for key, value in self.records.iteritems() if value is False]
    def unchanged_items(self):
        """returns a list of items that have not been changed since the last "committed" state."""
        return [key for key in self.data if self.records[key] is None]
    def append_nohistory(self, item):
        """appends an item to the list without affecting the "history"."""
        if not self.records.has_key(item):
            self.records[item] = None
            self.data.append(item)
    def remove_nohistory(self, item):
        """removes an item from the list without affecting the "history"."""
        if self.records.has_key(item):
            del self.records[item]
            self.data.remove(item)
    def has_item(self, item):
        return self.records.has_key(item)
    def __setitem__(self, i, item):
        if self._setrecord(a):
            self.data[i] = item
    def __delitem__(self, i):
        self._delrecord(self.data[i])
        del self.data[i]
    def __setslice__(self, i, j, other):
        i = max(i, 0); j = max(j, 0)
        if isinstance(other, UserList.UserList):
            l = other.data
        elif isinstance(other, type(self.data)):
            l = other
        else:
            l = list(other)
        g = [a for a in l if self._setrecord(a)]
        self.data[i:] = g
    def __delslice__(self, i, j):
        i = max(i, 0); j = max(j, 0)
        for a in self.data[i:j]:
            self._delrecord(a)
        del self.data[i:j]
    def append(self, item):
        if self._setrecord(item):
            self.data.append(item)
    def insert(self, i, item):
        if self._setrecord(item):
            self.data.insert(i, item)
    def pop(self, i=-1):
        item = self.data[i]
        if self._delrecord(item):
            return self.data.pop(i)
    def remove(self, item):
        if self._delrecord(item):
            self.data.remove(item)
    def __add__(self, other):
        raise NotImplementedError()
    def __radd__(self, other):
        raise NotImplementedError()
    def __iadd__(self, other):
        raise NotImplementedError()

class ScopedRegistry(object):
    """a Registry that can store one or multiple instances of a single class
    on a per-thread scoped basis, or on a customized scope

    createfunc - a callable that returns a new object to be placed in the registry
    scopefunc - a callable that will return a key to store/retrieve an object,
    defaults to thread.get_ident for thread-local objects.  use a value like
    lambda: True for application scope.
    """
    def __init__(self, createfunc, scopefunc=None):
        self.createfunc = createfunc
        if scopefunc is None:
            self.scopefunc = thread.get_ident
        else:
            self.scopefunc = scopefunc
        self.registry = {}
    def __call__(self):
        key = self._get_key()
        try:
            return self.registry[key]
        except KeyError:
            return self.registry.setdefault(key, self.createfunc())
    def set(self, obj):
        self.registry[self._get_key()] = obj
    def clear(self):
        try:
            del self.registry[self._get_key()]
        except KeyError:
            pass
    def _get_key(self):
        return self.scopefunc()

def constructor_args(instance, **kwargs):
    classobj = instance.__class__

    argspec = inspect.getargspec(classobj.__init__.im_func)

    argnames = argspec[0] or []
    defaultvalues = argspec[3] or []

    (requiredargs, namedargs) = (
            argnames[0:len(argnames) - len(defaultvalues)],
            argnames[len(argnames) - len(defaultvalues):]
            )

    newparams = {}

    for arg in requiredargs:
        if arg == 'self':
            continue
        elif kwargs.has_key(arg):
            newparams[arg] = kwargs[arg]
        else:
            newparams[arg] = getattr(instance, arg)

    for arg in namedargs:
        if kwargs.has_key(arg):
            newparams[arg] = kwargs[arg]
        else:
            if hasattr(instance, arg):
                newparams[arg] = getattr(instance, arg)
            else:
                raise AssertionError("instance has no attribute '%s'" % arg)

    return newparams
    <commit_msg>small cleanup courtesy j.ellis<commit_after># util.py
# Copyright (C) 2005,2006 Michael Bayer mike_mp@zzzcomputing.com
#
# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

__all__ = ['OrderedProperties', 'OrderedDict', 'generic_repr', 'HashSet', 'AttrProp']
import thread, weakref, UserList,string, inspect
from exceptions import *

def to_list(x):
    if x is None:
        return None
    if not isinstance(x, list) and not isinstance(x, tuple):
        return [x]
    else:
        return x

def to_set(x):
    if x is None:
        return HashSet()
    if not isinstance(x, HashSet):
        return HashSet(to_list(x))
    else:
        return x

class AttrProp(object):
    """a quick way to stick a property accessor on an object"""
    def __init__(self, key):
        self.key = key
    def __set__(self, obj, value):
        setattr(obj, self.key, value)
    def __delete__(self, obj):
        delattr(obj, self.key)
    def __get__(self, obj, owner):
        if obj is None:
            return self
        else:
            return getattr(obj, self.key)

def generic_repr(obj, exclude=None):
    L = ['%s=%s' % (a, repr(getattr(obj, a))) for a in dir(obj) if not callable(getattr(obj, a)) and not a.startswith('_') and (exclude is None or not exclude.has_key(a))]
    return '%s(%s)' % (obj.__class__.__name__, ','.join(L))

def hash_key(obj):
    if obj is None:
        return 'None'
    elif isinstance(obj, list):
        return repr([hash_key(o) for o in obj])
    elif hasattr(obj, 'hash_key'):
        return obj.hash_key()
    else:
        return repr(obj)

class OrderedProperties(object):
    """
    An object that maintains the order in which attributes are set upon it.
    also provides an iterator and a very basic getitem/setitem interface to those attributes.

    (Not really a dict, since it iterates over values, not keys.  Not really
    a list, either, since each value must have a key associated; hence there is
    no append or extend.)
    """
    def __init__(self):
        self.__dict__['_list'] = []
    def __len__(self):
        return len(self._list)
    def keys(self):
        return list(self._list)
    def get(self, key, default):
        return getattr(self, key, default)
    def has_key(self, key):
        return hasattr(self, key)
    def __iter__(self):
        return iter([self[x] for x in self._list])
    def __setitem__(self, key, object):
        setattr(self, key, object)
    def __getitem__(self, key):
        try:
          return getattr(self, key)
        except AttributeError:
          raise KeyError(key)
    def __delitem__(self, key):
        delattr(self, key)
        del self._list[self._list.index(key)]
    def __setattr__(self, key, object):
        if not hasattr(self, key):
            self._list.append(key)
        self.__dict__[key] = object
    def clear(self):
        self.__dict__.clear()
        self.__dict__['_list'] = []

class RecursionStack(object):
    """a thread-local stack used to detect recursive object traversals."""
    def __init__(self):
        self.stacks = {}
    def _get_stack(self):
        try:
            stack = self.stacks[thread.get_ident()]
        except KeyError:
            stack = {}
            self.stacks[thread.get_ident()] = stack
        return stack
    def push(self, obj):
        s = self._get_stack()
        if s.has_key(obj):
            return True
        else:
            s[obj] = True
            return False
    def pop(self, obj):
        stack = self._get_stack()
        del stack[obj]
        if len(stack) == 0:
            del self.stacks[thread.get_ident()]

class OrderedDict(dict):
    """A Dictionary that keeps its own internal ordering"""

    def __init__(self, values = None):
        self._list = []
        if values is not None:
            for val in values:
                self.update(val)

    def keys(self):
        return list(self._list)

    def clear(self):
        self._list = []
        dict.clear(self)

    def update(self, dict):
        for key in dict.keys():
            self.__setitem__(key, dict[key])

    def setdefault(self, key, value):
        if not self.has_key(key):
            self.__setitem__(key, value)
            return value
        else:
            return self.__getitem__(key)

    def values(self):
        return map(lambda key: self[key], self._list)

    def __iter__(self):
        return iter(self._list)

    def itervalues(self):
        return iter([self[key] for key in self._list])

    def iterkeys(self): return self.__iter__()

    def iteritems(self):
        return iter([(key, self[key]) for key in self.keys()])

    def __delitem__(self, key):
        try:
            del self._list[self._list.index(key)]
        except ValueError:
            raise KeyError(key)
        dict.__delitem__(self, key)

    def __setitem__(self, key, object):
        if not self.has_key(key):
            self._list.append(key)
        dict.__setitem__(self, key, object)

    def __getitem__(self, key):
        return dict.__getitem__(self, key)

class ThreadLocal(object):
    """an object in which attribute access occurs only within the context of the current thread"""
    def __init__(self, raiseerror = True):
        self.__dict__['_tdict'] = {}
        self.__dict__['_raiseerror'] = raiseerror
    def __hasattr__(self, key):
        return self._tdict.has_key("%d_%s" % (thread.get_ident(), key))
    def __delattr__(self, key):
        try:
            del self._tdict["%d_%s" % (thread.get_ident(), key)]
        except KeyError:
            raise AttributeError(key)
    def __getattr__(self, key):
        try:
            return self._tdict["%d_%s" % (thread.get_ident(), key)]
        except KeyError:
            if self._raiseerror:
                raise AttributeError(key)
            else:
                return None
    def __setattr__(self, key, value):
        self._tdict["%d_%s" % (thread.get_ident(), key)] = value

class DictDecorator(dict):
    def __init__(self, decorate):
        self.decorate = decorate
    def __getitem__(self, key):
        try:
            return dict.__getitem__(self, key)
        except KeyError:
            return self.decorate[key]
class HashSet(object):
    """implements a Set."""
    def __init__(self, iter=None, ordered=False):
        if ordered:
            self.map = OrderedDict()
        else:
            self.map  = {}
        if iter is not None:
            for i in iter:
                self.append(i)
    def __iter__(self):
        return iter(self.map.values())
    def contains(self, item):
        return self.map.has_key(item)
    def clear(self):
        self.map.clear()
    def empty(self):
        return len(self.map) == 0
    def append(self, item):
        self.map[item] = item
    def remove(self, item):
        del self.map[item]
    def __add__(self, other):
        return HashSet(self.map.values() + [i for i in other])
    def __len__(self):
        return len(self.map)
    def __delitem__(self, key):
        del self.map[key]
    def __getitem__(self, key):
        return self.map[key]
    def __repr__(self):
        return repr(self.map.values())

class HistoryArraySet(UserList.UserList):
    """extends a UserList to provide unique-set functionality as well as history-aware
    functionality, including information about what list elements were modified
    and commit/rollback capability.  When a HistoryArraySet is created with or
    without initial data, it is in a "committed" state.  as soon as changes are made
    to the list via the normal list-based access, it tracks "added" and "deleted" items,
    which remain until the history is committed or rolled back."""
    def __init__(self, data = None, readonly=False):
        # stores the array's items as keys, and a value of True, False or None indicating
        # added, deleted, or unchanged for that item
        self.records = OrderedDict()
        if data is not None:
            self.data = data
            for item in data:
                # add items without triggering any change events
                # *ASSUME* the list is unique already.  might want to change this.
                self.records[item] = None
        else:
            self.data = []
        self.readonly=readonly
    def __getattr__(self, attr):
        """proxies unknown HistoryArraySet methods and attributes to the underlying
        data array.  this allows custom list classes to be used."""
        return getattr(self.data, attr)
    def set_data(self, data):
        """sets the data for this HistoryArraySet to be that of the given data.
        duplicates in the incoming list will be removed."""
        # first mark everything current as "deleted"
        for i in self.data:
            self.records[i] = False

        # switch array
        self.data = data

        # TODO: fix this up, remove items from array while iterating
        for i in range(0, len(self.data)):
            if not self._setrecord(self.data[i]):
               del self.data[i]
               i -= 1
    def history_contains(self, obj):
        """returns true if the given object exists within the history
        for this HistoryArrayList."""
        return self.records.has_key(obj)
    def __hash__(self):
        return id(self)
    def _setrecord(self, item):
        if self.readonly:
            raise InvalidRequestError("This list is read only")
        try:
            val = self.records[item]
            if val is True or val is None:
                return False
            else:
                self.records[item] = None
                return True
        except KeyError:
            self.records[item] = True
            return True
    def _delrecord(self, item):
        if self.readonly:
            raise InvalidRequestError("This list is read only")
        try:
            val = self.records[item]
            if val is None:
                self.records[item] = False
                return True
            elif val is True:
                del self.records[item]
                return True
            return False
        except KeyError:
            return False
    def commit(self):
        """commits the added values in this list to be the new "unchanged" values.
        values that have been marked as deleted are removed from the history."""
        for key in self.records.keys():
            value = self.records[key]
            if value is False:
                del self.records[key]
            else:
                self.records[key] = None
    def rollback(self):
        """rolls back changes to this list to the last "committed" state."""
        # TODO: speed this up
        list = []
        for key, status in self.records.iteritems():
            if status is False or status is None:
                list.append(key)
        self.data[:] = []
        self.records = {}
        for l in list:
            self.append_nohistory(l)
    def added_items(self):
        """returns a list of items that have been added since the last "committed" state."""
        return [key for key in self.data if self.records[key] is True]
    def deleted_items(self):
        """returns a list of items that have been deleted since the last "committed" state."""
        return [key for key, value in self.records.iteritems() if value is False]
    def unchanged_items(self):
        """returns a list of items that have not been changed since the last "committed" state."""
        return [key for key in self.data if self.records[key] is None]
    def append_nohistory(self, item):
        """appends an item to the list without affecting the "history"."""
        if not self.records.has_key(item):
            self.records[item] = None
            self.data.append(item)
    def remove_nohistory(self, item):
        """removes an item from the list without affecting the "history"."""
        if self.records.has_key(item):
            del self.records[item]
            self.data.remove(item)
    def has_item(self, item):
        return self.records.has_key(item)
    def __setitem__(self, i, item):
        if self._setrecord(a):
            self.data[i] = item
    def __delitem__(self, i):
        self._delrecord(self.data[i])
        del self.data[i]
    def __setslice__(self, i, j, other):
        i = max(i, 0); j = max(j, 0)
        if isinstance(other, UserList.UserList):
            l = other.data
        elif isinstance(other, type(self.data)):
            l = other
        else:
            l = list(other)
        g = [a for a in l if self._setrecord(a)]
        self.data[i:] = g
    def __delslice__(self, i, j):
        i = max(i, 0); j = max(j, 0)
        for a in self.data[i:j]:
            self._delrecord(a)
        del self.data[i:j]
    def append(self, item):
        if self._setrecord(item):
            self.data.append(item)
    def insert(self, i, item):
        if self._setrecord(item):
            self.data.insert(i, item)
    def pop(self, i=-1):
        item = self.data[i]
        if self._delrecord(item):
            return self.data.pop(i)
    def remove(self, item):
        if self._delrecord(item):
            self.data.remove(item)
    def __add__(self, other):
        raise NotImplementedError()
    def __radd__(self, other):
        raise NotImplementedError()
    def __iadd__(self, other):
        raise NotImplementedError()

class ScopedRegistry(object):
    """a Registry that can store one or multiple instances of a single class
    on a per-thread scoped basis, or on a customized scope

    createfunc - a callable that returns a new object to be placed in the registry
    scopefunc - a callable that will return a key to store/retrieve an object,
    defaults to thread.get_ident for thread-local objects.  use a value like
    lambda: True for application scope.
    """
    def __init__(self, createfunc, scopefunc=None):
        self.createfunc = createfunc
        if scopefunc is None:
            self.scopefunc = thread.get_ident
        else:
            self.scopefunc = scopefunc
        self.registry = {}
    def __call__(self):
        key = self._get_key()
        try:
            return self.registry[key]
        except KeyError:
            return self.registry.setdefault(key, self.createfunc())
    def set(self, obj):
        self.registry[self._get_key()] = obj
    def clear(self):
        try:
            del self.registry[self._get_key()]
        except KeyError:
            pass
    def _get_key(self):
        return self.scopefunc()

def constructor_args(instance, **kwargs):
    classobj = instance.__class__

    argspec = inspect.getargspec(classobj.__init__.im_func)

    argnames = argspec[0] or []
    defaultvalues = argspec[3] or []

    (requiredargs, namedargs) = (
            argnames[0:len(argnames) - len(defaultvalues)],
            argnames[len(argnames) - len(defaultvalues):]
            )

    newparams = {}

    for arg in requiredargs:
        if arg == 'self':
            continue
        elif kwargs.has_key(arg):
            newparams[arg] = kwargs[arg]
        else:
            newparams[arg] = getattr(instance, arg)

    for arg in namedargs:
        if kwargs.has_key(arg):
            newparams[arg] = kwargs[arg]
        else:
            if hasattr(instance, arg):
                newparams[arg] = getattr(instance, arg)
            else:
                raise AssertionError("instance has no attribute '%s'" % arg)

    return newparams
    <|endoftext|>


<commit_before>import tempfile
import shutil
import os.path

from project.internal.makedirs import makedirs_ok_if_exists
from project.local_state_file import LocalStateFile

local_tmp = os.path.abspath("./build/tmp")
makedirs_ok_if_exists(local_tmp)

class TmpDir(object):
    def __init__(self, prefix):
        self._dir = tempfile.mkdtemp(prefix=prefix, dir=local_tmp)

    def __exit__(self, type, value, traceback):
        shutil.rmtree(path=self._dir)

    def __enter__(self):
        return self._dir

def with_directory_contents(contents, func):
    with (TmpDir(prefix="project-test-tmpdir-")) as dirname:
        for filename, file_content in contents.items():
            path = os.path.join(dirname, filename)
            makedirs_ok_if_exists(os.path.dirname(path))
            f = open(path, 'w')
            f.write(file_content)
            f.flush()
            f.close()
        func(os.path.realpath(dirname))

def with_temporary_file(func, dir=None):
    if dir is None:
        dir = local_tmp
    import tempfile
    f = tempfile.NamedTemporaryFile(dir=dir)
    try:
        func(f)
    finally:
        f.close()

def with_file_contents(contents, func, dir=None):
    def with_file_object(f):
        f.write(contents.encode("UTF-8"))
        f.flush()
        func(f.name)

    with_temporary_file(with_file_object, dir=dir)

def tmp_local_state_file():
    import tempfile
    f = tempfile.NamedTemporaryFile(dir=local_tmp)
    local_state = LocalStateFile(f.name)
    f.close()
    return local_state
<commit_msg>To further shorten our tmp directory names, shorten the prefix<commit_after>import tempfile
import shutil
import os.path

from project.internal.makedirs import makedirs_ok_if_exists
from project.local_state_file import LocalStateFile

local_tmp = os.path.abspath("./build/tmp")
makedirs_ok_if_exists(local_tmp)

class TmpDir(object):
    def __init__(self, prefix):
        self._dir = tempfile.mkdtemp(prefix=prefix, dir=local_tmp)

    def __exit__(self, type, value, traceback):
        shutil.rmtree(path=self._dir)

    def __enter__(self):
        return self._dir

def with_directory_contents(contents, func):
    with (TmpDir(prefix="test-")) as dirname:
        for filename, file_content in contents.items():
            path = os.path.join(dirname, filename)
            makedirs_ok_if_exists(os.path.dirname(path))
            f = open(path, 'w')
            f.write(file_content)
            f.flush()
            f.close()
        func(os.path.realpath(dirname))

def with_temporary_file(func, dir=None):
    if dir is None:
        dir = local_tmp
    import tempfile
    f = tempfile.NamedTemporaryFile(dir=dir)
    try:
        func(f)
    finally:
        f.close()

def with_file_contents(contents, func, dir=None):
    def with_file_object(f):
        f.write(contents.encode("UTF-8"))
        f.flush()
        func(f.name)

    with_temporary_file(with_file_object, dir=dir)

def tmp_local_state_file():
    import tempfile
    f = tempfile.NamedTemporaryFile(dir=local_tmp)
    local_state = LocalStateFile(f.name)
    f.close()
    return local_state
<|endoftext|>


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

import DataHolder
import PythonSerializer

def serialize(holder):
    return PythonSerializer.PythonSerializer().serialize_string(holder, no_prolog = True)

def test_includes_rendering():
    holder = DataHolder.DataHolder()
    holder.includes = ["szydlo", "mydlo", "powidlo"]

    ref = """\
from szydlo import *
from mydlo import *
from powidlo import *
"""
    assert ref == serialize(holder)

def test_typedefs_rendering():
    holder = DataHolder.DataHolder()
    holder.typedefs = [("a", "b")]

    ref = """\
a = b
"""
    assert ref == serialize(holder)

def test_typedefs_rendering_with_changed_enum_order():
    holder = DataHolder.DataHolder()
    holder.typedefs = [("TEnum2", "EEnum2")]
    holder.enums = [("EEnum1", [("EEnum1_1", "EEnum1_Val")]),
                    ("EEnum2", [("EEnum2_2", "EEnum2_Val")])]

    ref = """\
class EEnum2(prophy.enum):
    __metaclass__ = prophy.enum_generator
    _enumerators  = [('EEnum2_2', EEnum2_Val)]

EEnum2_2 = EEnum2_Val

TEnum2 = EEnum2

class EEnum1(prophy.enum):
    __metaclass__ = prophy.enum_generator
    _enumerators  = [('EEnum1_1', EEnum1_Val)]

EEnum1_1 = EEnum1_Val
"""
    assert ref == serialize(holder)

def test_typedefs_rendering_with_changed_struct_order():
    holder = DataHolder.DataHolder()
    holder.typedefs = [("TStruct2", "SStruct2")]

    msg1 = DataHolder.MessageHolder()
    msg1.name = "SStruct1"
    msg1.add_to_list(DataHolder.MemberHolder('x', 'u32'))

    msg2 = DataHolder.MessageHolder()
    msg2.name = "SStruct2"
    msg2.add_to_list(DataHolder.MemberHolder('y', 'i32'))

    holder.struct_list = [msg1, msg2]

    ref = """\
class SStruct2(prophy.struct):
    __metaclass__ = prophy.struct_generator
    _descriptor = [('y',prophy.i32)]

TStruct2 = SStruct2

class SStruct1(prophy.struct):
    __metaclass__ = prophy.struct_generator
    _descriptor = [('x',prophy.u32)]
"""
    assert ref == serialize(holder)

def test_enums_rendering():
    holder = DataHolder.DataHolder()
    holder.enums = [("EEnum", [("EEnum_A", "0"), ("EEnum_B", "1"), ("EEnum_C", "2")])]

    ref = """\
class EEnum(prophy.enum):
    __metaclass__ = prophy.enum_generator
    _enumerators  = [('EEnum_A', 0),
                     ('EEnum_B', 1),
                     ('EEnum_C', 2)]

EEnum_A = 0
EEnum_B = 1
EEnum_C = 2
"""
    assert ref == serialize(holder)

""" FIXME kl. this test is way too large. It needs to be split to multiple tests """
def test_of_PythonSerializer():
    ih = []
    th = []
    for x in range(20, 200, 60):
        ih.append("test_include_" + str(x))
        th.append(("td_elem_name_" + str(x), "td_elem_val_" + str(x)))
        th.append(("td_elem_name_" + str(x), "i_td_elem_val_" + str(x)))
        th.append(("td_elem_name_" + str(x), "u_td_elem_val_" + str(x)))

    enum = []
    for x in range(1, 100, 30):
        enum.append(("elem_" + str(x), "val_" + str(x)))

    const = DataHolder.ConstantHolder()
    const.add_to_list("C_A", "5")
    const.add_to_list("C_B", "5")
    const.add_to_list("C_C", "C_B + C_A")

    msg_h = DataHolder.MessageHolder()
    msg_h.name = "MAC_L2CallConfigResp"
    msg_h.add_to_list(DataHolder.MemberHolder('messageResult', 'SMessageResult'))

    dh = DataHolder.DataHolder()
    dh.includes = ih
    dh.typedefs = th
    dh.constant = const
    dh.msgs_list = [msg_h]
    dh.enums = [("test", enum)]

    ps = PythonSerializer.PythonSerializer()
    output = ps.serialize_string(dh)

    ref = """\
import prophy

def bitMaskOr(x, y):
    return x | y

def shiftLeft(x, y):
    return x << y

from test_include_20 import *
from test_include_80 import *
from test_include_140 import *

C_B = 5
C_A = 5
C_C = C_B + C_A

td_elem_name_20 = td_elem_val_20
td_elem_name_20 = prophy.i_td_elem_val_20
td_elem_name_20 = prophy.u_td_elem_val_20
td_elem_name_80 = td_elem_val_80
td_elem_name_80 = prophy.i_td_elem_val_80
td_elem_name_80 = prophy.u_td_elem_val_80
td_elem_name_140 = td_elem_val_140
td_elem_name_140 = prophy.i_td_elem_val_140
td_elem_name_140 = prophy.u_td_elem_val_140

class test(prophy.enum):
    __metaclass__ = prophy.enum_generator
    _enumerators  = [('elem_1', val_1),
                     ('elem_31', val_31),
                     ('elem_61', val_61),
                     ('elem_91', val_91)]

elem_1 = val_1
elem_31 = val_31
elem_61 = val_61
elem_91 = val_91

class MAC_L2CallConfigResp(prophy.struct):
    __metaclass__ = prophy.struct_generator
    _descriptor = [('messageResult',SMessageResult)]
"""
    assert ref == output
<commit_msg>test rendering constants<commit_after># -*- coding: utf-8 -*-

import DataHolder
import PythonSerializer

def serialize(holder):
    return PythonSerializer.PythonSerializer().serialize_string(holder, no_prolog = True)

def test_includes_rendering():
    holder = DataHolder.DataHolder()
    holder.includes = ["szydlo", "mydlo", "powidlo"]

    ref = """\
from szydlo import *
from mydlo import *
from powidlo import *
"""
    assert ref == serialize(holder)

def test_typedefs_rendering():
    holder = DataHolder.DataHolder()
    holder.typedefs = [("a", "b")]

    ref = """\
a = b
"""
    assert ref == serialize(holder)

def test_typedefs_rendering_with_changed_enum_order():
    holder = DataHolder.DataHolder()
    holder.typedefs = [("TEnum2", "EEnum2")]
    holder.enums = [("EEnum1", [("EEnum1_1", "EEnum1_Val")]),
                    ("EEnum2", [("EEnum2_2", "EEnum2_Val")])]

    ref = """\
class EEnum2(prophy.enum):
    __metaclass__ = prophy.enum_generator
    _enumerators  = [('EEnum2_2', EEnum2_Val)]

EEnum2_2 = EEnum2_Val

TEnum2 = EEnum2

class EEnum1(prophy.enum):
    __metaclass__ = prophy.enum_generator
    _enumerators  = [('EEnum1_1', EEnum1_Val)]

EEnum1_1 = EEnum1_Val
"""
    assert ref == serialize(holder)

def test_typedefs_rendering_with_changed_struct_order():
    holder = DataHolder.DataHolder()
    holder.typedefs = [("TStruct2", "SStruct2")]

    msg1 = DataHolder.MessageHolder()
    msg1.name = "SStruct1"
    msg1.add_to_list(DataHolder.MemberHolder('x', 'u32'))

    msg2 = DataHolder.MessageHolder()
    msg2.name = "SStruct2"
    msg2.add_to_list(DataHolder.MemberHolder('y', 'i32'))

    holder.struct_list = [msg1, msg2]

    ref = """\
class SStruct2(prophy.struct):
    __metaclass__ = prophy.struct_generator
    _descriptor = [('y',prophy.i32)]

TStruct2 = SStruct2

class SStruct1(prophy.struct):
    __metaclass__ = prophy.struct_generator
    _descriptor = [('x',prophy.u32)]
"""
    assert ref == serialize(holder)

def test_enums_rendering():
    holder = DataHolder.DataHolder()
    holder.enums = [("EEnum", [("EEnum_A", "0"), ("EEnum_B", "1"), ("EEnum_C", "2")])]

    ref = """\
class EEnum(prophy.enum):
    __metaclass__ = prophy.enum_generator
    _enumerators  = [('EEnum_A', 0),
                     ('EEnum_B', 1),
                     ('EEnum_C', 2)]

EEnum_A = 0
EEnum_B = 1
EEnum_C = 2
"""
    assert ref == serialize(holder)

def test_constants_rendering():
    holder = DataHolder.DataHolder()
    holder.constant.list = [("CONST_A", "0"), ("CONST_B", "31")]

    ref = """\
CONST_B = 31
CONST_A = 0
"""
    assert ref == serialize(holder)

""" FIXME kl. this test is way too large. It needs to be split to multiple tests """
def test_of_PythonSerializer():
    ih = []
    th = []
    for x in range(20, 200, 60):
        ih.append("test_include_" + str(x))
        th.append(("td_elem_name_" + str(x), "td_elem_val_" + str(x)))
        th.append(("td_elem_name_" + str(x), "i_td_elem_val_" + str(x)))
        th.append(("td_elem_name_" + str(x), "u_td_elem_val_" + str(x)))

    enum = []
    for x in range(1, 100, 30):
        enum.append(("elem_" + str(x), "val_" + str(x)))

    const = DataHolder.ConstantHolder()
    const.add_to_list("C_A", "5")
    const.add_to_list("C_B", "5")
    const.add_to_list("C_C", "C_B + C_A")

    msg_h = DataHolder.MessageHolder()
    msg_h.name = "MAC_L2CallConfigResp"
    msg_h.add_to_list(DataHolder.MemberHolder('messageResult', 'SMessageResult'))

    dh = DataHolder.DataHolder()
    dh.includes = ih
    dh.typedefs = th
    dh.constant = const
    dh.msgs_list = [msg_h]
    dh.enums = [("test", enum)]

    ps = PythonSerializer.PythonSerializer()
    output = ps.serialize_string(dh)

    ref = """\
import prophy

def bitMaskOr(x, y):
    return x | y

def shiftLeft(x, y):
    return x << y

from test_include_20 import *
from test_include_80 import *
from test_include_140 import *

C_B = 5
C_A = 5
C_C = C_B + C_A

td_elem_name_20 = td_elem_val_20
td_elem_name_20 = prophy.i_td_elem_val_20
td_elem_name_20 = prophy.u_td_elem_val_20
td_elem_name_80 = td_elem_val_80
td_elem_name_80 = prophy.i_td_elem_val_80
td_elem_name_80 = prophy.u_td_elem_val_80
td_elem_name_140 = td_elem_val_140
td_elem_name_140 = prophy.i_td_elem_val_140
td_elem_name_140 = prophy.u_td_elem_val_140

class test(prophy.enum):
    __metaclass__ = prophy.enum_generator
    _enumerators  = [('elem_1', val_1),
                     ('elem_31', val_31),
                     ('elem_61', val_61),
                     ('elem_91', val_91)]

elem_1 = val_1
elem_31 = val_31
elem_61 = val_61
elem_91 = val_91

class MAC_L2CallConfigResp(prophy.struct):
    __metaclass__ = prophy.struct_generator
    _descriptor = [('messageResult',SMessageResult)]
"""
    assert ref == output
<|endoftext|>


<commit_before>from django import template
from django.templatetags.static import static
from django.conf import settings

register = template.Library()

@register.simple_tag
def js_files():

    js_files = []
    if hasattr(settings, 'PROSO_JS_FILES'):
        for f in settings.PROSO_JS_FILES:
            print f, static(f)
            js_files.append('<script src="{}"></script>'.format(static(f)))
    return "\n".join(js_files)
<commit_msg>remove obsolete print<commit_after>from django import template
from django.templatetags.static import static
from django.conf import settings

register = template.Library()

@register.simple_tag
def js_files():

    js_files = []
    if hasattr(settings, 'PROSO_JS_FILES'):
        for f in settings.PROSO_JS_FILES:
            js_files.append('<script src="{}"></script>'.format(static(f)))
    return "\n".join(js_files)
<|endoftext|>


<commit_before>from datetime import datetime, timedelta

from django.http import HttpResponseRedirect, HttpResponse, Http404
from django.shortcuts import get_object_or_404
from django.core.urlresolvers import reverse
from django.contrib import messages
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.views.generic import TemplateView, DetailView, FormView, CreateView, ListView
from django.views.generic.detail import BaseDetailView
from django.conf import settings

from gargoyle import gargoyle

from utils import installed, blacklist_values, check_permissions, send_message
from eve_api.models import EVEAccount, EVEPlayerCorporation, EVEPlayerCharacter
from sso.tasks import update_user_access
from hr.forms import RecommendationForm, ApplicationForm, NoteForm, BlacklistUserForm, AdminNoteForm
from hr.models import Recommendation, Application, Audit, Blacklist, BlacklistSource
from hr.app_defines import *

### General Views

class HrIndexView(TemplateView):
    """
    Gives the main HR index page, with various options displayed depending on their
    access level.
    """

    template_name = 'hr/index.html'

    def get_context_data(self, **kwargs):
        context = super(HrIndexView, self).get_context_data(**kwargs)
        context['hrstaff'] = check_permissions(self.request.user)
        context['can_recommend'] = len(blacklist_values(self.request.user, BLACKLIST_LEVEL_ADVISORY)) == 0
        return context

### Application Management

class HrViewUserApplications(TemplateView):
    """
    Shows a list of the user's applications in the system
    """

    template_name = 'hr/applications/view_list.html'

    def get_context_data(self, **kwargs):
        context = super(HrViewUserApplications, self).get_context_data(**kwargs)
        context['applications'] = Application.objects.filter(user=self.request.user).order_by('id')
        return context

class HrViewApplication(DetailView):
    """
    View a individual application and related details
    """

    template_name = 'hr/applications/view.html'
    context_object_name = "app"
    model = Application
    slug_field = 'id'

    def get_context_data(self, **kwargs):
        context = super(HrViewApplication, self).get_context_data(**kwargs)
        perm = check_permissions(self.request.user, self.object)
        if perm == HR_VIEWONLY:
            context['audit'] = self.object.audit_set.filter(event__in=[AUDIT_EVENT_STATUSCHANGE, AUDIT_EVENT_REJECTION, AUDIT_EVENT_ACCEPTED, AUDIT_EVENT_MESSAGE])
        elif perm == HR_ADMIN:
            context['hrstaff'] = True
            context['audit'] = self.object.audit_set.all()
        return context

class HrAddApplication(FormView):

    form_class = ApplicationForm

    def get_form_kwargs(self, **kwargs):
        kwargs = super(HrAddApplication, self).get_form_kwargs(**kwargs)
        kwargs['user'] = self.request.user
        return kwargs

    def form_valid(self, form):
        app = Application(user=self.request.user, character=form.cleaned_data['character'], corporation=form.cleaned_data['corporation'])
        app.save()
        messages.add_message(self.request, messages.INFO, "Your application to %s has been created." % app.corporation)
        return HttpResponseRedirect(reverse('hr-viewapplication', args=[app.id]))

    def get_template_names(self):
        if len(EVEPlayerCorporation.objects.filter(application_config__is_accepting=True)):
            return 'hr/applications/add.html'
        else:
            return 'hr/applications/noadd.html'

### Recommendation Management

class HrViewRecommendations(TemplateView):
    """
    Shows a list of the user's recommendations in the system
    """

    template_name = 'hr/recommendations/view_list.html'

    def get_context_data(self, **kwargs):
        context = super(HrViewRecommendations, self).get_context_data(**kwargs)
        context['recommendations'] = Recommendation.objects.filter(user=self.request.user)
        return context

class HrAddRecommendation(FormView):

    template_name = 'hr/recommendations/add.html'
    form_class = RecommendationForm

    def dispatch(self, request, *args, **kwargs):
        if len(blacklist_values(request.user, BLACKLIST_LEVEL_ADVISORY)):
            raise Http404
        return super(HrAddRecommendation, self).dispatch(request, *args, **kwargs)

    def form_valid(self, form):
        rec = Recommendation(user=self.request.user)
        rec.user_character = form.cleaned_data['character']
        rec.application = form.cleaned_data['application']
        rec.save()

        messages.add_message(self.request, messages.INFO, "Recommendation added to %s's application" % rec.application )
        return HttpResponseRedirect(reverse('hr-viewrecommendations'))

    def get_form_kwargs(self):
        kwargs = super(HrAddRecommendation, self).get_form_kwargs()
        kwargs['user'] = self.request.user
        return kwargs

class HrAdminApplications(ListView):

    model = Application
    template_name = 'hr/applications/admin/view_list.html'
    context_object_name = 'apps'

    def get_queryset(self):
        if self.request.user.has_perm('hr.can_view_all'):
            apps = Application.objects.all()
        elif self.request.user.has_perm('hr.can_view_corp'):
            apps = Application.objects.filter(corporation__id__in=EVEPlayerCharacter.objects.filter(eveaccount__user=self.request.user).values_list('corporation__id', flat=True))
        else:
            apps = Application.objects.none()

        query = self.request.GET.get('q', None)
        order = self.request.GET.get('o', 'id')

        # Filter by the query string
        if query:
            apps = apps.filter(character__name__icontains=query)
        else:
            apps = apps.filter(status__in=[APPLICATION_STATUS_AWAITINGREVIEW, APPLICATION_STATUS_ACCEPTED, APPLICATION_STATUS_QUERY, APPLICATION_STATUS_FLAGGED])

        # If a invalid order as been passed, correct it
        if not order in ['id', 'corporation__name', 'character__name']:
            order = 'id'

        # If we've got a short search string, only get the first 50
        if query and len(query) < 3:
            apps = apps[:50]

        return apps

class HrUpdateApplication(BaseDetailView):
    """
    Updates the status of a application if the workflow and permissions allow so.
    """
    model = Application
    slug_field = 'id'

    def render_to_response(self, context):
        status = self.kwargs.get('status', None)
        if status and int(status) in APPLICATION_STATUS_ROUTES[self.object.status]:
            perm = check_permissions(self.request.user, self.object)
            if perm == HR_ADMIN or (perm == HR_VIEWONLY and int(status) <= 1):
                if not self.object.status == status:
                    self.object.status = status
                    self.object.save(user=self.request.user)
                    self.object = self.model.objects.get(pk=self.object.pk)
                    messages.add_message(self.request, messages.INFO, "Application %s has been changed to %s" % (self.object.id, self.object.get_status_display()))
        else:
             messages.add_message(self.request, messages.ERROR, "Invalid status change request")
        return HttpResponseRedirect(reverse('hr-viewapplication', args=[self.object.id]))

class HrAddNote(CreateView):
    """
    View to add a note to a application
    """

    template_name = 'hr/applications/add_note.html'
    form_class = NoteForm
    model = Audit

    def dispatch(self, request, *args, **kwargs):
        if not check_permissions(request.user) == HR_ADMIN:
            return HttpResponseRedirect(reverse('hr.views.HrIndexView'))
        self.application = Application.objects.get(pk=kwargs.get('applicationid'))
        return super(HrAddNote, self).dispatch(request, *args, **kwargs)

    def form_valid(self, form):
        if check_permissions(self.request.user, self.application) == HR_ADMIN:
            self.object = form.save(commit=False)
            self.object.event = AUDIT_EVENT_NOTE
            self.object.application = self.application
            self.object.user = self.request.user
            self.object.save()
        return HttpResponseRedirect(self.get_success_url())

    def get_success_url(self):
        return reverse('hr-viewapplication', args=[self.application.id])

    def get_context_data(self, **kwargs):
        context = super(HrAddNote, self).get_context_data(**kwargs)
        context['application'] = self.application
        return context

class HrAddMessage(HrAddNote):

    template_name = 'hr/applications/add_message.html'

    def dispatch(self, request, *args, **kwargs):
        self.application = Application.objects.get(pk=kwargs.get('applicationid'))
        self.perm = check_permissions(request.user, self.application)
        if not self.perm:
            return HttpResponseRedirect(reverse('hr.views.HrIndexView'))
        return super(HrAddMessage, self).dispatch(request, *args, **kwargs)

    def get_form_class(self):
        if self.perm == HR_ADMIN:
            return AdminNoteForm
        else:
            return NoteForm

    def get_form_kwargs(self):
        kwargs = super(HrAddMessage, self).get_form_kwargs()
        if self.perm == HR_ADMIN:
            kwargs['application'] = self.application
        return kwargs

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.application = self.application
        self.object.event = AUDIT_EVENT_MESSAGE
        self.object.user = self.request.user
        self.object.save()
        if not self.application.user == self.request.user:
            try:
                send_message(self.application, 'message', note=self.object.text)
            except:
                pass
        return HttpResponseRedirect(self.get_success_url())

class HrRejectApplication(CreateView):

    template_name = 'hr/applications/reject.html'
    message_template_name = 'rejected'
    form_class = AdminNoteForm
    model = Audit
    application_change_status = APPLICATION_STATUS_REJECTED
    audit_event_type = AUDIT_EVENT_REJECTION

    def dispatch(self, request, *args, **kwargs):
        self.application = get_object_or_404(Application, pk=kwargs.get('applicationid'))
        if not (check_permissions(request.user) == HR_ADMIN and request.user.has_perm('hr.can_accept')):
            return HttpResponseRedirect(reverse('hr-index'))
        return super(HrRejectApplication, self).dispatch(request, *args, **kwargs)

    def get_form_kwargs(self):
        kwargs = super(HrRejectApplication, self).get_form_kwargs()
        kwargs['application'] = self.application
        return kwargs

    def get_context_data(self, **kwargs):
        context = super(HrRejectApplication, self).get_context_data(**kwargs)
        context['application'] = self.application
        return context

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.application = self.application
        self.object.user = self.request.user
        self.object.event = self.audit_event_type
        self.object.save()

        self.object.application.status = self.application_change_status
        self.object.application.save(user=self.request.user)

        try:
            send_message(self.object.application, self.message_template_name, note=self.object.text)
        except:
            pass
        return HttpResponseRedirect(self.get_success_url())

    def get_success_url(self):
        return reverse('hr-viewapplication', args=[self.application.id])

class HrAcceptApplication(HrRejectApplication):

    template_name = 'hr/applications/accept.html'
    message_template_name = 'accepted'
    application_change_status = APPLICATION_STATUS_ACCEPTED
    audit_event_type = AUDIT_EVENT_ACCEPTED

    def dispatch(self, request, *args, **kwargs):
        app = get_object_or_404(Application, pk=kwargs.get('applicationid'))
        if app.blacklisted:
            messages.add_message(request, messages.INFO, "This application has one or more blacklist entries and cannot be accepted.")
            return HttpResponseRedirect(reverse('hr-viewapplication', args=[app.id]))
        return super(HrAcceptApplication, self).dispatch(request, *args, **kwargs)

class HrBlacklistUser(FormView):

    template_name = 'hr/blacklist/blacklist.html'
    form_class = BlacklistUserForm

    def dispatch(self, request, *args, **kwargs):
        self.blacklist_user = get_object_or_404(User, id=kwargs.get('userid'))
        return super(HrBlacklistUser, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(HrBlacklistUser, self).get_context_data(**kwargs)
        context['blacklistuser'] = self.blacklist_user
        return context

    def blacklist_item(type, value):
        Blacklist(type=self.type, value=self.value, level=self.level, source=self.source, expiry_date=self.expiry, created_by=self.request.user, reason=self.reason).save()

    def form_valid(self, form):
        self.source = BlacklistSource.objects.get(id=1)
        self.expiry = form.cleaned_data.get('expiry_date', None)
        if not self.expiry:
            self.expiry = datetime.utcnow() + timedelta(days=50*365) # 50 year default
        self.level = form.cleaned_data.get('level', 0)
        self.reason = form.cleaned_data.get('reason', 'No reason provided')

        # Blacklist email address
        self.blacklist_item(BLACKLIST_TYPE_EMAIL, self.blacklist_user.email)

        # Blacklist API keys
        for account in self.blacklist_user.eveaccount_set.all():
            self.blacklist_item(BLACKLIST_TYPE_APIUSERID, account.api_user_id)

        # Blacklist Characters
        for character in EVEPlayerCharacter.objects.filter(eveaccount__user=self.blacklist_user).distinct():
            self.blacklist_item(BLACKLIST_TYPE_CHARACTER, character.name)

        # Blacklist Reddit accounts
        if installed('reddit'):
            for account in u.redditaccount_set.all():
                self.blacklist_item(BLACKLIST_TYPE_REDDIT, account.username)

        messages.add_message(request, messages.INFO, "User %s has been blacklisted" % u.username )

        # Disable the account if requested
        if form.cleaned_data.get('disable', None):
            self.blacklist_user.active = False
            self.blacklist_user.save()

        update_user_access.delay(user=self.blacklist_user.id)

        messages.add_message(request, messages.INFO, "User %s disabled" % u.username )

        return redirect('sso.views.user_view', username=self.blacklist_user.username)
<commit_msg>Wrap in a set to make it run the Corp List query first, fixing a MySQL fail<commit_after>from datetime import datetime, timedelta

from django.http import HttpResponseRedirect, HttpResponse, Http404
from django.shortcuts import get_object_or_404
from django.core.urlresolvers import reverse
from django.contrib import messages
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.views.generic import TemplateView, DetailView, FormView, CreateView, ListView
from django.views.generic.detail import BaseDetailView
from django.conf import settings

from gargoyle import gargoyle

from utils import installed, blacklist_values, check_permissions, send_message
from eve_api.models import EVEAccount, EVEPlayerCorporation, EVEPlayerCharacter
from sso.tasks import update_user_access
from hr.forms import RecommendationForm, ApplicationForm, NoteForm, BlacklistUserForm, AdminNoteForm
from hr.models import Recommendation, Application, Audit, Blacklist, BlacklistSource
from hr.app_defines import *

### General Views

class HrIndexView(TemplateView):
    """
    Gives the main HR index page, with various options displayed depending on their
    access level.
    """

    template_name = 'hr/index.html'

    def get_context_data(self, **kwargs):
        context = super(HrIndexView, self).get_context_data(**kwargs)
        context['hrstaff'] = check_permissions(self.request.user)
        context['can_recommend'] = len(blacklist_values(self.request.user, BLACKLIST_LEVEL_ADVISORY)) == 0
        return context

### Application Management

class HrViewUserApplications(TemplateView):
    """
    Shows a list of the user's applications in the system
    """

    template_name = 'hr/applications/view_list.html'

    def get_context_data(self, **kwargs):
        context = super(HrViewUserApplications, self).get_context_data(**kwargs)
        context['applications'] = Application.objects.filter(user=self.request.user).order_by('id')
        return context

class HrViewApplication(DetailView):
    """
    View a individual application and related details
    """

    template_name = 'hr/applications/view.html'
    context_object_name = "app"
    model = Application
    slug_field = 'id'

    def get_context_data(self, **kwargs):
        context = super(HrViewApplication, self).get_context_data(**kwargs)
        perm = check_permissions(self.request.user, self.object)
        if perm == HR_VIEWONLY:
            context['audit'] = self.object.audit_set.filter(event__in=[AUDIT_EVENT_STATUSCHANGE, AUDIT_EVENT_REJECTION, AUDIT_EVENT_ACCEPTED, AUDIT_EVENT_MESSAGE])
        elif perm == HR_ADMIN:
            context['hrstaff'] = True
            context['audit'] = self.object.audit_set.all()
        return context

class HrAddApplication(FormView):

    form_class = ApplicationForm

    def get_form_kwargs(self, **kwargs):
        kwargs = super(HrAddApplication, self).get_form_kwargs(**kwargs)
        kwargs['user'] = self.request.user
        return kwargs

    def form_valid(self, form):
        app = Application(user=self.request.user, character=form.cleaned_data['character'], corporation=form.cleaned_data['corporation'])
        app.save()
        messages.add_message(self.request, messages.INFO, "Your application to %s has been created." % app.corporation)
        return HttpResponseRedirect(reverse('hr-viewapplication', args=[app.id]))

    def get_template_names(self):
        if len(EVEPlayerCorporation.objects.filter(application_config__is_accepting=True)):
            return 'hr/applications/add.html'
        else:
            return 'hr/applications/noadd.html'

### Recommendation Management

class HrViewRecommendations(TemplateView):
    """
    Shows a list of the user's recommendations in the system
    """

    template_name = 'hr/recommendations/view_list.html'

    def get_context_data(self, **kwargs):
        context = super(HrViewRecommendations, self).get_context_data(**kwargs)
        context['recommendations'] = Recommendation.objects.filter(user=self.request.user)
        return context

class HrAddRecommendation(FormView):

    template_name = 'hr/recommendations/add.html'
    form_class = RecommendationForm

    def dispatch(self, request, *args, **kwargs):
        if len(blacklist_values(request.user, BLACKLIST_LEVEL_ADVISORY)):
            raise Http404
        return super(HrAddRecommendation, self).dispatch(request, *args, **kwargs)

    def form_valid(self, form):
        rec = Recommendation(user=self.request.user)
        rec.user_character = form.cleaned_data['character']
        rec.application = form.cleaned_data['application']
        rec.save()

        messages.add_message(self.request, messages.INFO, "Recommendation added to %s's application" % rec.application )
        return HttpResponseRedirect(reverse('hr-viewrecommendations'))

    def get_form_kwargs(self):
        kwargs = super(HrAddRecommendation, self).get_form_kwargs()
        kwargs['user'] = self.request.user
        return kwargs

class HrAdminApplications(ListView):

    model = Application
    template_name = 'hr/applications/admin/view_list.html'
    context_object_name = 'apps'

    def get_queryset(self):
        if self.request.user.has_perm('hr.can_view_all'):
            apps = Application.objects.all()
        elif self.request.user.has_perm('hr.can_view_corp'):
            apps = Application.objects.filter(corporation__id__in=set(EVEPlayerCharacter.objects.filter(eveaccount__user=self.request.user).values_list('corporation__id', flat=True)))
        else:
            apps = Application.objects.none()

        query = self.request.GET.get('q', None)
        order = self.request.GET.get('o', 'id')

        # Filter by the query string
        if query:
            apps = apps.filter(character__name__icontains=query)
        else:
            apps = apps.filter(status__in=[APPLICATION_STATUS_AWAITINGREVIEW, APPLICATION_STATUS_ACCEPTED, APPLICATION_STATUS_QUERY, APPLICATION_STATUS_FLAGGED])

        # If a invalid order as been passed, correct it
        if not order in ['id', 'corporation__name', 'character__name']:
            order = 'id'

        # If we've got a short search string, only get the first 50
        if query and len(query) < 3:
            apps = apps[:50]

        return apps

class HrUpdateApplication(BaseDetailView):
    """
    Updates the status of a application if the workflow and permissions allow so.
    """
    model = Application
    slug_field = 'id'

    def render_to_response(self, context):
        status = self.kwargs.get('status', None)
        if status and int(status) in APPLICATION_STATUS_ROUTES[self.object.status]:
            perm = check_permissions(self.request.user, self.object)
            if perm == HR_ADMIN or (perm == HR_VIEWONLY and int(status) <= 1):
                if not self.object.status == status:
                    self.object.status = status
                    self.object.save(user=self.request.user)
                    self.object = self.model.objects.get(pk=self.object.pk)
                    messages.add_message(self.request, messages.INFO, "Application %s has been changed to %s" % (self.object.id, self.object.get_status_display()))
        else:
             messages.add_message(self.request, messages.ERROR, "Invalid status change request")
        return HttpResponseRedirect(reverse('hr-viewapplication', args=[self.object.id]))

class HrAddNote(CreateView):
    """
    View to add a note to a application
    """

    template_name = 'hr/applications/add_note.html'
    form_class = NoteForm
    model = Audit

    def dispatch(self, request, *args, **kwargs):
        if not check_permissions(request.user) == HR_ADMIN:
            return HttpResponseRedirect(reverse('hr.views.HrIndexView'))
        self.application = Application.objects.get(pk=kwargs.get('applicationid'))
        return super(HrAddNote, self).dispatch(request, *args, **kwargs)

    def form_valid(self, form):
        if check_permissions(self.request.user, self.application) == HR_ADMIN:
            self.object = form.save(commit=False)
            self.object.event = AUDIT_EVENT_NOTE
            self.object.application = self.application
            self.object.user = self.request.user
            self.object.save()
        return HttpResponseRedirect(self.get_success_url())

    def get_success_url(self):
        return reverse('hr-viewapplication', args=[self.application.id])

    def get_context_data(self, **kwargs):
        context = super(HrAddNote, self).get_context_data(**kwargs)
        context['application'] = self.application
        return context

class HrAddMessage(HrAddNote):

    template_name = 'hr/applications/add_message.html'

    def dispatch(self, request, *args, **kwargs):
        self.application = Application.objects.get(pk=kwargs.get('applicationid'))
        self.perm = check_permissions(request.user, self.application)
        if not self.perm:
            return HttpResponseRedirect(reverse('hr.views.HrIndexView'))
        return super(HrAddMessage, self).dispatch(request, *args, **kwargs)

    def get_form_class(self):
        if self.perm == HR_ADMIN:
            return AdminNoteForm
        else:
            return NoteForm

    def get_form_kwargs(self):
        kwargs = super(HrAddMessage, self).get_form_kwargs()
        if self.perm == HR_ADMIN:
            kwargs['application'] = self.application
        return kwargs

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.application = self.application
        self.object.event = AUDIT_EVENT_MESSAGE
        self.object.user = self.request.user
        self.object.save()
        if not self.application.user == self.request.user:
            try:
                send_message(self.application, 'message', note=self.object.text)
            except:
                pass
        return HttpResponseRedirect(self.get_success_url())

class HrRejectApplication(CreateView):

    template_name = 'hr/applications/reject.html'
    message_template_name = 'rejected'
    form_class = AdminNoteForm
    model = Audit
    application_change_status = APPLICATION_STATUS_REJECTED
    audit_event_type = AUDIT_EVENT_REJECTION

    def dispatch(self, request, *args, **kwargs):
        self.application = get_object_or_404(Application, pk=kwargs.get('applicationid'))
        if not (check_permissions(request.user) == HR_ADMIN and request.user.has_perm('hr.can_accept')):
            return HttpResponseRedirect(reverse('hr-index'))
        return super(HrRejectApplication, self).dispatch(request, *args, **kwargs)

    def get_form_kwargs(self):
        kwargs = super(HrRejectApplication, self).get_form_kwargs()
        kwargs['application'] = self.application
        return kwargs

    def get_context_data(self, **kwargs):
        context = super(HrRejectApplication, self).get_context_data(**kwargs)
        context['application'] = self.application
        return context

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.application = self.application
        self.object.user = self.request.user
        self.object.event = self.audit_event_type
        self.object.save()

        self.object.application.status = self.application_change_status
        self.object.application.save(user=self.request.user)

        try:
            send_message(self.object.application, self.message_template_name, note=self.object.text)
        except:
            pass
        return HttpResponseRedirect(self.get_success_url())

    def get_success_url(self):
        return reverse('hr-viewapplication', args=[self.application.id])

class HrAcceptApplication(HrRejectApplication):

    template_name = 'hr/applications/accept.html'
    message_template_name = 'accepted'
    application_change_status = APPLICATION_STATUS_ACCEPTED
    audit_event_type = AUDIT_EVENT_ACCEPTED

    def dispatch(self, request, *args, **kwargs):
        app = get_object_or_404(Application, pk=kwargs.get('applicationid'))
        if app.blacklisted:
            messages.add_message(request, messages.INFO, "This application has one or more blacklist entries and cannot be accepted.")
            return HttpResponseRedirect(reverse('hr-viewapplication', args=[app.id]))
        return super(HrAcceptApplication, self).dispatch(request, *args, **kwargs)

class HrBlacklistUser(FormView):

    template_name = 'hr/blacklist/blacklist.html'
    form_class = BlacklistUserForm

    def dispatch(self, request, *args, **kwargs):
        self.blacklist_user = get_object_or_404(User, id=kwargs.get('userid'))
        return super(HrBlacklistUser, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(HrBlacklistUser, self).get_context_data(**kwargs)
        context['blacklistuser'] = self.blacklist_user
        return context

    def blacklist_item(type, value):
        Blacklist(type=self.type, value=self.value, level=self.level, source=self.source, expiry_date=self.expiry, created_by=self.request.user, reason=self.reason).save()

    def form_valid(self, form):
        self.source = BlacklistSource.objects.get(id=1)
        self.expiry = form.cleaned_data.get('expiry_date', None)
        if not self.expiry:
            self.expiry = datetime.utcnow() + timedelta(days=50*365) # 50 year default
        self.level = form.cleaned_data.get('level', 0)
        self.reason = form.cleaned_data.get('reason', 'No reason provided')

        # Blacklist email address
        self.blacklist_item(BLACKLIST_TYPE_EMAIL, self.blacklist_user.email)

        # Blacklist API keys
        for account in self.blacklist_user.eveaccount_set.all():
            self.blacklist_item(BLACKLIST_TYPE_APIUSERID, account.api_user_id)

        # Blacklist Characters
        for character in EVEPlayerCharacter.objects.filter(eveaccount__user=self.blacklist_user).distinct():
            self.blacklist_item(BLACKLIST_TYPE_CHARACTER, character.name)

        # Blacklist Reddit accounts
        if installed('reddit'):
            for account in u.redditaccount_set.all():
                self.blacklist_item(BLACKLIST_TYPE_REDDIT, account.username)

        messages.add_message(request, messages.INFO, "User %s has been blacklisted" % u.username )

        # Disable the account if requested
        if form.cleaned_data.get('disable', None):
            self.blacklist_user.active = False
            self.blacklist_user.save()

        update_user_access.delay(user=self.blacklist_user.id)

        messages.add_message(request, messages.INFO, "User %s disabled" % u.username )

        return redirect('sso.views.user_view', username=self.blacklist_user.username)
<|endoftext|>


<commit_before>
# Copyright 2014 Open Connectome Project (http://openconnecto.me)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Created by Disa Mhembere
# Email: disa@jhu.edu
"""
@author: Disa Mhembere
@organization: Johns Hopkins University
@contact: disa@jhu.edu

@summary: A module containing the settings for the django project
"""
import os.path

from settings_secret import *

DEBUG = True
TEMPLATE_DEBUG = DEBUG

#ADMINS - moved

MANAGERS = ADMINS

# DATABASES - moved

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# On Unix systems, a value of None will cause Django to use the same
# timezone as the operating system.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'America/New_York'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
#MEDIA_ROOT = '/data/ocp'
MEDIA_ROOT = '/mnt/nfs/indata/'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = '/ocp/'

BASE_DIR = os.path.abspath(os.path.join(os.environ["M2G_HOME"], "MR-OCP", "MROCPdjango"))

# Directory containing templates
TEMPLATE_DIR = os.path.join(BASE_DIR, 'pipeline', 'templates/')
PROCESSING_SCRIPTS = os.path.join(os.environ["M2G_HOME"], "MR-OCP", "mrcap")

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"

STATIC_ROOT = os.path.join(BASE_DIR,'pipeline', 'static/')

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/graph-services/static/'

# DM for url resolution *MUST have a trailing slash /*
URL_BASE = "graph-services/"

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(BASE_DIR, 'pipeline', 'templates/static/'),
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    #'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# Make this unique, and don't share it with anybody.
# SECRET_KEY - moved

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    'django.template.loaders.eggs.Loader',
)

# DM Add for context processors in templates
TEMPLATE_CONTEXT_PROCESSORS = (
#    'django.core.context_processors.debug',
#    'django.core.context_processors.i18n',
#    'django.core.context_processors.media',
    'django.core.context_processors.request', # Access request data from template
    'django.core.context_processors.static', # {{STATIC_URL}}
    'django.contrib.auth.context_processors.auth', # For use with user An auth.User instance
#    'django.contrib.messages.context_processors.messages',
# Add method here that takes httpRequest & returns dict for custom context processors
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    #'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    #'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'ocpipeline.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'ocpipeline.wsgi.application'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    #os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),
    os.path.join(BASE_DIR,'pipeline', 'templates'),
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'pipeline', # DM
    'registration', # DM
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    'django.contrib.admindocs',
    'django_tables2',
    'djcelery',
)

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

# For invariant types
# The following are valid fileTypes:
# 0. 'lcc'|'lrgstConnComp' is largest connected component
# 1.'cc'|'clustCoeff' is the clustering coefficient
# 2.'deg'|'degree' is the local vertex degree
# 3.'eig'|'eigen' is the eigenvalues AND eigenvectors
# 4.'mad'|'maxAvgDeg' is the maximum average degree
# 5.'ss1'| 'scanStat1' is the scan statistic 1
# 6.'ss2'| 'scanStat2' is the scan statistic 2
# 7.'tri'|'triangle' is the triangle count
# 8.'fg'|'fibergraph' is a fibergraph built by gengraph.py
# 9.'apl'|'avePathLen' is the avergae path length
# 10. 'svd'|'singValDecomp' is the single value decomposition embedding

EQUIV_NP_ARRAYS = {'cc':'clustCoeff', 'deg':'degree', 'eig':'eigen', 'apl':'avePathLen',
                    'ss1': 'scanStat1','ss2': 'scanStat2','tri':'triangle','svd':'singValDecomp'}

VALID_FILE_TYPES = EQUIV_NP_ARRAYS
VALID_FILE_TYPES['mad'] = 'maxAvgDeg'
VALID_FILE_TYPES['fg'] = 'fibergraph'
VALID_FILE_TYPES['lcc'] = 'lrgstConnComp'
VALID_FILE_TYPES['gdia'] = 'graphDiam'

# Celery Settings
BROKER_URL = 'amqp://guest@localhost'
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_ACCEPT_CONTENT=['json', 'pickle']
CELERYD_PREFETCH_MULTIPLIER = 1
<commit_msg>[admin]: removed unnecessary media path<commit_after>
# Copyright 2014 Open Connectome Project (http://openconnecto.me)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Created by Disa Mhembere
# Email: disa@jhu.edu
"""
@author: Disa Mhembere
@organization: Johns Hopkins University
@contact: disa@jhu.edu

@summary: A module containing the settings for the django project
"""
import os.path

from settings_secret import *

DEBUG = True
TEMPLATE_DEBUG = DEBUG

#ADMINS - moved

MANAGERS = ADMINS

# DATABASES - moved

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# On Unix systems, a value of None will cause Django to use the same
# timezone as the operating system.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'America/New_York'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = '/ocp/'

BASE_DIR = os.path.abspath(os.path.join(os.environ["M2G_HOME"], "MR-OCP", "MROCPdjango"))

# Directory containing templates
TEMPLATE_DIR = os.path.join(BASE_DIR, 'pipeline', 'templates/')
PROCESSING_SCRIPTS = os.path.join(os.environ["M2G_HOME"], "MR-OCP", "mrcap")

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"

STATIC_ROOT = os.path.join(BASE_DIR,'pipeline', 'static/')

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/graph-services/static/'

# DM for url resolution *MUST have a trailing slash /*
URL_BASE = "graph-services/"

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(BASE_DIR, 'pipeline', 'templates/static/'),
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    #'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# Make this unique, and don't share it with anybody.
# SECRET_KEY - moved

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    'django.template.loaders.eggs.Loader',
)

# DM Add for context processors in templates
TEMPLATE_CONTEXT_PROCESSORS = (
#    'django.core.context_processors.debug',
#    'django.core.context_processors.i18n',
#    'django.core.context_processors.media',
    'django.core.context_processors.request', # Access request data from template
    'django.core.context_processors.static', # {{STATIC_URL}}
    'django.contrib.auth.context_processors.auth', # For use with user An auth.User instance
#    'django.contrib.messages.context_processors.messages',
# Add method here that takes httpRequest & returns dict for custom context processors
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    #'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    #'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'ocpipeline.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'ocpipeline.wsgi.application'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    #os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),
    os.path.join(BASE_DIR,'pipeline', 'templates'),
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'pipeline', # DM
    'registration', # DM
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    'django.contrib.admindocs',
    'django_tables2',
    'djcelery',
)

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

# For invariant types
# The following are valid fileTypes:
# 0. 'lcc'|'lrgstConnComp' is largest connected component
# 1.'cc'|'clustCoeff' is the clustering coefficient
# 2.'deg'|'degree' is the local vertex degree
# 3.'eig'|'eigen' is the eigenvalues AND eigenvectors
# 4.'mad'|'maxAvgDeg' is the maximum average degree
# 5.'ss1'| 'scanStat1' is the scan statistic 1
# 6.'ss2'| 'scanStat2' is the scan statistic 2
# 7.'tri'|'triangle' is the triangle count
# 8.'fg'|'fibergraph' is a fibergraph built by gengraph.py
# 9.'apl'|'avePathLen' is the avergae path length
# 10. 'svd'|'singValDecomp' is the single value decomposition embedding

EQUIV_NP_ARRAYS = {'cc':'clustCoeff', 'deg':'degree', 'eig':'eigen', 'apl':'avePathLen',
                    'ss1': 'scanStat1','ss2': 'scanStat2','tri':'triangle','svd':'singValDecomp'}

VALID_FILE_TYPES = EQUIV_NP_ARRAYS
VALID_FILE_TYPES['mad'] = 'maxAvgDeg'
VALID_FILE_TYPES['fg'] = 'fibergraph'
VALID_FILE_TYPES['lcc'] = 'lrgstConnComp'
VALID_FILE_TYPES['gdia'] = 'graphDiam'

# Celery Settings
BROKER_URL = 'amqp://guest@localhost'
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_ACCEPT_CONTENT=['json', 'pickle']
CELERYD_PREFETCH_MULTIPLIER = 1
<|endoftext|>