<commit_before># Basic settings for django. They should enable localhost-only development.
# These settings are extended for actual production use.

# Before go-live review: https://docs.djangoproject.com/en/dev/howto/deployment/checklist/

import os.path

TEMPLATE_DEBUG = DEBUG = True
# If development is set to true, all calls to Sailpoint are bypassed with stubbed returns.
DEVELOPMENT = True

ADMINS = ()
MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE':   'django.db.backends.sqlite3',
        'NAME':     'sqlite.db',
    }
}

# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.6/ref/settings/#allowed-hosts
ALLOWED_HOSTS = []

# 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.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'America/Los_Angeles'

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

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

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

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

# MyInfo / OAM does not use user-uploaded media at any point.
MEDIA_ROOT = ''
MEDIA_URL = ''

# 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: "/var/www/example.com/static/"
STATIC_ROOT = '/var/www/html/static/'

# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'

# 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.
)

# 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 = 'developmentkey'

# 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',
)

MIDDLEWARE_CLASSES = (
    'downtime.middleware.DowntimeMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django_cas.middleware.CASMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'lib.backends.OAMLoginBackend',
    'lib.backends.AccountPickupBackend',
    'lib.backends.ForgotPasswordBackend',
    'django_cas.backends.CASBackend',
)

ROOT_URLCONF = 'oam_base.urls'

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

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    #'django.contrib.sites',
    #'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'CustomUser',
    'MyInfo',
    'AccountPickup',
    'PasswordReset',
    'lib',
    # Apps related to plugins.
    'ajax',
    'widget_tweaks',
    'localflavor',
    'downtime',
    'debug_toolbar.apps.DebugToolbarConfig',
)

# 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'
        }
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s - %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'RotatingFileHandler': {
            'filename': 'myinfo.log',
            'maxBytes' : 262144,
            'backupCount' : 5,
            'formatter': 'verbose',
            'class': 'logging.handlers.RotatingFileHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        'oam_base': {
            'handlers': ['RotatingFileHandler'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'AccountPickup': {
            'handlers': ['RotatingFileHandler'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'MyInfo': {
            'handlers': ['RotatingFileHandler'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'PasswordReset': {
            'handlers': ['RotatingFileHandler'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'django_cas': {
            'handlers': ['RotatingFileHandler'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'lib': {
            'handlers': ['RotatingFileHandler'],
            'level': 'DEBUG',
            'propagate': False,
        },
    }
}

AUTH_USER_MODEL = 'CustomUser.PSUCustomUser'

# Settings related to CAS authentication.
#CAS_SERVER_URL = ''
#CAS_VERSION = '1'
CAS_SERVER_URL = 'https://ssodevel.oit.pdx.edu/cas/'
CAS_VERSION = 'CAS_2_SAML_1_0'

# Settings related to sailpoint.
SAILPOINT_SERVER_URL = ''
SAILPOINT_USERNAME = ''
SAILPOINT_PASSWORD = ''

# Username length patch.
MAX_USERNAME_LENGTH = 36

# Downtime exempt paths.
DOWNTIME_EXEMPT_PATHS = (
	'/admin',
    '/accounts/login',
)
<commit_msg>Disabled timezone support. We aren't using it.<commit_after># Basic settings for django. They should enable localhost-only development.
# These settings are extended for actual production use.

# Before go-live review: https://docs.djangoproject.com/en/dev/howto/deployment/checklist/

import os.path

TEMPLATE_DEBUG = DEBUG = True
# If development is set to true, all calls to Sailpoint are bypassed with stubbed returns.
DEVELOPMENT = True

ADMINS = ()
MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE':   'django.db.backends.sqlite3',
        'NAME':     'sqlite.db',
    }
}

# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.6/ref/settings/#allowed-hosts
ALLOWED_HOSTS = []

# 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.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'America/Los_Angeles'

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

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

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

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

# MyInfo / OAM does not use user-uploaded media at any point.
MEDIA_ROOT = ''
MEDIA_URL = ''

# 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: "/var/www/example.com/static/"
STATIC_ROOT = '/var/www/html/static/'

# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'

# 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.
)

# 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 = 'developmentkey'

# 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',
)

MIDDLEWARE_CLASSES = (
    'downtime.middleware.DowntimeMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django_cas.middleware.CASMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'lib.backends.OAMLoginBackend',
    'lib.backends.AccountPickupBackend',
    'lib.backends.ForgotPasswordBackend',
    'django_cas.backends.CASBackend',
)

ROOT_URLCONF = 'oam_base.urls'

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

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    #'django.contrib.sites',
    #'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'CustomUser',
    'MyInfo',
    'AccountPickup',
    'PasswordReset',
    'lib',
    # Apps related to plugins.
    'ajax',
    'widget_tweaks',
    'localflavor',
    'downtime',
    'debug_toolbar.apps.DebugToolbarConfig',
)

# 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'
        }
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s - %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'RotatingFileHandler': {
            'filename': 'myinfo.log',
            'maxBytes' : 262144,
            'backupCount' : 5,
            'formatter': 'verbose',
            'class': 'logging.handlers.RotatingFileHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        'oam_base': {
            'handlers': ['RotatingFileHandler'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'AccountPickup': {
            'handlers': ['RotatingFileHandler'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'MyInfo': {
            'handlers': ['RotatingFileHandler'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'PasswordReset': {
            'handlers': ['RotatingFileHandler'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'django_cas': {
            'handlers': ['RotatingFileHandler'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'lib': {
            'handlers': ['RotatingFileHandler'],
            'level': 'DEBUG',
            'propagate': False,
        },
    }
}

AUTH_USER_MODEL = 'CustomUser.PSUCustomUser'

# Settings related to CAS authentication.
#CAS_SERVER_URL = ''
#CAS_VERSION = '1'
CAS_SERVER_URL = 'https://ssodevel.oit.pdx.edu/cas/'
CAS_VERSION = 'CAS_2_SAML_1_0'

# Settings related to sailpoint.
SAILPOINT_SERVER_URL = ''
SAILPOINT_USERNAME = ''
SAILPOINT_PASSWORD = ''

# Username length patch.
MAX_USERNAME_LENGTH = 36

# Downtime exempt paths.
DOWNTIME_EXEMPT_PATHS = (
	'/admin',
    '/accounts/login',
)
<|endoftext|>


<commit_before>from sys import modules

from django.db import models

from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.generic import GenericForeignKey
from django.db import transaction
from django.db.utils import DatabaseError
from django.template.loader import get_template
from django.template import Context
from django.utils import simplejson

class LogActionManager(models.Manager):
    _cache = {}
    _SYNCED = False
    _DELAYED = []

    def register(self, key, template, build_cache=None):
        if LogActionManager._SYNCED:
            return LogAction.objects._register(key, template, build_cache)
        else:
            self._DELAYED.append((key, template, build_cache))

    @transaction.commit_manually
    def _register(self, key, template, build_cache=None):
        """
        Registers and caches an LogAction type

        @param key : Key identifying log action
        @param template : template associated with key
        """
        try:
            try:
                action = self.get_from_cache(key)
                action.template = template
                action.save()
            except LogAction.DoesNotExist:
                action, new = LogAction.objects.get_or_create(name=key, \
                template=template)
                self._cache.setdefault(self.db, {})[key] = action
                action.save()
            action.build_cache = build_cache
            return action
        except:
            transaction.rollback()
        finally:
            transaction.commit()

    def _register_delayed(sender, **kwargs):
        """
        Register all permissions that were delayed waiting for database tables to
        be created.

        Don't call this from outside code.
        """
        try:
            for args in LogActionManager._DELAYED:
                LogAction.objects._register(*args)
            models.signals.post_syncdb.disconnect(LogActionManager._register_delayed)
            LogActionManager._SYNCED = True
        except DatabaseError:
            # still waiting for models in other apps to be created
            pass

    # connect signal for delayed registration.  Filter by this module so that
    # it is only called once
    models.signals.post_syncdb.connect(_register_delayed, \
                                       sender=modules['object_log.models'])

    def get_from_cache(self, key):
        """
        Attempts to retrieve the LogAction from cache, if it fails, loads
        the action into the cache.

        @param key : key passed to LogAction.objects.get
        """
        try:
            action = self._cache[self.db][key]
        except KeyError:
            action = LogAction.objects.get(name=key)
            self._cache.setdefault(self.db, {})[key]=action

            # update build_cache function if needed
            for key_, template, build_cache in LogActionManager._DELAYED:
                if key == key_:
                    action.build_cache = build_cache

        return action

class LogAction(models.Model):
    """
    Type of action of log entry (for example: addition, deletion)

    @param name           string  verb (for example: add)
    """

    name = models.CharField(max_length=128, unique=True, primary_key=True)
    template = models.CharField(max_length=128, unique=True)
    objects = LogActionManager()

    def __str__(self):
        return 'LogAction: %s Template: %s \n'%(self.name, self.template)

class LogItemManager(models.Manager):

    def log_action(self, key, user, object1, object2=None, object3=None, data=None):
        """
        Creates new log entry

        @param user             Profile
        @param affected_object  any model
        @param key              string (LogAction.name)
        """
        # Want to use unicode?
        # Add this at import section of the file
        #from django.utils.encoding import smart_unicode
        # Uncomment below:
        #key = smart_unicode(key)
        action = LogAction.objects.get_from_cache(key)
        entry = self.model(action=action, user=user, object1=object1)

        if object2 is not None:
            entry.object2 = object2

        if object3 is not None:
            entry.object3 = object3

        # build cached data and or arbitrary data.
        if action.build_cache is not None:
            entry.data = action.build_cache(user, object1, object2, object3, data)
        elif data is not None:
            entry.data = data

        entry.save(force_insert=True)
        return entry

class LogItem(models.Model):
    """
    Single entry in log
    """
    action = models.ForeignKey(LogAction)
    #action = models.CharField(max_length=128)
    timestamp = models.DateTimeField(auto_now_add=True, )
    user = models.ForeignKey(User, related_name='log_items')

    object_type1 = models.ForeignKey(ContentType, \
    related_name='log_items1', null=True)
    object_id1 = models.PositiveIntegerField(null=True)
    object1 = GenericForeignKey("object_type1", "object_id1")

    object_type2 = models.ForeignKey(ContentType, \
    related_name='log_items2', null=True)
    object_id2 = models.PositiveIntegerField(null=True)
    object2 = GenericForeignKey("object_type2", "object_id2")

    object_type3 = models.ForeignKey(ContentType, \
    related_name='log_items3', null=True)
    object_id3 = models.PositiveIntegerField(null=True)
    object3 = GenericForeignKey("object_type3", "object_id3")

    serialized_data = models.TextField(null=True)

    objects = LogItemManager()
    _data = None

    class Meta:
        ordering = ("timestamp", )

    @property
    def data(self):
        if self._data is None and not self.serialized_data is None:
            self._data = simplejson.loads(self.serialized_data)
        return self._data

    @data.setter
    def data(self, value):
        self._data = value
        self.serialized_data = None

    @property
    def template(self):
        """
        retrieves template for this log item
        """
        action = LogAction.objects.get_from_cache(self.action_id)
        return get_template(action.template)

    def save(self, *args, **kwargs):
        if self._data is not None and self.serialized_data is None:
            self.serialized_data = simplejson.dumps(self._data)
        super(LogItem, self).save(*args, **kwargs)

    def render(self, **context):
        """
        render this LogItem

        @param context: extra kwargs to add to the context when rendering
        """
        context['log_item'] = self
        action = LogAction.objects.get_from_cache(self.action_id)
        template = get_template(action.template)
        return template.render(Context(context))

    def __repr__(self):
        return 'time: %s user: %s object_type1: %s'%(self.timestamp, self.user, self.object_type1)

    def __str__(self):
        """
        Renders single line log entry to a string,
        containing information like:
        - date and extensive time
        - user who performed an action
        - action itself
        - object affected by the action
        """
        return self.render()

#Most common log types, registered by default for convenience
def create_defaults():
    LogAction.objects.register('EDIT', 'object_log/edit.html')
    LogAction.objects.register('CREATE', 'object_log/add.html')
    LogAction.objects.register('DELETE', 'object_log/delete.html')
create_defaults()<commit_msg>adding related_name for LogItem -> LogAction<commit_after>from sys import modules

from django.db import models

from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.generic import GenericForeignKey
from django.db import transaction
from django.db.utils import DatabaseError
from django.template.loader import get_template
from django.template import Context
from django.utils import simplejson

class LogActionManager(models.Manager):
    _cache = {}
    _SYNCED = False
    _DELAYED = []

    def register(self, key, template, build_cache=None):
        if LogActionManager._SYNCED:
            return LogAction.objects._register(key, template, build_cache)
        else:
            self._DELAYED.append((key, template, build_cache))

    @transaction.commit_manually
    def _register(self, key, template, build_cache=None):
        """
        Registers and caches an LogAction type

        @param key : Key identifying log action
        @param template : template associated with key
        """
        try:
            try:
                action = self.get_from_cache(key)
                action.template = template
                action.save()
            except LogAction.DoesNotExist:
                action, new = LogAction.objects.get_or_create(name=key, \
                template=template)
                self._cache.setdefault(self.db, {})[key] = action
                action.save()
            action.build_cache = build_cache
            return action
        except:
            transaction.rollback()
        finally:
            transaction.commit()

    def _register_delayed(sender, **kwargs):
        """
        Register all permissions that were delayed waiting for database tables to
        be created.

        Don't call this from outside code.
        """
        try:
            for args in LogActionManager._DELAYED:
                LogAction.objects._register(*args)
            models.signals.post_syncdb.disconnect(LogActionManager._register_delayed)
            LogActionManager._SYNCED = True
        except DatabaseError:
            # still waiting for models in other apps to be created
            pass

    # connect signal for delayed registration.  Filter by this module so that
    # it is only called once
    models.signals.post_syncdb.connect(_register_delayed, \
                                       sender=modules['object_log.models'])

    def get_from_cache(self, key):
        """
        Attempts to retrieve the LogAction from cache, if it fails, loads
        the action into the cache.

        @param key : key passed to LogAction.objects.get
        """
        try:
            action = self._cache[self.db][key]
        except KeyError:
            action = LogAction.objects.get(name=key)
            self._cache.setdefault(self.db, {})[key]=action

            # update build_cache function if needed
            for key_, template, build_cache in LogActionManager._DELAYED:
                if key == key_:
                    action.build_cache = build_cache

        return action

class LogAction(models.Model):
    """
    Type of action of log entry (for example: addition, deletion)

    @param name           string  verb (for example: add)
    """

    name = models.CharField(max_length=128, unique=True, primary_key=True)
    template = models.CharField(max_length=128, unique=True)
    objects = LogActionManager()

    def __str__(self):
        return 'LogAction: %s Template: %s \n'%(self.name, self.template)

class LogItemManager(models.Manager):

    def log_action(self, key, user, object1, object2=None, object3=None, data=None):
        """
        Creates new log entry

        @param user             Profile
        @param affected_object  any model
        @param key              string (LogAction.name)
        """
        # Want to use unicode?
        # Add this at import section of the file
        #from django.utils.encoding import smart_unicode
        # Uncomment below:
        #key = smart_unicode(key)
        action = LogAction.objects.get_from_cache(key)
        entry = self.model(action=action, user=user, object1=object1)

        if object2 is not None:
            entry.object2 = object2

        if object3 is not None:
            entry.object3 = object3

        # build cached data and or arbitrary data.
        if action.build_cache is not None:
            entry.data = action.build_cache(user, object1, object2, object3, data)
        elif data is not None:
            entry.data = data

        entry.save(force_insert=True)
        return entry

class LogItem(models.Model):
    """
    Single entry in log
    """
    action = models.ForeignKey(LogAction, related_name="entries")
    #action = models.CharField(max_length=128)
    timestamp = models.DateTimeField(auto_now_add=True, )
    user = models.ForeignKey(User, related_name='log_items')

    object_type1 = models.ForeignKey(ContentType, \
    related_name='log_items1', null=True)
    object_id1 = models.PositiveIntegerField(null=True)
    object1 = GenericForeignKey("object_type1", "object_id1")

    object_type2 = models.ForeignKey(ContentType, \
    related_name='log_items2', null=True)
    object_id2 = models.PositiveIntegerField(null=True)
    object2 = GenericForeignKey("object_type2", "object_id2")

    object_type3 = models.ForeignKey(ContentType, \
    related_name='log_items3', null=True)
    object_id3 = models.PositiveIntegerField(null=True)
    object3 = GenericForeignKey("object_type3", "object_id3")

    serialized_data = models.TextField(null=True)

    objects = LogItemManager()
    _data = None

    class Meta:
        ordering = ("timestamp", )

    @property
    def data(self):
        if self._data is None and not self.serialized_data is None:
            self._data = simplejson.loads(self.serialized_data)
        return self._data

    @data.setter
    def data(self, value):
        self._data = value
        self.serialized_data = None

    @property
    def template(self):
        """
        retrieves template for this log item
        """
        action = LogAction.objects.get_from_cache(self.action_id)
        return get_template(action.template)

    def save(self, *args, **kwargs):
        if self._data is not None and self.serialized_data is None:
            self.serialized_data = simplejson.dumps(self._data)
        super(LogItem, self).save(*args, **kwargs)

    def render(self, **context):
        """
        render this LogItem

        @param context: extra kwargs to add to the context when rendering
        """
        context['log_item'] = self
        action = LogAction.objects.get_from_cache(self.action_id)
        template = get_template(action.template)
        return template.render(Context(context))

    def __repr__(self):
        return 'time: %s user: %s object_type1: %s'%(self.timestamp, self.user, self.object_type1)

    def __str__(self):
        """
        Renders single line log entry to a string,
        containing information like:
        - date and extensive time
        - user who performed an action
        - action itself
        - object affected by the action
        """
        return self.render()

#Most common log types, registered by default for convenience
def create_defaults():
    LogAction.objects.register('EDIT', 'object_log/edit.html')
    LogAction.objects.register('CREATE', 'object_log/add.html')
    LogAction.objects.register('DELETE', 'object_log/delete.html')
create_defaults()<|endoftext|>


<commit_before># Copyright (C) 2007, Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

"""
UNSTABLE. Used only internally by Activity and jarabe.
"""

import gtk

def get_activity_id(wnck_window):
    window = gtk.gdk.window_foreign_new(wnck_window.get_xid())
    prop_info = window.property_get('_SUGAR_ACTIVITY_ID', 'STRING')
    if prop_info is None:
        return None
    else:
        return prop_info[2]

def get_bundle_id(wnck_window):
    window = gtk.gdk.window_foreign_new(wnck_window.get_xid())
    prop_info = window.property_get('_SUGAR_BUNDLE_ID', 'STRING')
    if prop_info is None:
        return None
    else:
        return prop_info[2]

def set_activity_id(window, activity_id):
    window.property_change('_SUGAR_ACTIVITY_ID', 'STRING', 8,
                           gtk.gdk.PROP_MODE_REPLACE, activity_id)

def set_bundle_id(window, bundle_id):
    window.property_change('_SUGAR_BUNDLE_ID', 'STRING', 8,
                           gtk.gdk.PROP_MODE_REPLACE, bundle_id)
<commit_msg>Trap X errors in wm.py (#1123)<commit_after># Copyright (C) 2007, Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

"""
UNSTABLE. Used only internally by Activity and jarabe.
"""

import gtk
import logging

def _property_get_trapped(window, prop, prop_type):
    gtk.gdk.error_trap_push()

    prop_info = window.property_get('_SUGAR_ACTIVITY_ID', 'STRING')

    # We just log a message
    error = gtk.gdk.error_trap_pop()
    if gtk.gdk.error_trap_pop():
        logging.debug('Received X Error (%i) while getting '
                      'a property on a window' % error)

    return prop_info

def _property_change_trapped(window, prop, prop_type, format, mode, data):
    gtk.gdk.error_trap_push()

    window.property_change(prop, prop_type, format, mode, data)

    error = gtk.gdk.error_trap_pop()
    if error:
        logging.debug('Received X Error (%i) while setting '
                      'a property on a window' % error)
        raise RuntimeError('Received X Error (%i) while setting '
                           'a property on a window' % error)

def get_activity_id(wnck_window):
    window = gtk.gdk.window_foreign_new(wnck_window.get_xid())
    prop_info = _property_get_trapped(window, '_SUGAR_ACTIVITY_ID', 'STRING')
    if prop_info is None:
        return None
    else:
        return prop_info[2]

def get_bundle_id(wnck_window):
    window = gtk.gdk.window_foreign_new(wnck_window.get_xid())
    prop_info = _property_get_trapped(window, '_SUGAR_BUNDLE_ID', 'STRING')
    if prop_info is None:
        return None
    else:
        return prop_info[2]

def set_activity_id(window, activity_id):
    _property_change_trapped(window, '_SUGAR_ACTIVITY_ID', 'STRING', 8,
                             gtk.gdk.PROP_MODE_REPLACE, activity_id)

def set_bundle_id(window, bundle_id):
    _property_change_trapped(window, '_SUGAR_BUNDLE_ID', 'STRING', 8,
                             gtk.gdk.PROP_MODE_REPLACE, bundle_id)

<|endoftext|>


<commit_before>"""
Run Chrome with debugging console enabled
"""

import logging
import os
import subprocess
import signal
import time
import json
import sys
import websocket
from datetime import datetime
from .remote_shell import ChromeRemoteShell
from .chrome_profile import chrome_profile

logger = logging.getLogger(__name__)

def dict_val(key, dictionary):
    if key in dictionary:
        return dictionary[key]
    elif key.lower() in dictionary:
        return dictionary[key.lower()]
    return None

def dict_intval(key, dictionary):
    if dict_val(key, dictionary) is not None:
        return int(dict_val(key, dictionary))
    return None

class Request(object):

    def __init__(self, request_id):
        self.id = request_id
        self.failed = False
        self.status_code = None
        self.url = None
        self.content_length = None
        self.mime_type = None
        self.complete = False
        self.redirected = []

class ChromeBrowser(object):

    profile = chrome_profile()

    def __init__(
            self,
            socket=9111,
            startup_delay=4,
            shutdown_delay=3,
            remote_shell_timeout=3,
            chrome_bin=None,
            headless=True,
            content_dir=False,
            work_dir='/tmp/chromeremote',
            user_agent=False,
            master_timeout=120,
    ):
        self.work_dir = work_dir
        self.chrome_sock = socket
        self.chrome_pid = False
        self.chrome_log_file = os.path.join(self.work_dir, 'chromelog.json')
        self.cookie_log_file = os.path.join(self.work_dir, 'cookies.json')
        self.content_dir = os.path.join(self.work_dir, 'content')
        if content_dir:
            self.content_dir = content_dir
        self.open_requests = []
        self.chrome_log = []
        self.shell = False
        self.startup_delay = startup_delay
        self.shutdown_delay = shutdown_delay
        self.remote_shell_timeout = remote_shell_timeout
        self.chrome_bin = chrome_bin
        self.headless = headless
        self.reqs = []
        self.user_agent = user_agent
        if self.chrome_bin is None:
            logger.error('chrome binary not found')
            sys.exit(1)
        self.master_timeout = master_timeout

    def _receive_chrome(self):
        response = json.loads(self.shell.soc.recv())
        self.chrome_log.append(response)
        return response

    def _send_chrome(self, data):
        navcom = json.dumps(data)
        self.shell.soc.send(navcom)
        response = self._receive_chrome()
        return response

    def _start_console(self):
        self.shell = ChromeRemoteShell(
                host='localhost',
                port=self.chrome_sock,
                socket_timeout=self.remote_shell_timeout,
        )
        self.shell.connect()
        logger.debug('Socket timeout: {}s'.format(self.shell.soc.gettimeout()))
        self._send_chrome(
                {"id": 0, "method": "Network.enable"})
        self._send_chrome(
                {"id": 0, "method": "DOMStorage.enable"})
        if self.user_agent:
            self._send_chrome(
                    {
                            "id": 0,
                            "method": "Network.setUserAgentOverride",
                            "params": {
                                    "userAgent": self.user_agent,
                            }
                    }
            )

    def _get_requests(self, redirect_only=False):
        requests = {}
        for entry in self.chrome_log:
            if 'method' not in entry \
                    or not entry['method'].startswith('Network.'):
                continue
            rp = entry['params']
            req_id = rp['requestId']
            if entry['method'] == 'Network.requestWillBeSent':
                if 'redirectResponse' in rp:
                    rdr = rp['redirectResponse']
                    last_req = requests[req_id]
                    last_req.complete = True
                    last_req.status_code = rdr['status']
                    requests[req_id] = Request(req_id)
                    if len(last_req.redirected) > 0:
                        requests[req_id].redirected = last_req.redirected
                        last_req.redirected = []
                    requests[req_id].redirected.append(last_req)
                else:
                    requests[req_id] = Request(req_id)
                requests[req_id].url = rp['request']['url']
            elif entry['method'] == 'Network.requestServedFromCache':
                requests[req_id] = Request(req_id)
            elif entry['method'] == 'Network.responseReceived':
                rpr = rp['response']
                requests[req_id].mime_type = rpr['mimeType']
                requests[req_id].status_code = rpr['status']
                requests[req_id].content_length = dict_intval(
                        'Content-Length', rpr['headers'])
            elif entry['method'] == 'Network.loadingFinished':
                requests[req_id].complete = True
            elif entry['method'] == 'Network.loadingFailed':
                requests[req_id].complete = True
                requests[req_id].failed = True
        for req in sorted(requests):
            r = requests[req]
            if r.url is None:
                continue
            if r.url.startswith('data'):
                continue
            if redirect_only:
                if len(r.redirected) > 0:
                    self.reqs.append(r)
            else:
                self.reqs.append(r)

    def _read_data(self, data=False):
        domstorage_activities = 0
        while True:
            if data and 'method' in data:
                if data['method'] == 'Network.requestWillBeSent' \
                        or data['method'] == 'Network.requestServedFromCache':
                    request_id = data['params']['requestId']
                    # logger.debug('open req {}'.format(request_id))
                    if request_id not in self.open_requests:
                        self.open_requests.append(request_id)
                    domstorage_activities = 0
                elif data['method'] == 'Network.loadingFinished':
                    request_id = data['params']['requestId']
                    # logger.debug('finished req {}'.format(request_id))
                    try:
                        self.open_requests.remove(request_id)
                    except ValueError:
                        logger.error(
                                'loadingFinished but request {} not found'
                                ' in open requests'.format(request_id)
                        )
                    domstorage_activities = 0
                elif data['method'] == 'Network.loadingFailed':
                    request_id = data['params']['requestId']
                    logger.debug('loading failed on req {}'.format(request_id))
                    try:
                        self.open_requests.remove(request_id)
                    except ValueError:
                        logger.error(
                                'request {} not found in open requests'.format(
                                        request_id)
                        )
                    domstorage_activities = 0
                elif data['method'].startswith('DOMStorage'):
                    domstorage_activities += 1
            if domstorage_activities > 10:
                # exit when there is no more network traffic
                logger.debug('looks like a DOMStorage loop. stopping it...')
                self._send_chrome(
                    {"id": 0, "method": "DOMStorage.disable"})
                break
            self.check_timeout()
            try:
                data = self._receive_chrome()
            except websocket.WebSocketTimeoutException:
                logger.debug('TIMEOUT REACHED')
                break

    def start_chrome(self):
        chrome_dir = os.path.join(self.work_dir, 'chrome_profile')
        if not os.path.exists(chrome_dir):
            os.makedirs(chrome_dir)
        logger.debug('Extract Chrome profile to {}...'.format(chrome_dir))
        p = subprocess.call(
                ['tar', 'xz', '-C', chrome_dir, '-f', chrome_profile()])
        logger.debug('Start Chrome...')
        chrome_args = [
                self.chrome_bin,
                '--remote-debugging-port={}'.format(self.chrome_sock),
                '--no-default-browser-check',
                '--user-data-dir={}/chrometemp'.format(chrome_dir),
                '--disable-translate',
                '--net-log-capture-mode=IncludeCookiesAndCredentials',
                '-homepage',
                'about:blank',
                '--disable-extensions']
        if self.headless:
            chrome_args.append('--headless')
        logger.debug(' '.join(chrome_args))
        p = subprocess.Popen(
                chrome_args,
                stdout=subprocess.DEVNULL,
                stderr=subprocess.DEVNULL
        )
        time.sleep(self.startup_delay)
        self.chrome_pid = p.pid
        logger.debug(
                'Chrome PID: {} listening on port {}'.format(
                        p.pid, self.chrome_sock)
        )
        self.start_time = datetime.now()

    def clean_chrome(self):
        logger.debug('Kill Chrome...')
        os.kill(int(self.chrome_pid), signal.SIGTERM)
        logger.debug('Remove chrome profile...')
        time.sleep(self.shutdown_delay)
        subprocess.call(
                ['rm', '-rf', os.path.join(self.work_dir, 'chrome_profile')])

    def check_timeout(self):
        now = datetime.now()
        runtime = now - self.start_time
        if runtime.total_seconds() > self.master_timeout:
            logger.error('chrome master_timeout reached')
            os.kill(int(self.chrome_pid), signal.SIGTERM)
            time.sleep(self.shutdown_delay)
            subprocess.call(
                ['rm', '-rf', os.path.join(self.work_dir, 'chrome_profile')])

    def load_page(self, url):
        if not self.shell:
            self._start_console()
        data = self._send_chrome(
                {
                        "id": 0,
                        "method": "Page.navigate",
                        "params": {
                                "url": url
                        }
                }
        )
        self._read_data(data)
        loopcount = 0
        while len(self.open_requests) > 0 and loopcount < 5:
            self.check_timeout()
            logger.debug('we have {} open requests: {}'.format(
                    len(self.open_requests), self.open_requests))
            self._read_data()
            loopcount += 1
        if len(self.open_requests) > 0:
            logger.debug('open requests: {}'.format(self.open_requests))
        self._send_chrome(
                {"id": 0, "method": "Page.stopLoading"})
        logger.debug('writing log to {}...'.format(self.chrome_log_file))
        with open(self.chrome_log_file, 'w') as f:
            json.dump(self.chrome_log, f, indent=4)

    def get_content(self):
        if not os.path.exists(self.content_dir):
            os.makedirs(self.content_dir)
        cache_index_file = '{}/index.json'.format(self.content_dir)
        cache_index = {}
        req_count = 0
        self._get_requests()
        for req in self.reqs:
            if req.failed:
                continue
            if req.status_code not in (200, 206):
                logger.debug('No Content with Status {}: {} - {}'.format(
                        req.status_code, req.id, req.url))
                continue
            if req.content_length == 0:
                logger.debug(
                        'No Content with Content-Length 0: {} - {}'.format(
                                req.id, req.url)
                )
                continue
            response = ''
            cache_index[req.id] = {
                'url': req.url,
                'type': req.mime_type,
            }
            self.check_timeout()
            response = self._send_chrome({
                    "id": 0,
                    "method": "Network.getResponseBody",
                    "params": {"requestId": req.id}
            })
            while 'result' not in response:
                try:
                    response = self._receive_chrome()
                except websocket.WebSocketTimeoutException:
                    logger.debug('TIMEOUT REACHED')
                    break
                self.check_timeout()
            if not response:
                logger.error('TIMEOUT FAIL: {} - {}'.format(req.id, req.url))
                continue
            elif 'result' not in response:
                logger.error('RESPONSE FAIL: {} - {}'.format(req.id, req.url))
                logger.debug('response: {}'.format(response))
                logger.debug('content-length: {}'.format(req.content_length))
                continue
            response['result']['content-type'] = req.mime_type
            cfile = self.content_dir + '/{}'.format(req.id)
            with open(cfile, 'w') as f:
                json.dump(response, f, indent=4)
            req_count += 1
        logger.debug('{} content files saved'.format(req_count))
        with open(cache_index_file, 'w') as f:
            json.dump(cache_index, f, indent=4)
        logger.debug('cache index written.')

    def get_cookies(self):
        self.check_timeout()
        response = self._send_chrome({
            "id": 0,
            "method": "Network.getAllCookies",
        })
        while 'result' not in response:
            try:
                response = self._receive_chrome()
            except websocket.WebSocketTimeoutException:
                logger.debug('TIMEOUT REACHED WAITING FOR COOKIES')
                break
        logger.debug(
                'writing cookie log to {}...'.format(self.cookie_log_file))
        with open(self.cookie_log_file, 'w') as f:
            json.dump(response, f, indent=4)
<commit_msg>store screenshot<commit_after>"""
Run Chrome with debugging console enabled
"""

import base64
import logging
import os
import subprocess
import signal
import time
import json
import sys
import websocket
from datetime import datetime
from .remote_shell import ChromeRemoteShell
from .chrome_profile import chrome_profile

logger = logging.getLogger(__name__)

def dict_val(key, dictionary):
    if key in dictionary:
        return dictionary[key]
    elif key.lower() in dictionary:
        return dictionary[key.lower()]
    return None

def dict_intval(key, dictionary):
    if dict_val(key, dictionary) is not None:
        return int(dict_val(key, dictionary))
    return None

class Request(object):

    def __init__(self, request_id):
        self.id = request_id
        self.failed = False
        self.status_code = None
        self.url = None
        self.content_length = None
        self.mime_type = None
        self.complete = False
        self.redirected = []

class ChromeBrowser(object):

    profile = chrome_profile()

    def __init__(
            self,
            socket=9111,
            startup_delay=4,
            shutdown_delay=3,
            remote_shell_timeout=3,
            chrome_bin=None,
            headless=True,
            content_dir=False,
            work_dir='/tmp/chromeremote',
            user_agent=False,
            master_timeout=120,
    ):
        self.work_dir = work_dir
        self.chrome_sock = socket
        self.chrome_pid = False
        self.chrome_log_file = os.path.join(self.work_dir, 'chromelog.json')
        self.cookie_log_file = os.path.join(self.work_dir, 'cookies.json')
        self.content_dir = os.path.join(self.work_dir, 'content')
        if content_dir:
            self.content_dir = content_dir
        self.open_requests = []
        self.chrome_log = []
        self.shell = False
        self.startup_delay = startup_delay
        self.shutdown_delay = shutdown_delay
        self.remote_shell_timeout = remote_shell_timeout
        self.chrome_bin = chrome_bin
        self.headless = headless
        self.reqs = []
        self.user_agent = user_agent
        if self.chrome_bin is None:
            logger.error('chrome binary not found')
            sys.exit(1)
        self.master_timeout = master_timeout

    def _receive_chrome(self):
        response = json.loads(self.shell.soc.recv())
        self.chrome_log.append(response)
        return response

    def _send_chrome(self, data):
        navcom = json.dumps(data)
        self.shell.soc.send(navcom)
        response = self._receive_chrome()
        return response

    def _start_console(self):
        self.shell = ChromeRemoteShell(
                host='localhost',
                port=self.chrome_sock,
                socket_timeout=self.remote_shell_timeout,
        )
        self.shell.connect()
        logger.debug('Socket timeout: {}s'.format(self.shell.soc.gettimeout()))
        self._send_chrome(
                {"id": 0, "method": "Network.enable"})
        self._send_chrome(
                {"id": 0, "method": "DOMStorage.enable"})
        if self.user_agent:
            self._send_chrome(
                    {
                            "id": 0,
                            "method": "Network.setUserAgentOverride",
                            "params": {
                                    "userAgent": self.user_agent,
                            }
                    }
            )

    def _get_requests(self, redirect_only=False):
        requests = {}
        for entry in self.chrome_log:
            if 'method' not in entry \
                    or not entry['method'].startswith('Network.'):
                continue
            rp = entry['params']
            req_id = rp['requestId']
            if entry['method'] == 'Network.requestWillBeSent':
                if 'redirectResponse' in rp:
                    rdr = rp['redirectResponse']
                    last_req = requests[req_id]
                    last_req.complete = True
                    last_req.status_code = rdr['status']
                    requests[req_id] = Request(req_id)
                    if len(last_req.redirected) > 0:
                        requests[req_id].redirected = last_req.redirected
                        last_req.redirected = []
                    requests[req_id].redirected.append(last_req)
                else:
                    requests[req_id] = Request(req_id)
                requests[req_id].url = rp['request']['url']
            elif entry['method'] == 'Network.requestServedFromCache':
                requests[req_id] = Request(req_id)
            elif entry['method'] == 'Network.responseReceived':
                rpr = rp['response']
                requests[req_id].mime_type = rpr['mimeType']
                requests[req_id].status_code = rpr['status']
                requests[req_id].content_length = dict_intval(
                        'Content-Length', rpr['headers'])
            elif entry['method'] == 'Network.loadingFinished':
                requests[req_id].complete = True
            elif entry['method'] == 'Network.loadingFailed':
                requests[req_id].complete = True
                requests[req_id].failed = True
        for req in sorted(requests):
            r = requests[req]
            if r.url is None:
                continue
            if r.url.startswith('data'):
                continue
            if redirect_only:
                if len(r.redirected) > 0:
                    self.reqs.append(r)
            else:
                self.reqs.append(r)

    def _read_data(self, data=False):
        domstorage_activities = 0
        while True:
            if data and 'method' in data:
                if data['method'] == 'Network.requestWillBeSent' \
                        or data['method'] == 'Network.requestServedFromCache':
                    request_id = data['params']['requestId']
                    # logger.debug('open req {}'.format(request_id))
                    if request_id not in self.open_requests:
                        self.open_requests.append(request_id)
                    domstorage_activities = 0
                elif data['method'] == 'Network.loadingFinished':
                    request_id = data['params']['requestId']
                    # logger.debug('finished req {}'.format(request_id))
                    try:
                        self.open_requests.remove(request_id)
                    except ValueError:
                        logger.error(
                                'loadingFinished but request {} not found'
                                ' in open requests'.format(request_id)
                        )
                    domstorage_activities = 0
                elif data['method'] == 'Network.loadingFailed':
                    request_id = data['params']['requestId']
                    logger.debug('loading failed on req {}'.format(request_id))
                    try:
                        self.open_requests.remove(request_id)
                    except ValueError:
                        logger.error(
                                'request {} not found in open requests'.format(
                                        request_id)
                        )
                    domstorage_activities = 0
                elif data['method'].startswith('DOMStorage'):
                    domstorage_activities += 1
                elif data['method'] in (
                        'Network.dataReceived', 'Network.responseReceived'):
                    domstorage_activities = 0
                else:
                    logger.debug(
                            'unexpected data[\'method\']: {}'.format(
                                    data['method'])
                    )
            if domstorage_activities > 20:
                # exit when there is no more network traffic
                logger.debug('looks like a DOMStorage loop. stopping it...')
                self._send_chrome(
                    {"id": 0, "method": "DOMStorage.disable"})
                break
            self.check_timeout()
            try:
                data = self._receive_chrome()
            except websocket.WebSocketTimeoutException:
                logger.debug('TIMEOUT REACHED')
                break

    def start_chrome(self):
        chrome_dir = os.path.join(self.work_dir, 'chrome_profile')
        if not os.path.exists(chrome_dir):
            os.makedirs(chrome_dir)
        logger.debug('Extract Chrome profile to {}...'.format(chrome_dir))
        p = subprocess.call(
                ['tar', 'xz', '-C', chrome_dir, '-f', chrome_profile()])
        logger.debug('Start Chrome...')
        chrome_args = [
                self.chrome_bin,
                '--remote-debugging-port={}'.format(self.chrome_sock),
                '--no-default-browser-check',
                '--user-data-dir={}/chrometemp'.format(chrome_dir),
                '--disable-translate',
                '--net-log-capture-mode=IncludeCookiesAndCredentials',
                '-homepage',
                'about:blank',
                '--disable-extensions']
        if self.headless:
            chrome_args.append('--headless')
        logger.debug(' '.join(chrome_args))
        p = subprocess.Popen(
                chrome_args,
                stdout=subprocess.DEVNULL,
                stderr=subprocess.DEVNULL
        )
        time.sleep(self.startup_delay)
        self.chrome_pid = p.pid
        logger.debug(
                'Chrome PID: {} listening on port {}'.format(
                        p.pid, self.chrome_sock)
        )
        self.start_time = datetime.now()

    def clean_chrome(self):
        logger.debug('Kill Chrome...')
        os.kill(int(self.chrome_pid), signal.SIGTERM)
        logger.debug('Remove chrome profile...')
        time.sleep(self.shutdown_delay)
        subprocess.call(
                ['rm', '-rf', os.path.join(self.work_dir, 'chrome_profile')])

    def check_timeout(self):
        now = datetime.now()
        runtime = now - self.start_time
        if runtime.total_seconds() > self.master_timeout:
            logger.error('chrome master_timeout reached')
            os.kill(int(self.chrome_pid), signal.SIGTERM)
            time.sleep(self.shutdown_delay)
            subprocess.call(
                ['rm', '-rf', os.path.join(self.work_dir, 'chrome_profile')])

    def load_page(self, url):
        if not self.shell:
            self._start_console()
        data = self._send_chrome(
                {
                        "id": 0,
                        "method": "Page.navigate",
                        "params": {
                                "url": url
                        }
                }
        )
        self._read_data(data)
        loopcount = 0
        while len(self.open_requests) > 0 and loopcount < 5:
            self.check_timeout()
            logger.debug('we have {} open requests: {}'.format(
                    len(self.open_requests), self.open_requests))
            self._read_data()
            loopcount += 1
        if len(self.open_requests) > 0:
            logger.debug('open requests: {}'.format(self.open_requests))
        self._send_chrome(
                {"id": 0, "method": "Page.stopLoading"})
        resp = self._send_chrome(
                {
                        "id": 0,
                        "method": "Page.captureScreenshot",
                        "params": {
                            "format": "jpeg",
                            "quality": 80
                        }
                }
        )
        if 'result' in resp:
            scrsht = os.path.join(self.work_dir, 'screenshot.jpg')
            with open(scrsht, 'wb') as f:
                f.write(base64.b64decode(resp['result']['data']))
            logger.debug('screenshot written to {}'.format(scrsht))
        self._read_data()
        logger.debug('writing log to {}...'.format(self.chrome_log_file))
        with open(self.chrome_log_file, 'w') as f:
            json.dump(self.chrome_log, f, indent=4)

    def get_content(self):
        if not os.path.exists(self.content_dir):
            os.makedirs(self.content_dir)
        cache_index_file = '{}/index.json'.format(self.content_dir)
        cache_index = {}
        req_count = 0
        self._get_requests()
        for req in self.reqs:
            if req.failed:
                continue
            if req.status_code not in (200, 206):
                logger.debug('No Content with Status {}: {} - {}'.format(
                        req.status_code, req.id, req.url))
                continue
            if req.content_length == 0:
                logger.debug(
                        'No Content with Content-Length 0: {} - {}'.format(
                                req.id, req.url)
                )
                continue
            response = ''
            cache_index[req.id] = {
                'url': req.url,
                'type': req.mime_type,
            }
            self.check_timeout()
            response = self._send_chrome({
                    "id": 0,
                    "method": "Network.getResponseBody",
                    "params": {"requestId": req.id}
            })
            while 'result' not in response:
                try:
                    response = self._receive_chrome()
                except websocket.WebSocketTimeoutException:
                    logger.debug('TIMEOUT REACHED')
                    break
                self.check_timeout()
            if not response:
                logger.error('TIMEOUT FAIL: {} - {}'.format(req.id, req.url))
                continue
            elif 'result' not in response:
                logger.error('RESPONSE FAIL: {} - {}'.format(req.id, req.url))
                logger.debug('response: {}'.format(response))
                logger.debug('content-length: {}'.format(req.content_length))
                continue
            response['result']['content-type'] = req.mime_type
            cfile = self.content_dir + '/{}'.format(req.id)
            with open(cfile, 'w') as f:
                json.dump(response, f, indent=4)
            req_count += 1
        logger.debug('{} content files saved'.format(req_count))
        with open(cache_index_file, 'w') as f:
            json.dump(cache_index, f, indent=4)
        logger.debug('cache index written.')

    def get_cookies(self):
        self.check_timeout()
        response = self._send_chrome({
            "id": 0,
            "method": "Network.getAllCookies",
        })
        while 'result' not in response:
            try:
                response = self._receive_chrome()
            except websocket.WebSocketTimeoutException:
                logger.debug('TIMEOUT REACHED WAITING FOR COOKIES')
                break
        logger.debug(
                'writing cookie log to {}...'.format(self.cookie_log_file))
        with open(self.cookie_log_file, 'w') as f:
            json.dump(response, f, indent=4)
<|endoftext|>


<commit_before>try:
    # CKAN 2.7 and later
    from ckan.common import config
except ImportError:
    # CKAN 2.6 and earlier
    from pylons import config
import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit
from ckanext.filtersearch import helpers
import pprint
import re
import json

def get_topic_field():
    # Get the value of the ckan.iauthfunctions.users_can_create_groups
    # setting from the CKAN config file as a string, or False if the setting
    # isn't in the config file.
    topic_field = config.get(
        'ckanext.filtersearch.topic_field', False)

    if topic_field:
        return 'extras_' + topic_field
    else:
        return None

class FiltersearchPlugin(plugins.SingletonPlugin):
    plugins.implements(plugins.IConfigurer)
    plugins.implements(plugins.IFacets)
    plugins.implements(plugins.ITemplateHelpers)
    #plugins.implements(plugins.IRoutes, inherit=True)
    plugins.implements(plugins.IPackageController, inherit=True)
    #plugins.implements(plugins.IOrganizationController, inherit=True)
    #plugins.implements(plugins.IGroupController, inherit=True)

    # IConfigurer
    def update_config(self, config_):
        toolkit.add_template_directory(config_, 'templates')
        toolkit.add_public_directory(config_, 'public')
        toolkit.add_resource('fanstatic', 'filtersearch')
        self._topic_field =  get_topic_field()

    # ITemplateHelpers
    def get_helpers(self):
        return {
            'filtersearch_get_topic': helpers.filtersearch_get_topic,
            'filtersearch_get_items': helpers.filtersearch_get_items,
            'filtersearch_get_topic_field': helpers.filtersearch_get_topic_field,
            'filtersearch_get_bbox': helpers.filtersearch_get_bbox,
            'filtersearch_get_date_value': helpers.filtersearch_get_date_value,
            'filtersearch_check_resource_field': helpers.filtersearch_check_resource_field,
            'filtersearch_get_search_facets_from_fields': helpers.filtersearch_get_search_facets_from_fields
        }

    def topic_field (self):
        return self._topic_field

    # IFacets
    def dataset_facets(self, facets_dict, package_type):
        return self._facets(facets_dict)
    def group_facets(self, facets_dict, group_type, package_type):
        return self._facets(facets_dict)
    def organization_facets(self, facets_dict, organization_type, package_type):
        return self._facets(facets_dict)

    def _facets(self, facets_dict):
        # Reorder facets

        # Delete facets
        facets_dict.pop('organization', None)
        facets_dict.pop('license_id', None)
        facets_dict.pop('res_format', None)
        facets_dict.pop('groups', None)
        facets_dict.pop('tags', None)
        #facets_dict.pop('author', None)

        # Add them
        #facets_dict['extras_iso_exTempStart'] = 'Temporal Extend Start'
        #facets_dict['extras_iso_exTempEnd'] = 'Temporal Extend End'
        #facets_dict['metadata_modified'] = 'Last modified'

        if self._topic_field:
            facets_dict[self._topic_field] = 'Categories'

        #print self._topic_field

        facets_dict['tags'] = 'Keywords'
        facets_dict['author'] = 'Authors'
        facets_dict['organization'] = 'Organizations'
        facets_dict['groups'] = 'Groups'
        facets_dict['license_id'] = 'Licenses'
        facets_dict['res_format'] = 'Formats'
        facets_dict['res_extras_par_model'] = 'Model'
        facets_dict['res_extras_par_experiment'] = 'Experiment'
        facets_dict['res_extras_par_frequency'] = 'Frequency'
        facets_dict['res_extras_par_variables'] = 'Variables'
        facets_dict['res_extras_par_ensemble'] = 'Ensemble'

        return facets_dict

    # IPackageController
    def after_search(self, search_results, search_params):
        #print("Params  -------------------------------------")
        #pprint.pprint(search_params)
        #print("Results -------------------------------------")
        #pprint.pprint(search_results)
        # Extract resource facet params from fq
        #print "after_search"
        #print search_results
        #print search_results['results']

        search_items = ["res_format", "res_extras_par_frequency", "res_extras_par_model", "res_extras_par_experiment","res_extras_par_variables","res_extras_par_ensemble"]
        package_text = ["format", "par_frequency", "par_model", "par_experiment","par_variables","par_ensemble" ]
        max_values_per_item = 40
        num_search_items = len (search_items)
        search_values = [[None for x in range(len(search_items))] for y in range(max_values_per_item)]

        fq = toolkit.get_or_bust(search_params, 'fq')
        #print (fq)

        if (fq[0].find('res_format') == -1) and (fq[0].find('par_') == -1):
            # Check versions and modify resource_list accordingly
            new_resource_list = []
            for pkg in search_results['results']:
                 resource_list = pkg['resources']
                 new_resource_list = []
                 #print resource_list
                 for resource in resource_list:
                     # Consider resource versions for Kathi; 27.6.17
                     #print resource
                     if 'newer_version' in resource:
                         #print "newer_version"
                         if resource['newer_version'] != '':
                              continue
                         else:
                             new_resource_list.append(resource)
                     pkg['resources'] = new_resource_list

            return search_results

        len_s = len(search_items)
        #print "otto"
        i = -1

        for item in search_items:
            i += 1
            if fq[0].find(search_items[i]) == -1:
                continue
            x = [m.start() for m in re.finditer(search_items[i], fq[0])]
            j = -1
            for val in x:
                j += 1
                tmp_str = fq[0][val:-1].split(' ')[0]
                tmp_str =  tmp_str.split(':')[1]
                if tmp_str.startswith('"') and tmp_str.endswith('"'):
                    tmp_str = tmp_str[1:-1]
                search_values[i][j] = tmp_str
                #print tmp_str

        #print search_values
        try:
              # Filter resources from package_search result :
              # logical or within one facet logical and across all facets
              #print search_results['results']
              for pkg in search_results['results']:
                  pkg['total_resources'] = len(pkg['resources'])
                  resource_list = pkg['resources']
                  new_resource_list = []
                  #print resource_list
                  for resource in resource_list:
                      # Consider resource versions for Kathi; 27.6.17
                      #print resource
                      if 'newer_version' in resource:
                          #print "newer_version"
                          if resource['newer_version'] != '':
                               pkg['total_resources'] -= 1
                               continue
                      num_matches = [False for x in range(num_search_items)]
                      for i in range(num_search_items):
                          #print search_values[i][0]
                          if search_values[i][0] == None:
                              #print "None Found: " + str(i)
                              num_matches[i] = True     # ie. parameter was not specified
                              continue
                          for j in range (len(search_values[i])):
                              if search_values[i][j] == None:
                                 break
                              #print resource
                              #print search_values[i][j]
                              #print resource.get(package_text[i])
                              #print resource.get('par_variables')
                              if resource.get(package_text[i]) == search_values[i][j]:
                                 num_matches[i] = True

                      append_resource = True
                      #print num_matches
                      for i in range (num_search_items):
                          if num_matches[i] == False:
                             append_resource = False

                      if append_resource:
                          new_resource_list.append(resource)

                  pkg['resources'] = new_resource_list

        except Exception as e:
            print("Exception: " + str(e))

        return search_results
<commit_msg>bug fix<commit_after>try:
    # CKAN 2.7 and later
    from ckan.common import config
except ImportError:
    # CKAN 2.6 and earlier
    from pylons import config
import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit
from ckanext.filtersearch import helpers
import pprint
import re
import json

def get_topic_field():
    # Get the value of the ckan.iauthfunctions.users_can_create_groups
    # setting from the CKAN config file as a string, or False if the setting
    # isn't in the config file.
    topic_field = config.get(
        'ckanext.filtersearch.topic_field', False)

    if topic_field:
        return 'extras_' + topic_field
    else:
        return None

class FiltersearchPlugin(plugins.SingletonPlugin):
    plugins.implements(plugins.IConfigurer)
    plugins.implements(plugins.IFacets)
    plugins.implements(plugins.ITemplateHelpers)
    #plugins.implements(plugins.IRoutes, inherit=True)
    plugins.implements(plugins.IPackageController, inherit=True)
    #plugins.implements(plugins.IOrganizationController, inherit=True)
    #plugins.implements(plugins.IGroupController, inherit=True)

    # IConfigurer
    def update_config(self, config_):
        toolkit.add_template_directory(config_, 'templates')
        toolkit.add_public_directory(config_, 'public')
        toolkit.add_resource('fanstatic', 'filtersearch')
        self._topic_field =  get_topic_field()

    # ITemplateHelpers
    def get_helpers(self):
        return {
            'filtersearch_get_topic': helpers.filtersearch_get_topic,
            'filtersearch_get_items': helpers.filtersearch_get_items,
            'filtersearch_get_topic_field': helpers.filtersearch_get_topic_field,
            'filtersearch_get_bbox': helpers.filtersearch_get_bbox,
            'filtersearch_get_date_value': helpers.filtersearch_get_date_value,
            'filtersearch_check_resource_field': helpers.filtersearch_check_resource_field,
            'filtersearch_get_search_facets_from_fields': helpers.filtersearch_get_search_facets_from_fields
        }

    def topic_field (self):
        return self._topic_field

    # IFacets
    def dataset_facets(self, facets_dict, package_type):
        return self._facets(facets_dict)
    def group_facets(self, facets_dict, group_type, package_type):
        return self._facets(facets_dict)
    def organization_facets(self, facets_dict, organization_type, package_type):
        return self._facets(facets_dict)

    def _facets(self, facets_dict):
        # Reorder facets

        # Delete facets
        facets_dict.pop('organization', None)
        facets_dict.pop('license_id', None)
        facets_dict.pop('res_format', None)
        facets_dict.pop('groups', None)
        facets_dict.pop('tags', None)
        #facets_dict.pop('author', None)

        # Add them
        #facets_dict['extras_iso_exTempStart'] = 'Temporal Extend Start'
        #facets_dict['extras_iso_exTempEnd'] = 'Temporal Extend End'
        #facets_dict['metadata_modified'] = 'Last modified'

        if self._topic_field:
            facets_dict[self._topic_field] = 'Categories'

        #print self._topic_field

        facets_dict['tags'] = 'Keywords'
        facets_dict['author'] = 'Authors'
        facets_dict['organization'] = 'Organizations'
        facets_dict['groups'] = 'Groups'
        facets_dict['license_id'] = 'Licenses'
        facets_dict['res_format'] = 'Formats'
        facets_dict['res_extras_par_model'] = 'Model'
        facets_dict['res_extras_par_experiment'] = 'Experiment'
        facets_dict['res_extras_par_frequency'] = 'Frequency'
        facets_dict['res_extras_par_variables'] = 'Variables'
        facets_dict['res_extras_par_ensemble'] = 'Ensemble'

        return facets_dict

    # IPackageController
    def after_search(self, search_results, search_params):
        #print("Params  -------------------------------------")
        #pprint.pprint(search_params)
        #print("Results -------------------------------------")
        #pprint.pprint(search_results)
        # Extract resource facet params from fq
        #print "after_search"
        #print search_results
        #print search_results['results']

        search_items = ["res_format", "res_extras_par_frequency", "res_extras_par_model", "res_extras_par_experiment","res_extras_par_variables","res_extras_par_ensemble"]
        package_text = ["format", "par_frequency", "par_model", "par_experiment","par_variables","par_ensemble" ]
        max_values_per_item = 40
        num_search_items = len (search_items)
        search_values = [[None for x in range(len(search_items))] for y in range(max_values_per_item)]

        fq = toolkit.get_or_bust(search_params, 'fq')
        #print (fq)

        if (fq[0].find('res_format') == -1) and (fq[0].find('par_') == -1):
            # Check versions and modify resource_list accordingly
            for pkg in search_results['results']:
                 resource_list = pkg['resources']
                 #print resource_list
                 new_resource_list = []
                 for resource in resource_list:
                     # Consider resource versions for Kathi; 27.6.17
                     #print resource
                     if 'newer_version' in resource:
                         #print "newer_version"
                         if resource['newer_version'] != '':
                              continue
                         else:
                             new_resource_list.append(resource)
                 pkg['resources'] = new_resource_list

            return search_results

        len_s = len(search_items)
        #print "otto"
        i = -1

        for item in search_items:
            i += 1
            if fq[0].find(search_items[i]) == -1:
                continue
            x = [m.start() for m in re.finditer(search_items[i], fq[0])]
            j = -1
            for val in x:
                j += 1
                tmp_str = fq[0][val:-1].split(' ')[0]
                tmp_str =  tmp_str.split(':')[1]
                if tmp_str.startswith('"') and tmp_str.endswith('"'):
                    tmp_str = tmp_str[1:-1]
                search_values[i][j] = tmp_str
                #print tmp_str

        #print search_values
        try:
              # Filter resources from package_search result :
              # logical or within one facet logical and across all facets
              #print search_results['results']
              for pkg in search_results['results']:
                  pkg['total_resources'] = len(pkg['resources'])
                  resource_list = pkg['resources']
                  new_resource_list = []
                  #print resource_list
                  for resource in resource_list:
                      # Consider resource versions for Kathi; 27.6.17
                      #print resource
                      if 'newer_version' in resource:
                          #print "newer_version"
                          if resource['newer_version'] != '':
                               pkg['total_resources'] -= 1
                               continue
                      num_matches = [False for x in range(num_search_items)]
                      for i in range(num_search_items):
                          #print search_values[i][0]
                          if search_values[i][0] == None:
                              #print "None Found: " + str(i)
                              num_matches[i] = True     # ie. parameter was not specified
                              continue
                          for j in range (len(search_values[i])):
                              if search_values[i][j] == None:
                                 break
                              #print resource
                              #print search_values[i][j]
                              #print resource.get(package_text[i])
                              #print resource.get('par_variables')
                              if resource.get(package_text[i]) == search_values[i][j]:
                                 num_matches[i] = True

                      append_resource = True
                      #print num_matches
                      for i in range (num_search_items):
                          if num_matches[i] == False:
                             append_resource = False

                      if append_resource:
                          new_resource_list.append(resource)

                  pkg['resources'] = new_resource_list

        except Exception as e:
            print("Exception: " + str(e))

        return search_results
<|endoftext|>


<commit_before>'''
Created on Nov 7, 2012

@author: dstrauss
'''

import numpy as np
D = {'solverType':'contrastX', 'flavor':'TE', 'numRuns':3, 'expt':'testThree'}

def getMyVars(parseNumber, D):
    '''routine to return the parameters to test at the current iteration.'''

    if (parseNumber == 0):
        D['freqs'] = np.array([1e3])
        D['numProcs'] = 1
        D['numSensors'] = 2010
    elif (parseNumber == 1):
        D['freqs'] = np.array([1e3, 25e3])
        D['numProcs'] = 2
        D['numSensors'] = 400
    elif (parseNumber == 2):
        D['freqs'] = np.array([25e3])
        D['numProcs'] = 1
        D['numSensors'] = 400
    elif (parseNumber == 3):
        D['freqs'] = np.linspace(1e3,25e3,6)
        D['numProcs'] = 6
        D['numSensors'] = 400

    D['lam'] = 1e-8
    D['rho'] = 1e-3
    D['inc'] = np.array([75*np.pi/180])
    D['bkgNo'] =  0
    D['maxIter'] = 200

    return D
<commit_msg>rho at 0.1!<commit_after>'''
Created on Nov 7, 2012

@author: dstrauss
'''

import numpy as np
D = {'solverType':'contrastX', 'flavor':'TE', 'numRuns':3, 'expt':'testThree'}

def getMyVars(parseNumber, D):
    '''routine to return the parameters to test at the current iteration.'''

    if (parseNumber == 0):
        D['freqs'] = np.array([1e3])
        D['numProcs'] = 1
        D['numSensors'] = 2010
    elif (parseNumber == 1):
        D['freqs'] = np.array([1e3, 25e3])
        D['numProcs'] = 2
        D['numSensors'] = 400
    elif (parseNumber == 2):
        D['freqs'] = np.array([25e3])
        D['numProcs'] = 1
        D['numSensors'] = 400
    elif (parseNumber == 3):
        D['freqs'] = np.linspace(1e3,25e3,6)
        D['numProcs'] = 6
        D['numSensors'] = 400

    D['lam'] = 1e-8
    D['rho'] = 0.1
    D['inc'] = np.array([75*np.pi/180])
    D['bkgNo'] =  0
    D['maxIter'] = 200

    return D
<|endoftext|>


<commit_before>import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit
from ckan.logic.action import get, create, update, delete
from ckan.model.package import Package
from ckan.model.resource import Resource
from ckan.lib.base import c
import logging
import ConfigParser
import os
import requests
import json
import time

config = ConfigParser.ConfigParser()
config.read(os.environ['CKAN_CONFIG'])

PLUGIN_SECTION = 'plugin:logging'
LOGGING_URL = config.get(PLUGIN_SECTION, 'logging_url')
APP_ID = config.get(PLUGIN_SECTION, 'app_id')

log = logging.getLogger(__name__)

def send_log(context, pkg_dict, msg, _type, id_keyword):
    current_time = time.time()
    log.debug('%s at %s' % (msg, current_time))
    user = context['auth_user_obj']
    custom_attr = {id_keyword: pkg_dict["id"]}
    if user is not None:
        custom_attr['UserID'] = user.id
    data = {'msg': msg,
            'appId': APP_ID,
            'type': _type,
            'timestamp': current_time,
            'custom_attr': custom_attr
            }
    response = requests.post(LOGGING_URL + '/log/ods', data=json.dumps(data),
                             headers={'Content-type': 'application/json'},
                             verify=False)
    if response.status_code >= 400:
        log.debug(response.content)

def send_dataset_log(context, pkg_dict, msg, _type):
    send_log(context, pkg_dict, msg, _type, 'DatasetID')

def send_resource_log(context, pkg_dict, msg, _type):
    send_log(context, pkg_dict, msg, _type, 'ResourceID')

@toolkit.side_effect_free
def package_show(context, data_dict):
    package_dict = get.package_show(context, data_dict)
    package = Package.get(package_dict['id'])
    package_dict['ratings'] = package.get_average_rating()
    # if package_dict['type'] == 'dataset':
    #     send_log(context, package_dict, 'Dataset metadata accessed',
    #              'DatasetMetadataAccessed')
    return package_dict

def package_create(context, data_dict):
    package_dict = create.package_create(context, data_dict)
    if package_dict['type'] == 'dataset':
        send_dataset_log(context, package_dict, 'Dataset created',
                         'DatasetPublished')

def package_update(context, data_dict):
    package_dict = update.package_update(context, data_dict)
    if package_dict['type'] == 'dataset':
        send_dataset_log(context, package_dict, 'Dataset updated',
                         'DatasetMetadataUpdated')
    return package_dict

def package_delete(context, data_dict):
    model = context['model']
    delete.package_delete(context, data_dict)
    package = Package.get(data_dict['id'])
    package.purge()
    model.repo.commit_and_remove()
    send_dataset_log(context, data_dict, 'Dataset removed',
                     'DatasetRemoved')
    return None

@toolkit.side_effect_free
def resource_show(context, data_dict):
    resource_dict = get.resource_show(context, data_dict)
    # send_resource_log(context, resource_dict, 'Resource metadata accessed',
    #                   'ResourceMetadataAccessed')

    return resource_dict

def resource_create(context, data_dict):
    if 'url' not in data_dict:
        url = ""
        data_dict['url'] = url
    resource_dict = create.resource_create(context, data_dict)
    send_resource_log(context, resource_dict, 'Resource created',
                      'ResourcePublished')
    return resource_dict

def resource_update(context, data_dict):
    model = context['model']
    resource = Resource.get(data_dict['id'])
    extras = resource.extras
    if resource is not None and 'url' not in data_dict:
        url = resource.url
        data_dict['url'] = url
    resource_dict = update.resource_update(context, data_dict)
    resource = Resource.get(data_dict['id'])
    if len(resource.extras) <= 0:
        resource.extras = extras
    model.repo.commit()
    for key in resource.extras:
        resource_dict[key] = resource.extras[key]

    send_resource_log(context, resource_dict, 'Resource metadata updated',
                      'ResourceMetadataUpdated')

    return resource_dict

def resource_delete(context, data_dict):
    delete.resource_delete(context, data_dict)
    send_resource_log(context, data_dict, 'Resource removed',
                      'ResourceRemoved')

def send_dataset_log_helper(pkg_dict, msg, _type):
    send_dataset_log({'auth_user_obj': c.userobj}, pkg_dict, msg, _type)

def send_resource_log_helper(resource_dict, msg, _type):
    send_resource_log({'auth_user_obj': c.userobj}, resource_dict, msg, _type)

class Welive_UtilsPlugin(plugins.SingletonPlugin, toolkit.DefaultDatasetForm):
    plugins.implements(plugins.IConfigurer, inherit=False)
    plugins.implements(plugins.IActions)
    plugins.implements(plugins.IDatasetForm, inherit=False)
    plugins.implements(plugins.ITemplateHelpers)

    # ITemplateHelpers
    def get_helpers(self):
        return {'send_dataset_log_helper': send_dataset_log_helper,
                'send_resource_log_helper': send_resource_log_helper}

    # IConfigurer

    def update_config(self, config_):
        toolkit.add_template_directory(config_, 'templates')
        toolkit.add_public_directory(config_, 'public')
        toolkit.add_resource('fanstatic', 'welive_utils')

    # IActions

    def get_actions(self):
        return {'package_show': package_show,
                'package_create': package_create,
                'package_update': package_update,
                'package_delete': package_delete,
                'resource_show': resource_show,
                'resource_create': resource_create,
                'resource_update': resource_update,
                'resource_delete': resource_delete
                }

    # IDatasetForm

    def is_fallback(self):
        # Return True to register this plugin as the default handler for
        # package types not handled by any other IDatasetForm plugin.
        return True

    def package_types(self):
        # This plugin doesn't handle any special package types, it just
        # registers itself as the default (above).
        return ['dataset']

    def create_package_schema(self):
        schema = super(Welive_UtilsPlugin, self).create_package_schema()
        schema.update({
            'language': [toolkit.get_converter('convert_to_extras')]
        })
        return schema

    def update_package_schema(self):
        schema = super(Welive_UtilsPlugin, self).update_package_schema()
        # our custom field
        schema.update({
            'language': [toolkit.get_converter('convert_to_extras')]
        })
        return schema

    def show_package_schema(self):
        schema = super(Welive_UtilsPlugin, self).show_package_schema()
        schema.update({
            'language': [toolkit.get_converter('convert_from_extras')]
        })
        return schema
<commit_msg>Fixing bugs<commit_after>import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit
from ckan.logic.action import get, create, update, delete
from ckan.model.package import Package
from ckan.model.resource import Resource
from ckan.lib.base import c
import logging
import ConfigParser
import os
import requests
import json
import time

config = ConfigParser.ConfigParser()
config.read(os.environ['CKAN_CONFIG'])

PLUGIN_SECTION = 'plugin:logging'
LOGGING_URL = config.get(PLUGIN_SECTION, 'logging_url')
APP_ID = config.get(PLUGIN_SECTION, 'app_id')

log = logging.getLogger(__name__)

def send_log(context, pkg_dict, msg, _type, id_keyword):
    current_time = time.time()
    log.debug('%s at %s' % (msg, current_time))
    user = context['auth_user_obj']
    custom_attr = {id_keyword: pkg_dict["id"]}
    if user is not None:
        custom_attr['UserID'] = user.id
    data = {'msg': msg,
            'appId': APP_ID,
            'type': _type,
            'timestamp': current_time,
            'custom_attr': custom_attr
            }
    response = requests.post(LOGGING_URL + '/log/ods', data=json.dumps(data),
                             headers={'Content-type': 'application/json'},
                             verify=False)
    if response.status_code >= 400:
        log.debug(response.content)

def send_dataset_log(context, pkg_dict, msg, _type):
    send_log(context, pkg_dict, msg, _type, 'DatasetID')

def send_resource_log(context, pkg_dict, msg, _type):
    send_log(context, pkg_dict, msg, _type, 'ResourceID')

@toolkit.side_effect_free
def package_show(context, data_dict):
    package_dict = get.package_show(context, data_dict)
    package = Package.get(package_dict['id'])
    package_dict['ratings'] = package.get_average_rating()
    # if package_dict['type'] == 'dataset':
    #     send_log(context, package_dict, 'Dataset metadata accessed',
    #              'DatasetMetadataAccessed')
    return package_dict

def package_create(context, data_dict):
    package_dict = create.package_create(context, data_dict)
    if package_dict['type'] == 'dataset':
        send_dataset_log(context, package_dict, 'Dataset created',
                         'DatasetPublished')

def package_update(context, data_dict):
    package_dict = update.package_update(context, data_dict)
    if package_dict['type'] == 'dataset':
        send_dataset_log(context, package_dict, 'Dataset updated',
                         'DatasetMetadataUpdated')
    return package_dict

def package_delete(context, data_dict):
    model = context['model']
    delete.package_delete(context, data_dict)
    package = Package.get(data_dict['id'])
    package.purge()
    model.repo.commit_and_remove()
    send_dataset_log(context, data_dict, 'Dataset removed',
                     'DatasetRemoved')
    return None

@toolkit.side_effect_free
def resource_show(context, data_dict):
    resource_dict = get.resource_show(context, data_dict)
    # send_resource_log(context, resource_dict, 'Resource metadata accessed',
    #                   'ResourceMetadataAccessed')

    return resource_dict

def resource_create(context, data_dict):
    if 'url' not in data_dict:
        url = ""
        data_dict['url'] = url
    resource_dict = create.resource_create(context, data_dict)
    send_resource_log(context, resource_dict, 'Resource created',
                      'ResourcePublished')
    return resource_dict

def resource_update(context, data_dict):
    model = context['model']
    resource = Resource.get(data_dict['id'])
    extras = resource.extras
    if resource is not None and 'url' not in data_dict:
        url = resource.url
        data_dict['url'] = url
    resource_dict = update.resource_update(context, data_dict)
    resource = Resource.get(data_dict['id'])
    if len(resource.extras) <= 0:
        resource.extras = extras
    model.repo.commit()
    for key in resource.extras:
        resource_dict[key] = resource.extras[key]

    send_resource_log(context, resource_dict, 'Resource metadata updated',
                      'ResourceMetadataUpdated')

    return resource_dict

def resource_delete(context, data_dict):
    delete.resource_delete(context, data_dict)
    send_resource_log(context, data_dict, 'Resource removed',
                      'ResourceRemoved')

def send_dataset_log_helper(pkg_dict, msg, _type):
    send_dataset_log({'auth_user_obj': c.userobj}, pkg_dict, msg, _type)

def send_resource_log_helper(resource_dict, msg, _type):
    send_resource_log({'auth_user_obj': c.userobj}, resource_dict, msg, _type)

class Welive_UtilsPlugin(plugins.SingletonPlugin, toolkit.DefaultDatasetForm):
    plugins.implements(plugins.IConfigurer, inherit=False)
    plugins.implements(plugins.IActions)
    plugins.implements(plugins.IDatasetForm, inherit=False)
    plugins.implements(plugins.ITemplateHelpers)

    # ITemplateHelpers
    def get_helpers(self):
        return {'send_dataset_log_helper': send_dataset_log_helper,
                'send_resource_log_helper': send_resource_log_helper}

    # IConfigurer

    def update_config(self, config_):
        toolkit.add_template_directory(config_, 'templates')
        toolkit.add_public_directory(config_, 'public')
        toolkit.add_resource('fanstatic', 'welive_utils')

    # IActions

    def get_actions(self):
        return {'package_show': package_show,
                'package_create': package_create,
                'package_update': package_update,
                'package_delete': package_delete,
                'resource_show': resource_show,
                'resource_create': resource_create,
                'resource_update': resource_update,
                'resource_delete': resource_delete
                }

    # IDatasetForm

    def is_fallback(self):
        # Return True to register this plugin as the default handler for
        # package types not handled by any other IDatasetForm plugin.
        return False

    def package_types(self):
        # This plugin doesn't handle any special package types, it just
        # registers itself as the default (above).
        return ['dataset']

    def create_package_schema(self):
        schema = super(Welive_UtilsPlugin, self).create_package_schema()
        schema.update({
            'language': [toolkit.get_converter('convert_to_extras')]
        })
        return schema

    def update_package_schema(self):
        schema = super(Welive_UtilsPlugin, self).update_package_schema()
        # our custom field
        schema.update({
            'language': [toolkit.get_converter('convert_to_extras')]
        })
        return schema

    def show_package_schema(self):
        schema = super(Welive_UtilsPlugin, self).show_package_schema()
        schema.update({
            'language': [toolkit.get_converter('convert_from_extras')]
        })
        return schema
<|endoftext|>