<commit_before>import six

from .node import Node
from .utils import parse_date

@six.python_2_unicode_compatible
class Organization(Node):
    """Represents an Organization on CrunchBase
    API Docs: https://developer.crunchbase.com/docs
    """

    KNOWN_RELATIONSHIPS = [
        'acquisitions',
        'board_members_and_advisors',
        'categories',
        'competitors',
        'current_team',
        'customers',
        'founders',
        'funding_rounds',
        'headquarters',
        'images',
        'investments',
        'ipo',
        'members',
        'news',
        'offices',
        'past_team',
        'primary_image',
        'products',
        'sub_organizations'
        'websites',
    ]

    KNOWN_PROPERTIES = [
        'closed_on',
        'description',
        'founded_on',
        'homepage_url',
        'name',
        'number_of_employees',
        'number_of_investments',
        'permalink',
        'short_description',
        'stock_symbol',
        'total_funding_usd',
    ]

    def _coerce_values(self):
        """A delegate method to handle parsing all data and converting
        them into python values
        """
        # special cases to convert strings to pythonic value
        for attr in ['closed_on', 'founded_on']:
            if getattr(self, attr, None):
                setattr(self, attr, parse_date(getattr(self, attr)))
        for attr in ['number_of_employees', 'number_of_investments']:
            if getattr(self, attr, None):
                setattr(self, attr, int(getattr(self, attr)))

    def __str__(self):
        return 'Organization: %s' % self.name

    def __repr__(self):
        return self.__str__()
<commit_msg>Fix #9<commit_after>import six

from .node import Node
from .utils import parse_date

@six.python_2_unicode_compatible
class Organization(Node):
    """Represents an Organization on CrunchBase
    API Docs: https://developer.crunchbase.com/docs
    """

    KNOWN_RELATIONSHIPS = [
        'acquisitions',
        'board_members_and_advisors',
        'categories',
        'competitors',
        'current_team',
        'customers',
        'founders',
        'funding_rounds',
        'headquarters',
        'images',
        'investments',
        'ipo',
        'members',
        'news',
        'offices',
        'past_team',
        'primary_image',
        'products',
        'sub_organizations',
        'websites',
    ]

    KNOWN_PROPERTIES = [
        'closed_on',
        'description',
        'founded_on',
        'homepage_url',
        'name',
        'number_of_employees',
        'number_of_investments',
        'permalink',
        'short_description',
        'stock_symbol',
        'total_funding_usd',
    ]

    def _coerce_values(self):
        """A delegate method to handle parsing all data and converting
        them into python values
        """
        # special cases to convert strings to pythonic value
        for attr in ['closed_on', 'founded_on']:
            if getattr(self, attr, None):
                setattr(self, attr, parse_date(getattr(self, attr)))
        for attr in ['number_of_employees', 'number_of_investments']:
            if getattr(self, attr, None):
                setattr(self, attr, int(getattr(self, attr)))

    def __str__(self):
        return 'Organization: %s' % self.name

    def __repr__(self):
        return self.__str__()
<|endoftext|>


<commit_before>from functools import partial

import six

from graphene import Field, Interface
from graphene.types.objecttype import ObjectType, ObjectTypeMeta, attrs_without_fields, GrapheneObjectType, get_interfaces
from graphene.relay import Node
from graphene.relay.node import NodeMeta
from .converter import convert_django_field_with_choices
from graphene.types.options import Options
from .utils import get_model_fields, is_valid_django_model, DJANGO_FILTER_INSTALLED
from .registry import Registry, get_global_registry
from graphene.utils.is_base_type import is_base_type
from graphene.utils.copy_fields import copy_fields
from graphene.utils.get_graphql_type import get_graphql_type
from graphene.utils.get_fields import get_fields
from graphene.utils.as_field import as_field

class DjangoObjectTypeMeta(ObjectTypeMeta):
    def _construct_fields(cls, fields, options):
        _model_fields = get_model_fields(options.model)

        for field in _model_fields:
            name = field.name
            is_not_in_only = options.fields and name not in options.fields
            is_already_created = name in fields
            is_excluded = field.name in options.exclude or is_already_created
            if is_not_in_only or is_excluded:
                # We skip this field if we specify only_fields and is not
                # in there. Or when we exclude this field in exclude_fields
                continue
            converted = convert_django_field_with_choices(field, options.registry)
            if not converted:
                continue
            fields[name] = as_field(converted)

        fields = copy_fields(Field, fields, parent=cls)

        return fields

    @staticmethod
    def _create_objecttype(cls, name, bases, attrs):
        # super_new = super(DjangoObjectTypeMeta, cls).__new__
        super_new = type.__new__

        # Also ensure initialization is only performed for subclasses of Model
        # (excluding Model class itself).
        if not is_base_type(bases, DjangoObjectTypeMeta):
            return super_new(cls, name, bases, attrs)

        defaults = dict(
            name=None,
            description=None,
            model=None,
            fields=(),
            exclude=(),
            interfaces=(),
            registry=None
        )
        if DJANGO_FILTER_INSTALLED:
            # In case Django filter is available, then
            # we allow more attributes in Meta
            defaults = dict(
                defaults,
                filter_fields=(),
                filter_order_by=(),
            )

        options = Options(
            attrs.pop('Meta', None),
            **defaults
        )
        if not options.registry:
            options.registry = get_global_registry()
        assert isinstance(options.registry, Registry), 'The attribute registry in {}.Meta needs to be an instance of Registry, received "{}".'.format(name, options.registry)
        assert is_valid_django_model(options.model), 'You need to pass a valid Django Model in {}.Meta, received "{}".'.format(name, options.model)

        interfaces = tuple(options.interfaces)
        fields = get_fields(ObjectType, attrs, bases, interfaces)
        attrs = attrs_without_fields(attrs, fields)
        cls = super_new(cls, name, bases, dict(attrs, _meta=options))

        base_interfaces = tuple(b for b in bases if issubclass(b, Interface))
        options.graphql_type = GrapheneObjectType(
            graphene_type=cls,
            name=options.name or cls.__name__,
            description=options.description or cls.__doc__,
            fields=partial(cls._construct_fields, fields, options),
            interfaces=tuple(get_interfaces(interfaces + base_interfaces))
        )
        options.get_fields = partial(cls._construct_fields, fields, options)

        if issubclass(cls, DjangoObjectType):
            options.registry.register(cls)

        return cls

class DjangoObjectType(six.with_metaclass(DjangoObjectTypeMeta, ObjectType)):
    pass

class DjangoNodeMeta(DjangoObjectTypeMeta, NodeMeta):

    @staticmethod
    def _get_interface_options(meta):
        return Options(
            meta,
            name=None,
            description=None,
            graphql_type=None,
            registry=False
        )

    @staticmethod
    def _create_interface(cls, name, bases, attrs):
        cls = super(DjangoNodeMeta, cls)._create_interface(cls, name, bases, attrs)
        if not cls._meta.registry:
            cls._meta.registry = get_global_registry()
        assert isinstance(cls._meta.registry, Registry), 'The attribute registry in {}.Meta needs to be an instance of Registry.'.format(name)
        return cls

class DjangoNode(six.with_metaclass(DjangoNodeMeta, Node)):
    @classmethod
    def get_node(cls, id, context, info):
        try:
            return cls._meta.model.objects.get(id=id)
        except cls._meta.model.DoesNotExist:
            return None

    @classmethod
    def resolve_type(cls, type, context, info):
        # We get the model from the _meta in the Django class/instance
        model = type._meta.model
        graphene_type = cls._meta.registry.get_type_for_model(model)
        if graphene_type:
            return get_graphql_type(graphene_type)

        raise Exception("Type not found for model \"{}\"".format(model))
<commit_msg>Improved django defaults<commit_after>from functools import partial

import six

from graphene import Field, Interface
from graphene.types.objecttype import ObjectType, ObjectTypeMeta, attrs_without_fields, GrapheneObjectType, get_interfaces
from graphene.relay import Node
from graphene.relay.node import NodeMeta
from .converter import convert_django_field_with_choices
from graphene.types.options import Options
from .utils import get_model_fields, is_valid_django_model, DJANGO_FILTER_INSTALLED
from .registry import Registry, get_global_registry
from graphene.utils.is_base_type import is_base_type
from graphene.utils.copy_fields import copy_fields
from graphene.utils.get_graphql_type import get_graphql_type
from graphene.utils.get_fields import get_fields
from graphene.utils.as_field import as_field

class DjangoObjectTypeMeta(ObjectTypeMeta):
    def _construct_fields(cls, fields, options):
        _model_fields = get_model_fields(options.model)

        for field in _model_fields:
            name = field.name
            is_not_in_only = options.fields and name not in options.fields
            is_already_created = name in fields
            is_excluded = field.name in options.exclude or is_already_created
            if is_not_in_only or is_excluded:
                # We skip this field if we specify only_fields and is not
                # in there. Or when we exclude this field in exclude_fields
                continue
            converted = convert_django_field_with_choices(field, options.registry)
            if not converted:
                continue
            fields[name] = as_field(converted)

        fields = copy_fields(Field, fields, parent=cls)

        return fields

    @staticmethod
    def _create_objecttype(cls, name, bases, attrs):
        # super_new = super(DjangoObjectTypeMeta, cls).__new__
        super_new = type.__new__

        # Also ensure initialization is only performed for subclasses of Model
        # (excluding Model class itself).
        if not is_base_type(bases, DjangoObjectTypeMeta):
            return super_new(cls, name, bases, attrs)

        defaults = dict(
            name=None,
            description=None,
            model=None,
            fields=(),
            exclude=(),
            interfaces=(),
            registry=None
        )
        if DJANGO_FILTER_INSTALLED:
            # In case Django filter is available, then
            # we allow more attributes in Meta
            defaults.update(
                filter_fields=(),
                filter_order_by=(),
            )

        options = Options(
            attrs.pop('Meta', None),
            **defaults
        )
        if not options.registry:
            options.registry = get_global_registry()
        assert isinstance(options.registry, Registry), 'The attribute registry in {}.Meta needs to be an instance of Registry, received "{}".'.format(name, options.registry)
        assert is_valid_django_model(options.model), 'You need to pass a valid Django Model in {}.Meta, received "{}".'.format(name, options.model)

        interfaces = tuple(options.interfaces)
        fields = get_fields(ObjectType, attrs, bases, interfaces)
        attrs = attrs_without_fields(attrs, fields)
        cls = super_new(cls, name, bases, dict(attrs, _meta=options))

        base_interfaces = tuple(b for b in bases if issubclass(b, Interface))
        options.graphql_type = GrapheneObjectType(
            graphene_type=cls,
            name=options.name or cls.__name__,
            description=options.description or cls.__doc__,
            fields=partial(cls._construct_fields, fields, options),
            interfaces=tuple(get_interfaces(interfaces + base_interfaces))
        )
        options.get_fields = partial(cls._construct_fields, fields, options)

        if issubclass(cls, DjangoObjectType):
            options.registry.register(cls)

        return cls

class DjangoObjectType(six.with_metaclass(DjangoObjectTypeMeta, ObjectType)):
    pass

class DjangoNodeMeta(DjangoObjectTypeMeta, NodeMeta):

    @staticmethod
    def _get_interface_options(meta):
        return Options(
            meta,
            name=None,
            description=None,
            graphql_type=None,
            registry=False
        )

    @staticmethod
    def _create_interface(cls, name, bases, attrs):
        cls = super(DjangoNodeMeta, cls)._create_interface(cls, name, bases, attrs)
        if not cls._meta.registry:
            cls._meta.registry = get_global_registry()
        assert isinstance(cls._meta.registry, Registry), 'The attribute registry in {}.Meta needs to be an instance of Registry.'.format(name)
        return cls

class DjangoNode(six.with_metaclass(DjangoNodeMeta, Node)):
    @classmethod
    def get_node(cls, id, context, info):
        try:
            return cls._meta.model.objects.get(id=id)
        except cls._meta.model.DoesNotExist:
            return None

    @classmethod
    def resolve_type(cls, type, context, info):
        # We get the model from the _meta in the Django class/instance
        model = type._meta.model
        graphene_type = cls._meta.registry.get_type_for_model(model)
        if graphene_type:
            return get_graphql_type(graphene_type)

        raise Exception("Type not found for model \"{}\"".format(model))
<|endoftext|>


<commit_before># coding=utf-8
from Models.Oferta.RespostaDetalhar import RespostaDetalhar
from Framework.Controller import Controller
from Database.Controllers.Oferta import Oferta as BDOferta
from Database.Controllers.Ass_oferta_turma import Ass_oferta_turma as BDAssof
from Database.Controllers.Turma import Turma as BDTurma
from Database.Controllers.Ass_turma_sala_horario import Ass_turma_sala_horario as BDAssoc
from Database.Controllers.Sala import Sala as BDSala
from Database.Controllers.Horario import Horario as BDHorario
from Models.Horario.Horario import Horario as ModelHorario
from Models.Turma.Turma import Turma as ModelTurma

class Oferta(Controller):

	def Detalhar(self,pedido_detalhar):
		oferta = BDOferta().pegarOferta('where id_disciplina = %s',(str(pedido_detalhar.getId_disciplina())))
		turmas = self.buscaturmas(oferta)
		return RespostaDetalhar(oferta,turmas)

	def buscaturmas(self,oferta):
		associacoes = BDAssof.pegarAss_oferta_turmas('where id_oferta = %s',(str(oferta.getId())))
		self.turmas = []
		for associacao in associacoes:
			turma = BDTurma().pegarTurma('where id = %s',(str(associacao.getId_turma()),))
			horarios = self.buscahorarios(turma)
			self.turmas.append(ModelTurma(turma,horarios))
		return self.turmas

	def buscahorarios(self,turma):
		associacoes = BDassoc.pegarAss_turma_sala_horarios('where id_turma = %s',(str(turma.getId(),)))
		self.horarios = []
		for associacao in associacoes:
			sala = BDsala().pegarSala('where id = %s',(str(associacao.getId_sala()))).pegarCodigo()
			horario = BDHorario().pegarHorario('where id = %s',(str(associacao.getId_horario())))
			self.horarios.append(ModelHorario(horario,sala))
		return self.horarios
						<commit_msg>detalhar oferta<commit_after># coding=utf-8
from Models.Oferta.RespostaDetalhar import RespostaDetalhar
from Framework.Controller import Controller
from Database.Controllers.Oferta import Oferta as BDOferta
from Database.Controllers.Ass_oferta_turma import Ass_oferta_turma as BDAssof
from Database.Controllers.Turma import Turma as BDTurma
from Database.Controllers.Ass_turma_sala_horario import Ass_turma_sala_horario as BDAssoc
from Database.Controllers.Sala import Sala as BDSala
from Database.Controllers.Horario import Horario as BDHorario
from Models.Horario.Horario import Horario as ModelHorario
from Models.Turma.Turma import Turma as ModelTurma

class Oferta(Controller):

	def Detalhar(self,pedido_detalhar):
		oferta = BDOferta().pegarOferta('where id_disciplina = %s',(str(pedido_detalhar.getId_disciplina())))
		turmas = self.buscaturmas(oferta)
		return RespostaDetalhar(oferta,turmas)

	def buscaturmas(self,oferta):
		associacoes = BDAssof().pegarAss_oferta_turmas('where id_oferta = %s',(str(oferta.getId())))
		self.turmas = []
		for associacao in associacoes:
			turma = BDTurma().pegarTurma('where id = %s',(str(associacao.getId_turma()),))
			horarios = self.buscahorarios(turma)
			self.turmas.append(ModelTurma(turma,horarios))
		return self.turmas

	def buscahorarios(self,turma):
		associacoes = BDassoc().pegarAss_turma_sala_horarios('where id_turma = %s',(str(turma.getId(),)))
		self.horarios = []
		for associacao in associacoes:
			sala = BDsala().pegarSala('where id = %s',(str(associacao.getId_sala()))).pegarCodigo()
			horario = BDHorario().pegarHorario('where id = %s',(str(associacao.getId_horario())))
			self.horarios.append(ModelHorario(horario,sala))
		return self.horarios
						<|endoftext|>


<commit_before>import discord
from discord.ext import commands
from cogs.utils import checks
from cogs.utils.dataIO import dataIO
from cogs.utils.chat_formatting import box, pagify
from __main__ import settings, send_cmd_help
from copy import deepcopy
import asyncio
import logging
import os

log = logging.getLogger("red.admin")

class Admin:
    """Admin tools, more to come."""

    def __init__(self, bot):
        self.bot = bot
        self._announce_msg = None
        self._settings = dataIO.load_json('data/admin/settings.json')
        self._settable_roles = self._settings.get("ROLES", {})

    async def _confirm_invite(self, server, owner, ctx):
        answers = ("yes", "y")
        invite = await self.bot.create_invite(server)
        if ctx.message.channel.is_private:
            await self.bot.say(invite)
        else:
            await self.bot.say("Are you sure you want to post an invite to {} "
                               "here? (yes/no)".format(server.name))
            msg = await self.bot.wait_for_message(author=owner, timeout=15)
            if msg is None:
                await self.bot.say("I guess not.")
            elif msg.content.lower().strip() in answers:
                await self.bot.say(invite)
            else:
                await self.bot.say("Alright then.")

    def _get_selfrole_names(self, server):
        if server.id not in self._settable_roles:
            return None
        else:
            return self._settable_roles[server.id]

    def _is_server_locked(self):
        return self._settings.get("SERVER_LOCK", False)

    def _role_from_string(self, server, rolename, roles=None):
        if roles is None:
            roles = server.roles
        role = discord.utils.find(lambda r: r.name.lower() == rolename.lower(),
                                  roles)
        try:
            log.debug("Role {} found from rolename {}".format(
                role.name, rolename))
        except:
            log.debug("Role not found for rolename {}".format(rolename))
        return role

    def _save_settings(self):
        dataIO.save_json('data/admin/settings.json', self._settings)

    def _set_selfroles(self, server, rolelist):
        self._settable_roles[server.id] = rolelist
        self._settings["ROLES"] = self._settable_roles
        self._save_settings()

    def _set_serverlock(self, lock=True):
        self._settings["SERVER_LOCK"] = lock
        self._save_settings()

    @commands.command(no_pm=True, pass_context=True)
    @checks.admin_or_permissions(manage_roles=True)
    async def addrole(self, ctx, rolename, user: discord.Member=None):
        """Adds a role to a user, defaults to author

        Role name must be in quotes if there are spaces."""
        author = ctx.message.author
        channel = ctx.message.channel
        server = ctx.message.server

        if user is None:
            user = author

        role = self._role_from_string(server, rolename)

        if role is None:
            await self.bot.say('That role cannot be found.')
            return

        if not channel.permissions_for(server.me).manage_roles:
            await self.bot.say('I don\'t have manage_roles.')
            return

        await self.bot.add_roles(user, role)
        await self.bot.say('Added role {} to {}'.format(role.name, user.name))

    @commands.group(pass_context=True, no_pm=True)
    async def adminset(self, ctx):
        """Manage Admin settings"""
        if ctx.invoked_subcommand is None:
            await send_cmd_help(ctx)

    @adminset.command(pass_context=True, name="selfroles")
    @checks.admin_or_permissions(manage_roles=True)
    async def adminset_selfroles(self, ctx, *, rolelist=None):
        """Set which roles users can set themselves.

        COMMA SEPARATED LIST (e.g. Admin,Staff,Mod)"""
        server = ctx.message.server
        if rolelist is None:
            await self.bot.say("selfrole list cleared.")
            self._set_selfroles(server, [])
            return
        unparsed_roles = list(map(lambda r: r.strip(), rolelist.split(',')))
        parsed_roles = list(map(lambda r: self._role_from_string(server, r),
                                unparsed_roles))
        if len(unparsed_roles) != len(parsed_roles):
            not_found = set(unparsed_roles) - {r.name for r in parsed_roles}
            await self.bot.say(
                "These roles were not found: {}\n\nPlease"
                " try again.".format(not_found))
        parsed_role_set = list({r.name for r in parsed_roles})
        self._set_selfroles(server, parsed_role_set)
        await self.bot.say(
            "Self roles successfully set to: {}".format(parsed_role_set))

    @commands.command(pass_context=True)
    @checks.is_owner()
    async def announce(self, ctx, *, msg):
        """Announces a message to all servers that a bot is in."""
        if self._announce_msg is not None:
            await self.bot.say("Already announcing, wait until complete to"
                               " issue a new announcement.")
        else:
            self._announce_msg = msg

    @commands.command(pass_context=True)
    @checks.is_owner()
    async def serverlock(self, ctx):
        """Toggles locking the current server list, will not join others"""
        if self._is_server_locked():
            self._set_serverlock(False)
            await self.bot.say("Server list unlocked")
        else:
            self._set_serverlock()
            await self.bot.say("Server list locked.")

    @commands.command(pass_context=True)
    @checks.is_owner()
    async def partycrash(self, ctx, idnum=None):
        """Lists servers and generates invites for them"""
        owner = ctx.message.author
        if idnum:
            server = discord.utils.get(self.bot.servers, id=idnum)
            if server:
                await self._confirm_invite(server, owner, ctx)
            else:
                await self.bot.say("I'm not in that server")
        else:
            msg = ""
            servers = sorted(self.bot.servers, key=lambda s: s.name)
            for i, server in enumerate(servers, 1):
                msg += "{}: {}\n".format(i, server.name)
            msg += "\nTo post an invite for a server just type its number."
            for page in pagify(msg, delims=["\n"]):
                await self.bot.say(box(page))
                await asyncio.sleep(1.5)  # Just in case for rate limits
            msg = await self.bot.wait_for_message(author=owner, timeout=15)
            if msg is not None:
                try:
                    msg = int(msg.content.strip())
                    server = servers[msg - 1]
                except ValueError:
                    await self.bot.say("You must enter a number.")
                except IndexError:
                    await self.bot.say("Index out of range.")
                else:
                    try:
                        await self._confirm_invite(server, owner, ctx)
                    except discord.Forbidden:
                        await self.bot.say("I'm not allowed to make an invite"
                                           " for {}".format(server.name))
            else:
                await self.bot.say("Response timed out.")

    @commands.command(no_pm=True, pass_context=True)
    @checks.admin_or_permissions(manage_roles=True)
    async def removerole(self, ctx, rolename, user: discord.Member=None):
        """Removes a role from user, defaults to author

        Role name must be in quotes if there are spaces."""
        server = ctx.message.server
        author = ctx.message.author

        role = self._role_from_string(server, rolename)
        if role is None:
            await self.bot.say("Role not found.")
            return

        if user is None:
            user = author

        if role in user.roles:
            try:
                await self.bot.remove_roles(user, role)
                await self.bot.say("Role successfully removed.")
            except discord.Forbidden:
                await self.bot.say("I don't have permissions to manage roles!")
        else:
            await self.bot.say("User does not have that role.")

    @commands.command(pass_context=True, no_pm=True)
    async def say(self, ctx, *, text):
        """Bot repeats what you tell it to, utility for scheduler."""
        channel = ctx.message.channel
        await self.bot.send_message(channel, text)

    @commands.group(no_pm=True, pass_context=True, invoke_without_command=True)
    async def selfrole(self, ctx, *, rolename):
        """Allows users to set their own role.

        Configurable using `adminset`"""
        server = ctx.message.server
        author = ctx.message.author
        role_names = self._get_selfrole_names(server)
        if role_names is None:
            await self.bot.say("I have no user settable roles for this"
                               " server.")
            return

        roles = list(map(lambda r: self._role_from_string(server, r),
                         role_names))

        role_to_add = self._role_from_string(server, rolename, roles=roles)

        try:
            await self.bot.add_roles(author, role_to_add)
        except discord.errors.Forbidden:
            log.debug("{} just tried to add a role but I was forbidden".format(
                author.name))
            await self.bot.say("I don't have permissions to do that.")
        except AttributeError:  # role_to_add is NoneType
            log.debug("{} not found as settable on {}".format(rolename,
                                                              server.id))
            await self.bot.say("That role isn't user settable.")
        else:
            log.debug("Role {} added to {} on {}".format(rolename, author.name,
                                                         server.id))
            await self.bot.say("Role added.")

    @selfrole.command(no_pm=True, pass_context=True, name="remove")
    async def selfrole_remove(self, ctx, *, rolename):
        """Allows users to remove their own roles

        Configurable using `adminset`"""
        server = ctx.message.server
        author = ctx.message.author
        role_names = self._get_selfrole_names(server)
        if role_names is None:
            await self.bot.say("I have no user settable roles for this"
                               " server.")
            return

        roles = list(map(lambda r: self._role_from_string(server, r),
                         role_names))
        role_to_remove = self._role_from_string(server, rolename, roles=roles)

        try:
            await self.bot.remove_roles(author, role_to_remove)
        except discord.errors.Forbidden:
            log.debug("{} just tried to remove a role but I was"
                      " forbidden".format(author.name))
            await self.bot.say("I don't have permissions to do that.")
        except AttributeError:  # role_to_remove is NoneType
            log.debug("{} not found as removeable on {}".format(rolename,
                                                                server.id))
            await self.bot.say("That role isn't user removeable.")
        else:
            log.debug("Role {} removed from {} on {}".format(rolename,
                                                             author.name,
                                                             server.id))
            await self.bot.say("Role removed.")

    @commands.command(pass_context=True)
    @checks.is_owner()
    async def sudo(self, ctx, user: discord.Member, *, command):
        """Runs the [command] as if [user] had run it. DON'T ADD A PREFIX
        """
        new_msg = deepcopy(ctx.message)
        new_msg.author = user
        new_msg.content = self.bot.command_prefix[0] + command
        await self.bot.process_commands(new_msg)

    @commands.command(pass_context=True, hidden=True)
    @checks.is_owner()  # I don't know how permissive this should be yet
    async def whisper(self, ctx, id, *, text):
        author = ctx.message.author

        target = discord.utils.get(self.bot.get_all_members(), id=id)
        if target is None:
            target = self.bot.get_channel(id)
            if target is None:
                target = self.bot.get_server(id)

        prefix = "Hello, you're getting a message from {} ({})".format(
            author.name, author.id)
        payload = "{}\n\n{}".format(prefix, text)

        try:
            for page in pagify(payload, delims=[" ", "\n"], shorten_by=10):
                await self.bot.send_message(target, box(page))
        except discord.errors.Forbidden:
            log.debug("Forbidden to send message to {}".format(id))
        except (discord.errors.NotFound, discord.errors.InvalidArgument):
            log.debug("{} not found!".format(id))
        else:
            await self.bot.say("Done.")

    async def announcer(self, msg):
        server_ids = map(lambda s: s.id, self.bot.servers)
        for server_id in server_ids:
            if self != self.bot.get_cog('Admin'):
                break
            server = self.bot.get_server(server_id)
            if server is None:
                continue
            chan = server.default_channel
            log.debug("Looking to announce to {} on {}".format(chan.name,
                                                               server.name))
            me = server.me
            if chan.permissions_for(me).send_messages:
                log.debug("I can send messages to {} on {}, sending".format(
                    server.name, chan.name))
                await self.bot.send_message(chan, msg)
            await asyncio.sleep(1)

    async def announce_manager(self):
        while self == self.bot.get_cog('Admin'):
            if self._announce_msg is not None:
                log.debug("Found new announce message, announcing")
                await self.announcer(self._announce_msg)
                self._announce_msg = None
            await asyncio.sleep(1)

    async def server_locker(self, server):
        if self._is_server_locked():
            await self.bot.leave_server(server)

def check_files():
    if not os.path.exists('data/admin/settings.json'):
        try:
            os.mkdir('data/admin')
        except FileExistsError:
            pass
        else:
            dataIO.save_json('data/admin/settings.json', {})

def setup(bot):
    check_files()
    n = Admin(bot)
    bot.add_cog(n)
    bot.add_listener(n.server_locker, "on_server_join")
    bot.loop.create_task(n.announce_manager())
<commit_msg>[Admin] Update send_cmd_help reference<commit_after>import discord
from discord.ext import commands
from cogs.utils import checks
from cogs.utils.dataIO import dataIO
from cogs.utils.chat_formatting import box, pagify
from copy import deepcopy
import asyncio
import logging
import os

log = logging.getLogger("red.admin")

class Admin:
    """Admin tools, more to come."""

    def __init__(self, bot):
        self.bot = bot
        self._announce_msg = None
        self._settings = dataIO.load_json('data/admin/settings.json')
        self._settable_roles = self._settings.get("ROLES", {})

    async def _confirm_invite(self, server, owner, ctx):
        answers = ("yes", "y")
        invite = await self.bot.create_invite(server)
        if ctx.message.channel.is_private:
            await self.bot.say(invite)
        else:
            await self.bot.say("Are you sure you want to post an invite to {} "
                               "here? (yes/no)".format(server.name))
            msg = await self.bot.wait_for_message(author=owner, timeout=15)
            if msg is None:
                await self.bot.say("I guess not.")
            elif msg.content.lower().strip() in answers:
                await self.bot.say(invite)
            else:
                await self.bot.say("Alright then.")

    def _get_selfrole_names(self, server):
        if server.id not in self._settable_roles:
            return None
        else:
            return self._settable_roles[server.id]

    def _is_server_locked(self):
        return self._settings.get("SERVER_LOCK", False)

    def _role_from_string(self, server, rolename, roles=None):
        if roles is None:
            roles = server.roles
        role = discord.utils.find(lambda r: r.name.lower() == rolename.lower(),
                                  roles)
        try:
            log.debug("Role {} found from rolename {}".format(
                role.name, rolename))
        except:
            log.debug("Role not found for rolename {}".format(rolename))
        return role

    def _save_settings(self):
        dataIO.save_json('data/admin/settings.json', self._settings)

    def _set_selfroles(self, server, rolelist):
        self._settable_roles[server.id] = rolelist
        self._settings["ROLES"] = self._settable_roles
        self._save_settings()

    def _set_serverlock(self, lock=True):
        self._settings["SERVER_LOCK"] = lock
        self._save_settings()

    @commands.command(no_pm=True, pass_context=True)
    @checks.admin_or_permissions(manage_roles=True)
    async def addrole(self, ctx, rolename, user: discord.Member=None):
        """Adds a role to a user, defaults to author

        Role name must be in quotes if there are spaces."""
        author = ctx.message.author
        channel = ctx.message.channel
        server = ctx.message.server

        if user is None:
            user = author

        role = self._role_from_string(server, rolename)

        if role is None:
            await self.bot.say('That role cannot be found.')
            return

        if not channel.permissions_for(server.me).manage_roles:
            await self.bot.say('I don\'t have manage_roles.')
            return

        await self.bot.add_roles(user, role)
        await self.bot.say('Added role {} to {}'.format(role.name, user.name))

    @commands.group(pass_context=True, no_pm=True)
    async def adminset(self, ctx):
        """Manage Admin settings"""
        if ctx.invoked_subcommand is None:
            await self.bot.send_cmd_help(ctx)

    @adminset.command(pass_context=True, name="selfroles")
    @checks.admin_or_permissions(manage_roles=True)
    async def adminset_selfroles(self, ctx, *, rolelist=None):
        """Set which roles users can set themselves.

        COMMA SEPARATED LIST (e.g. Admin,Staff,Mod)"""
        server = ctx.message.server
        if rolelist is None:
            await self.bot.say("selfrole list cleared.")
            self._set_selfroles(server, [])
            return
        unparsed_roles = list(map(lambda r: r.strip(), rolelist.split(',')))
        parsed_roles = list(map(lambda r: self._role_from_string(server, r),
                                unparsed_roles))
        if len(unparsed_roles) != len(parsed_roles):
            not_found = set(unparsed_roles) - {r.name for r in parsed_roles}
            await self.bot.say(
                "These roles were not found: {}\n\nPlease"
                " try again.".format(not_found))
        parsed_role_set = list({r.name for r in parsed_roles})
        self._set_selfroles(server, parsed_role_set)
        await self.bot.say(
            "Self roles successfully set to: {}".format(parsed_role_set))

    @commands.command(pass_context=True)
    @checks.is_owner()
    async def announce(self, ctx, *, msg):
        """Announces a message to all servers that a bot is in."""
        if self._announce_msg is not None:
            await self.bot.say("Already announcing, wait until complete to"
                               " issue a new announcement.")
        else:
            self._announce_msg = msg

    @commands.command(pass_context=True)
    @checks.is_owner()
    async def serverlock(self, ctx):
        """Toggles locking the current server list, will not join others"""
        if self._is_server_locked():
            self._set_serverlock(False)
            await self.bot.say("Server list unlocked")
        else:
            self._set_serverlock()
            await self.bot.say("Server list locked.")

    @commands.command(pass_context=True)
    @checks.is_owner()
    async def partycrash(self, ctx, idnum=None):
        """Lists servers and generates invites for them"""
        owner = ctx.message.author
        if idnum:
            server = discord.utils.get(self.bot.servers, id=idnum)
            if server:
                await self._confirm_invite(server, owner, ctx)
            else:
                await self.bot.say("I'm not in that server")
        else:
            msg = ""
            servers = sorted(self.bot.servers, key=lambda s: s.name)
            for i, server in enumerate(servers, 1):
                msg += "{}: {}\n".format(i, server.name)
            msg += "\nTo post an invite for a server just type its number."
            for page in pagify(msg, delims=["\n"]):
                await self.bot.say(box(page))
                await asyncio.sleep(1.5)  # Just in case for rate limits
            msg = await self.bot.wait_for_message(author=owner, timeout=15)
            if msg is not None:
                try:
                    msg = int(msg.content.strip())
                    server = servers[msg - 1]
                except ValueError:
                    await self.bot.say("You must enter a number.")
                except IndexError:
                    await self.bot.say("Index out of range.")
                else:
                    try:
                        await self._confirm_invite(server, owner, ctx)
                    except discord.Forbidden:
                        await self.bot.say("I'm not allowed to make an invite"
                                           " for {}".format(server.name))
            else:
                await self.bot.say("Response timed out.")

    @commands.command(no_pm=True, pass_context=True)
    @checks.admin_or_permissions(manage_roles=True)
    async def removerole(self, ctx, rolename, user: discord.Member=None):
        """Removes a role from user, defaults to author

        Role name must be in quotes if there are spaces."""
        server = ctx.message.server
        author = ctx.message.author

        role = self._role_from_string(server, rolename)
        if role is None:
            await self.bot.say("Role not found.")
            return

        if user is None:
            user = author

        if role in user.roles:
            try:
                await self.bot.remove_roles(user, role)
                await self.bot.say("Role successfully removed.")
            except discord.Forbidden:
                await self.bot.say("I don't have permissions to manage roles!")
        else:
            await self.bot.say("User does not have that role.")

    @commands.command(pass_context=True, no_pm=True)
    async def say(self, ctx, *, text):
        """Bot repeats what you tell it to, utility for scheduler."""
        channel = ctx.message.channel
        await self.bot.send_message(channel, text)

    @commands.group(no_pm=True, pass_context=True, invoke_without_command=True)
    async def selfrole(self, ctx, *, rolename):
        """Allows users to set their own role.

        Configurable using `adminset`"""
        server = ctx.message.server
        author = ctx.message.author
        role_names = self._get_selfrole_names(server)
        if role_names is None:
            await self.bot.say("I have no user settable roles for this"
                               " server.")
            return

        roles = list(map(lambda r: self._role_from_string(server, r),
                         role_names))

        role_to_add = self._role_from_string(server, rolename, roles=roles)

        try:
            await self.bot.add_roles(author, role_to_add)
        except discord.errors.Forbidden:
            log.debug("{} just tried to add a role but I was forbidden".format(
                author.name))
            await self.bot.say("I don't have permissions to do that.")
        except AttributeError:  # role_to_add is NoneType
            log.debug("{} not found as settable on {}".format(rolename,
                                                              server.id))
            await self.bot.say("That role isn't user settable.")
        else:
            log.debug("Role {} added to {} on {}".format(rolename, author.name,
                                                         server.id))
            await self.bot.say("Role added.")

    @selfrole.command(no_pm=True, pass_context=True, name="remove")
    async def selfrole_remove(self, ctx, *, rolename):
        """Allows users to remove their own roles

        Configurable using `adminset`"""
        server = ctx.message.server
        author = ctx.message.author
        role_names = self._get_selfrole_names(server)
        if role_names is None:
            await self.bot.say("I have no user settable roles for this"
                               " server.")
            return

        roles = list(map(lambda r: self._role_from_string(server, r),
                         role_names))
        role_to_remove = self._role_from_string(server, rolename, roles=roles)

        try:
            await self.bot.remove_roles(author, role_to_remove)
        except discord.errors.Forbidden:
            log.debug("{} just tried to remove a role but I was"
                      " forbidden".format(author.name))
            await self.bot.say("I don't have permissions to do that.")
        except AttributeError:  # role_to_remove is NoneType
            log.debug("{} not found as removeable on {}".format(rolename,
                                                                server.id))
            await self.bot.say("That role isn't user removeable.")
        else:
            log.debug("Role {} removed from {} on {}".format(rolename,
                                                             author.name,
                                                             server.id))
            await self.bot.say("Role removed.")

    @commands.command(pass_context=True)
    @checks.is_owner()
    async def sudo(self, ctx, user: discord.Member, *, command):
        """Runs the [command] as if [user] had run it. DON'T ADD A PREFIX
        """
        new_msg = deepcopy(ctx.message)
        new_msg.author = user
        new_msg.content = self.bot.command_prefix[0] + command
        await self.bot.process_commands(new_msg)

    @commands.command(pass_context=True, hidden=True)
    @checks.is_owner()  # I don't know how permissive this should be yet
    async def whisper(self, ctx, id, *, text):
        author = ctx.message.author

        target = discord.utils.get(self.bot.get_all_members(), id=id)
        if target is None:
            target = self.bot.get_channel(id)
            if target is None:
                target = self.bot.get_server(id)

        prefix = "Hello, you're getting a message from {} ({})".format(
            author.name, author.id)
        payload = "{}\n\n{}".format(prefix, text)

        try:
            for page in pagify(payload, delims=[" ", "\n"], shorten_by=10):
                await self.bot.send_message(target, box(page))
        except discord.errors.Forbidden:
            log.debug("Forbidden to send message to {}".format(id))
        except (discord.errors.NotFound, discord.errors.InvalidArgument):
            log.debug("{} not found!".format(id))
        else:
            await self.bot.say("Done.")

    async def announcer(self, msg):
        server_ids = map(lambda s: s.id, self.bot.servers)
        for server_id in server_ids:
            if self != self.bot.get_cog('Admin'):
                break
            server = self.bot.get_server(server_id)
            if server is None:
                continue
            chan = server.default_channel
            log.debug("Looking to announce to {} on {}".format(chan.name,
                                                               server.name))
            me = server.me
            if chan.permissions_for(me).send_messages:
                log.debug("I can send messages to {} on {}, sending".format(
                    server.name, chan.name))
                await self.bot.send_message(chan, msg)
            await asyncio.sleep(1)

    async def announce_manager(self):
        while self == self.bot.get_cog('Admin'):
            if self._announce_msg is not None:
                log.debug("Found new announce message, announcing")
                await self.announcer(self._announce_msg)
                self._announce_msg = None
            await asyncio.sleep(1)

    async def server_locker(self, server):
        if self._is_server_locked():
            await self.bot.leave_server(server)

def check_files():
    if not os.path.exists('data/admin/settings.json'):
        try:
            os.mkdir('data/admin')
        except FileExistsError:
            pass
        else:
            dataIO.save_json('data/admin/settings.json', {})

def setup(bot):
    check_files()
    n = Admin(bot)
    bot.add_cog(n)
    bot.add_listener(n.server_locker, "on_server_join")
    bot.loop.create_task(n.announce_manager())
<|endoftext|>


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

import os
from webob import Request
from webob.exc import HTTPMovedPermanently, HTTPNotFound
from paste import httpserver
from paste.fileapp import FileApp

from ass2m import Ass2m, NotWorkingDir

class Ass2mFileApp(FileApp):
    def guess_type(self):
        # add UTF-8 by default to text content-types
        guess = FileApp.guess_type(self)
        content_type = guess[0]
        if content_type and "text/" in content_type and "charset=" not in content_type:
            content_type += "; charset=UTF-8"
        return (content_type, guess[1])

class Client(object):
    def __init__(self, ass2m, environ, start_response):
        self.ass2m = ass2m
        self.environ = environ
        self.req = Request(environ)
        self.start_response = start_response
        self.user = None

    def answer(self):
        parsed_path = self.req.path_info[1:]
        fpath = os.path.join(self.ass2m.root, parsed_path)
        if os.path.isfile(fpath):
            fapp = Ass2mFileApp(fpath)
            # serve the file, delegate everything to to FileApp
            return fapp(self.environ, self.start_response)

        if os.path.isdir(fpath):
            if parsed_path[-1:] != "/" and len(parsed_path):
                resp = HTTPMovedPermanently(location=self.req.path_info+"/")
                return resp(self.environ, self.start_response)
            else:
                return self.listdir(parsed_path)

        resp = HTTPNotFound()
        return resp(self.environ, self.start_response)

    def listdir(self, relpath):
        directory = os.path.join(self.ass2m.root, relpath)
        self.start_response('200 OK', [('Content-Type', 'text/html; charset=UTF-8')])
        yield """

    <title>Index of /%s</title>

Listing /%s

""" % (relpath, relpath)
        for filename in sorted(os.listdir(directory)):
            f = self.ass2m.get_file(os.path.join(directory, filename))
            if self.user and not self.user.has_perms(f):
                continue
            if os.path.isdir(os.path.join(directory, filename)):
                yield '
%s/
' % (filename, filename)
            else:
                yield '
%s
' % (filename, filename)
        yield """

<address>ass2m</address>

"""

class Server(object):
    def __init__(self, root):
        self.root = root
        try:
            self.ass2m = Ass2m(root)
        except NotWorkingDir:
            self.ass2m = None

    def bind(self, hostname, port):
        httpserver.serve(self.process, host=hostname, port=str(port))

    def process(self, environ, start_response):
        if not self.ass2m:
            try:
                self.ass2m = Ass2m(self.root)
            except NotWorkingDir:
                return self.error_notworkingdir(start_response, self.root)

        client = Client(self.ass2m, environ, start_response)
        return client.answer()

    def error_notworkingdir(self, start_response, root):
        start_response('500 ERROR', [('Content-Type', 'text/html; charset=UTF-8')])
        yield """

    <title>Error</title>

Internal error

The configured root path is not an ass2m working directory.

Please use:

$ cd %s && ass2m init

<address>ass2m</address>

""" % root

<commit_msg>Move back error_notworkingdir() to Client<commit_after># -*- coding: utf-8 -*-

import os
from webob import Request
from webob.exc import HTTPMovedPermanently, HTTPNotFound
from paste import httpserver
from paste.fileapp import FileApp

from ass2m import Ass2m, NotWorkingDir

class Ass2mFileApp(FileApp):
    def guess_type(self):
        # add UTF-8 by default to text content-types
        guess = FileApp.guess_type(self)
        content_type = guess[0]
        if content_type and "text/" in content_type and "charset=" not in content_type:
            content_type += "; charset=UTF-8"
        return (content_type, guess[1])

class Client(object):
    def __init__(self, root, environ, start_response):
        try:
            self.ass2m = Ass2m(root)
        except NotWorkingDir:
            self.ass2m = None
        self.root = root
        self.environ = environ
        self.req = Request(environ)
        self.start_response = start_response
        self.user = None

    def error_notworkingdir(self):
        self.start_response('500 ERROR', [('Content-Type', 'text/html; charset=UTF-8')])
        yield """

    <title>Error</title>

Internal error

The configured root path is not an ass2m working directory.

Please use:

$ cd %s && ass2m init

<address>ass2m</address>

""" % self.root

    def answer(self):
        if not self.ass2m:
            return self.error_notworkingdir()

        parsed_path = self.req.path_info[1:]
        fpath = os.path.join(self.ass2m.root, parsed_path)
        if os.path.isfile(fpath):
            fapp = Ass2mFileApp(fpath)
            # serve the file, delegate everything to to FileApp
            return fapp(self.environ, self.start_response)

        if os.path.isdir(fpath):
            if parsed_path[-1:] != "/" and len(parsed_path):
                resp = HTTPMovedPermanently(location=self.req.path_info+"/")
                return resp(self.environ, self.start_response)
            else:
                return self.listdir(parsed_path)

        resp = HTTPNotFound()
        return resp(self.environ, self.start_response)

    def listdir(self, relpath):
        directory = os.path.join(self.ass2m.root, relpath)
        self.start_response('200 OK', [('Content-Type', 'text/html; charset=UTF-8')])
        yield """

    <title>Index of /%s</title>

Listing /%s

""" % (relpath, relpath)
        for filename in sorted(os.listdir(directory)):
            f = self.ass2m.get_file(os.path.join(directory, filename))
            if self.user and not self.user.has_perms(f):
                continue
            if os.path.isdir(os.path.join(directory, filename)):
                yield '
%s/
' % (filename, filename)
            else:
                yield '
%s
' % (filename, filename)
        yield """

<address>ass2m</address>

"""

class Server(object):
    def __init__(self, root):
        self.root = root

    def bind(self, hostname, port):
        httpserver.serve(self.process, host=hostname, port=str(port))

    def process(self, environ, start_response):
        client = Client(self.root, environ, start_response)
        return client.answer()

<|endoftext|>


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

"""Provides sync for podio."""

__all__ = [
    'add_notification',
    'AddNotification'
]

from pyramid_torque_engine import unpack
from pyramid_torque_engine import operations as ops

from pyramid_torque_engine import repo
from pyramid import path

import colander
import notification_table_executer
import datetime
import pyramid_basemodel as bm
import requests
import json

def send_email_from_notification_dispatch(request, notification_dispatch_id):
    """Boilerplate to extract information from the notification
    dispatch and send an email.
    """

    lookup = repo.LookupNotificationDispatch()
    dotted_name_resolver = path.DottedNameResolver()

    notification_dispatch = lookup(notification_dispatch_id)
    if not notification_dispatch:
        return False

    # Get our spec.
    spec = notification_dispatch.single_spec

    # Get our Address to send to.
    send_to = notification_dispatch.address

    # Get our view to render the spec.
    view = dotted_name_resolver.resolve(notification_dispatch.view)

    # Get the context.
    context = notification_dispatch.notification.event.parent

    # Send the email.
    view(request, context, spec, send_to)

    return True

def notification_email_single_view(request):
    """View to handle a single email notification dispatch"""

    class SingleNotificationSchema(colander.Schema):
        notification_dispatch_id = colander.SchemaNode(
            colander.Integer(),
        )

    schema = SingleNotificationSchema()

    # Decode JSON.
    try:
        json = request.json
    except ValueError as err:
        request.response.status_int = 400
        return {'JSON error': str(err)}

    # Validate.
    try:
        appstruct = schema.deserialize(json)
    except colander.Invalid as err:
        request.response.status_int = 400
        return {'error': err.asdict()}

    # Get data out of JSON.
    notification_dispatch_id = appstruct['notification_dispatch_id']

    # Send the email.
    r = send_email_from_notification_dispatch(request, notification_dispatch_id)
    if not r:
        request.response.status_int = 404
        return {'error': u'Notification dispatch not Found.'}

    # Return 200.
    return {'dispatched': 'ok'}

def notification_email_batch_view(request):
    """View to handle a batch email notification dispatch"""
    pass

class AddNotification(object):
    """Standard boilerplate to add a notification."""

    def __init__(self, iface, role, dispatch_mapping, delay=None):
        """"""

        self.dispatch_mapping = dispatch_mapping
        self.notification_factory = repo.NotificationFactory
        self.role = role
        self.delay = delay
        self.iface = iface

    def __call__(self, request, context, event, op, **kwargs):
        """"""

        # Unpack.
        dispatch_mapping = self.dispatch_mapping
        notification_factory = self.notification_factory(request)
        delay = self.delay
        iface = self.iface
        role = self.role

        # get relevant information.
        interested_users_func = get_roles_mapping(request, iface)
        interested_users = interested_users_func(request, context)
        for user in interested_users[role]:
            _ = notification_factory(event, user, dispatch_mapping, delay)

        # Trigger a run over the notification dispatcher as we have new notifications.
        request.dispatch_notification()

def add_notification(config,
                     iface,
                     role,
                     state_or_action_changes,
                     dispatch_mapping,
                     delay=None):

    # Unpack.
    _, o, _, s = unpack.constants()
    _, on, _ = unpack.directives(config)

    o.register(
        'CREATE_NOTIFICATION',
    )

    create_notification_in_db = AddNotification(iface, role, dispatch_mapping, delay)
    on(iface, state_or_action_changes, o.CREATE_NOTIFICATION, create_notification_in_db)

def add_roles_mapping(config, iface, mapping):
    """Adds a roles mapping to the resource."""

    # Unpack.
    registry = config.registry

    # Noop if we've done this already.
    roles_mapping = registry.roles_mapping
    if iface in roles_mapping:
        return

    # Register the role mapping.
    roles_mapping[iface] = mapping

def get_roles_mapping(request, iface):
    """Gets the role mapping for the resource."""

    # Unpack.
    registry = request.registry
    roles_mapping = registry.roles_mapping

    return roles_mapping.get(iface, None)

def get_operator_user(request, registry=None):
    """We have a special user in our db representing the operator user. Here
      we look them up by username, constructed from the client id.
      XXXXXX at the moment Andre is the operator user!!!1
    """

    if registry == None:
        # Unpack.
        settings = request.registry.settings
    else:
        settings = registry.settings

    # Get the user.
    from pyramid_simpleauth.model import get_existing_user
    try:
        username = u'aprado'
    except KeyError:
        username = None

    return get_existing_user(username=username)

def dispatch_notification(request, data=None, path=None):
    """Dispatches a notification so that the dispatcher runs."""

    # Compose.
    if path is None:
        path = 'localhost:5100/engine/notifications/dispatch'
    if data is None:
        data = {}

    return request.torque.dispatch(path, data)

class IncludeMe(object):
    """Set up the state change event subscription system and provide an
      ``add_engine_subscriber`` directive.
    """

    def __init__(self, **kwargs):
        self.add_notification = kwargs.get('add_notification', add_notification)
        self.add_roles_mapping = kwargs.get('add_roles_mapping', add_roles_mapping)
        self.get_roles_mapping = kwargs.get('get_roles_mapping', get_roles_mapping)

    def __call__(self, config):
        """Handle `/events` requests and provide subscription directive."""

        # Adds a notification to the resource.
        config.add_directive('add_notification', self.add_notification)
        config.registry.roles_mapping = {}

        # Adds / gets role mapping.
        config.add_directive('add_roles_mapping', self.add_roles_mapping)
        config.add_directive('get_roles_mapping', self.get_roles_mapping)

        # Operator user to receive admin related emails.
        config.add_request_method(get_operator_user, 'operator_user', reify=True)

        # Expose webhook view to run the notification system on "trigger mode".
        config.add_route('notification_dispatcher', '/notifications/dispatch')
        config.add_view(notification_dispatcher_view, renderer='json',
                request_method='POST', route_name='notification_dispatcher')

        # Dispatcher to simply calling the notification dispatcher view.
        config.add_request_method(dispatch_notification, 'dispatch_notification', reify=True)

        # Expose webhook views to notifications such as single / batch emails / sms's.
        config.add_route('notification_email_single', '/notifications/email_single')
        config.add_view(notification_email_single_view, renderer='json',
                request_method='POST', route_name='notification_email_single')
        config.add_route('notification_email_batch', '/notifications/email_batch')
        config.add_view(notification_email_batch_view, renderer='json',
                request_method='POST', route_name='notification_email_batch')

includeme = IncludeMe().__call__
<commit_msg>notification: implement notifications_dispatcher as a simple optimistic send email call.<commit_after># -*- coding: utf-8 -*-

"""Provides sync for podio."""

__all__ = [
    'add_notification',
    'AddNotification'
]

from pyramid_torque_engine import unpack
from pyramid_torque_engine import operations as ops

from pyramid_torque_engine import repo
from pyramid import path

import colander
import notification_table_executer
import datetime
import pyramid_basemodel as bm
import requests
import json

def send_email_from_notification_dispatch(request, notification_dispatch_id):
    """Boilerplate to extract information from the notification
    dispatch and send an email.
    """

    lookup = repo.LookupNotificationDispatch()
    dotted_name_resolver = path.DottedNameResolver()

    notification_dispatch = lookup(notification_dispatch_id)
    if not notification_dispatch:
        return False

    # Get our spec.
    spec = notification_dispatch.single_spec

    # Get our Address to send to.
    send_to = notification_dispatch.address

    # Get our view to render the spec.
    view = dotted_name_resolver.resolve(notification_dispatch.view)

    # Get the context.
    context = notification_dispatch.notification.event.parent

    # Send the email.
    view(request, context, spec, send_to)

    return True

def notification_email_single_view(request):
    """View to handle a single email notification dispatch"""

    class SingleNotificationSchema(colander.Schema):
        notification_dispatch_id = colander.SchemaNode(
            colander.Integer(),
        )

    schema = SingleNotificationSchema()

    # Decode JSON.
    try:
        json = request.json
    except ValueError as err:
        request.response.status_int = 400
        return {'JSON error': str(err)}

    # Validate.
    try:
        appstruct = schema.deserialize(json)
    except colander.Invalid as err:
        request.response.status_int = 400
        return {'error': err.asdict()}

    # Get data out of JSON.
    notification_dispatch_id = appstruct['notification_dispatch_id']

    # Send the email.
    r = send_email_from_notification_dispatch(request, notification_dispatch_id)
    if not r:
        request.response.status_int = 404
        return {'error': u'Notification dispatch not Found.'}

    # Return 200.
    return {'dispatched': 'ok'}

def notification_email_batch_view(request):
    """View to handle a batch email notification dispatch"""
    pass

class AddNotification(object):
    """Standard boilerplate to add a notification."""

    def __init__(self, iface, role, dispatch_mapping, delay=None):
        """"""

        self.dispatch_mapping = dispatch_mapping
        self.notification_factory = repo.NotificationFactory
        self.role = role
        self.delay = delay
        self.iface = iface

    def __call__(self, request, context, event, op, **kwargs):
        """"""

        # Unpack.
        dispatch_mapping = self.dispatch_mapping
        notification_factory = self.notification_factory(request)
        delay = self.delay
        iface = self.iface
        role = self.role

        # Prepare.
        notifications = []

        # get relevant information.
        interested_users_func = get_roles_mapping(request, iface)
        interested_users = interested_users_func(request, context)
        for user in interested_users[role]:
            notification = notification_factory(event, user, dispatch_mapping, delay)
            notifications.append(notification)

        # Tries to optimistically send the notification.
        dispatch_notifications(request, notifications)

def add_notification(config,
                     iface,
                     role,
                     state_or_action_changes,
                     dispatch_mapping,
                     delay=None):

    # Unpack.
    _, o, _, s = unpack.constants()
    _, on, _ = unpack.directives(config)

    o.register(
        'CREATE_NOTIFICATION',
    )

    create_notification_in_db = AddNotification(iface, role, dispatch_mapping, delay)
    on(iface, state_or_action_changes, o.CREATE_NOTIFICATION, create_notification_in_db)

def add_roles_mapping(config, iface, mapping):
    """Adds a roles mapping to the resource."""

    # Unpack.
    registry = config.registry

    # Noop if we've done this already.
    roles_mapping = registry.roles_mapping
    if iface in roles_mapping:
        return

    # Register the role mapping.
    roles_mapping[iface] = mapping

def get_roles_mapping(request, iface):
    """Gets the role mapping for the resource."""

    # Unpack.
    registry = request.registry
    roles_mapping = registry.roles_mapping

    return roles_mapping.get(iface, None)

def get_operator_user(request, registry=None):
    """We have a special user in our db representing the operator user. Here
      we look them up by username, constructed from the client id.
      XXXXXX at the moment Andre is the operator user!!!1
    """

    if registry == None:
        # Unpack.
        settings = request.registry.settings
    else:
        settings = registry.settings

    # Get the user.
    from pyramid_simpleauth.model import get_existing_user
    try:
        username = u'aprado'
    except KeyError:
        username = None

    return get_existing_user(username=username)

def dispatch_notifications(request, notifications):
    """Dispatches a notification directly without waiting for the
    background process."""

    lookup = repo.LookupNotificationDispatch()

    for notification in notifications:
        for dispatch in lookup.by_notification_id(notification.id):
            r = send_email_from_notification_dispatch(request, dispatch.id)
            if r:
                dispatch.sent = datetime.datetime.now()
                bm.save(dispatch)

class IncludeMe(object):
    """Set up the state change event subscription system and provide an
      ``add_engine_subscriber`` directive.
    """

    def __init__(self, **kwargs):
        self.add_notification = kwargs.get('add_notification', add_notification)
        self.add_roles_mapping = kwargs.get('add_roles_mapping', add_roles_mapping)
        self.get_roles_mapping = kwargs.get('get_roles_mapping', get_roles_mapping)

    def __call__(self, config):
        """Handle `/events` requests and provide subscription directive."""

        # XXX Add apps needed to send an email.
        config.include('pyramid_mako')
        config.include('pyramid_postmark')
        config.include('fabbed.urls')

        # Dispatch the notifications.
        config.add_request_method(dispatch_notifications, 'dispatch_notifications', reify=True)

        # Adds a notification to the resource.
        config.add_directive('add_notification', self.add_notification)
        config.registry.roles_mapping = {}

        # Adds / gets role mapping.
        config.add_directive('add_roles_mapping', self.add_roles_mapping)
        config.add_directive('get_roles_mapping', self.get_roles_mapping)

        # Operator user to receive admin related emails.
        config.add_request_method(get_operator_user, 'operator_user', reify=True)

        # Expose webhook views to notifications such as single / batch emails / sms's.
        config.add_route('notification_email_single', '/notifications/email_single')
        config.add_view(notification_email_single_view, renderer='json',
                request_method='POST', route_name='notification_email_single')
        config.add_route('notification_email_batch', '/notifications/email_batch')
        config.add_view(notification_email_batch_view, renderer='json',
                request_method='POST', route_name='notification_email_batch')

includeme = IncludeMe().__call__
<|endoftext|>


<commit_before>from Framework.Controller import Controller
from Database.Controllers.Predio import Predio as BDPredio
from Models.Predio.RespostaListar import RespostaListar
from Models.Predio.RespostaCadastrar import RespostaCadastrar
from Models.Predio.RespostaEditar import RespostaEditar
from Models.Predio.RespostaVer import RespostaVer
from Models.Predio.RespostaDeletar import RespostaDeletar
from Database.Models.Predio import Predio as ModelPredio

class Predio(Controller):

	def Listar(self,pedido_listar):
		return RespostaListar(BDPredio().pegarPredios("WHERE id_campus = %s AND nome LIKE %s LIMIT %s OFFSET %s",(pedido_listar.getIdCampus(),"%"+pedido_listar.getNome().replace(' ','%')+"%",pedido_listar.getQuantidade(),(pedido_listar.getQuantidade()*pedido_listar.getPagina()))))

	def Ver(self, pedido_ver):
		return RespostaVer(BDPredio().pegarPredio("WHERE id = %s ", (str(pedido_ver.getId()),)))

	def Cadastrar(self,pedido_cadastrar):
		predio = ModelPredio()
		predio.setNome(pedido_cadastrar.getNome())
		predio.setSigla(pedido_cadastrar.getSigla())
		predio.setLatitude(pedido_cadastrar.getLatitude())
		predio.setLongitude(pedido_cadastrar.getLongitude())
		predio.setId_campus(pedido_cadastrar.getId_campus())
		return RespostaCadastrar(BDPredio().inserirPredio(predio))

	def Editar(self,pedido_editar):
		predio = BDPredio().pegarPredio("WHERE id = %s ", (str(pedido_editar.getId()),))
		predio.setNome(pedido_editar.getNome())
		predio.setSigla(pedido_cadastrar.getSigla())
		predio.setLatitude(pedido_cadastrar.getLatitude())
		predio.setLongitude(pedido_cadastrar.getLongitude())
		BDPredio().alterarPredio(predio)
		return RespostaEditar("Predio Editado com sucesso!")

	def Deletar(self,pedido_deletar):
		predio = BDPredio().pegarPredio("WHERE id = %s ", (str(pedido_deletar.getId()),))
		BDPredio().removerPredio(predio)
		return RespostaDeletar("Predio Removido com sucesso!")
<commit_msg>consertando controller predio<commit_after>from Framework.Controller import Controller
from Database.Controllers.Predio import Predio as BDPredio
from Models.Predio.RespostaListar import RespostaListar
from Models.Predio.RespostaCadastrar import RespostaCadastrar
from Models.Predio.RespostaEditar import RespostaEditar
from Models.Predio.RespostaVer import RespostaVer
from Models.Predio.RespostaDeletar import RespostaDeletar
from Database.Models.Predio import Predio as ModelPredio

class Predio(Controller):

	def Listar(self,pedido_listar):
		return RespostaListar(BDPredio().pegarPredios("WHERE id_campus = %s AND nome LIKE %s LIMIT %s OFFSET %s",(pedido_listar.getIdCampus(),"%"+pedido_listar.getNome().replace(' ','%')+"%",pedido_listar.getQuantidade(),(pedido_listar.getQuantidade()*pedido_listar.getPagina()))))

	def Ver(self, pedido_ver):
		return RespostaVer(BDPredio().pegarPredio("WHERE id = %s ", (str(pedido_ver.getId()),)))

	def Cadastrar(self,pedido_cadastrar):
		predio = ModelPredio()
		predio.setNome(pedido_cadastrar.getNome())
		predio.setSigla(pedido_cadastrar.getSigla())
		predio.setLatitude(pedido_cadastrar.getLatitude())
		predio.setLongitude(pedido_cadastrar.getLongitude())
		predio.setId_campus(pedido_cadastrar.getId_campus())
		return RespostaCadastrar(BDPredio().inserirPredio(predio))

	def Editar(self,pedido_editar):
		predio = BDPredio().pegarPredio("WHERE id = %s ", (str(pedido_editar.getId()),))
		predio.setNome(pedido_editar.getNome())
		predio.setSigla(pedido_editar.getSigla())
		predio.setLatitude(pedido_editar.getLatitude())
		predio.setLongitude(pedido_editar.getLongitude())
		BDPredio().alterarPredio(predio)
		return RespostaEditar("Predio Editado com sucesso!")

	def Deletar(self,pedido_deletar):
		predio = BDPredio().pegarPredio("WHERE id = %s ", (str(pedido_deletar.getId()),))
		BDPredio().removerPredio(predio)
		return RespostaDeletar("Predio Removido com sucesso!")
<|endoftext|>


<commit_before>
"""
https://github.com/westurner/house_prices/commit/3f586b57ac081a9bef27f43906b3df83a27f0b45
  - do_autoclean=True
  - do_get_dummies=False
"""
# import numpy as np
from collections import OrderedDict as odict

from datacleaner import autoclean_cv
import pandas as pd

from sklearn.ensemble import ExtraTreesRegressor, GradientBoostingRegressor, VotingClassifier
from sklearn.linear_model import ElasticNet
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline, make_union
from sklearn.preprocessing import FunctionTransformer

class PredictionPipeline(object):
    def __init__(self,
                 train_csv='train.csv',
                 test_csv='test.csv',
                 predict_csv='predict.csv',
                 classcolname='class'):
        self.cfg = odict()
        self.cfg['train_csv'] = train_csv
        self.cfg['test_csv'] = test_csv
        self.cfg['predict_csv'] = predict_csv
        self.cfg['classcolname'] = classcolname
        self.data = odict()

    def fit(self, **kwargs):
        train_csv = kwargs.get('train_csv', self.cfg['train_csv'])
        test_csv = kwargs.get('test_csv', self.cfg['test_csv'])
        classcolname = kwargs.get('classcolname', self.cfg['classcolname'])
        # TODO: log
        train_df = pd.read_csv(train_csv)
        test_df = pd.read_csv(test_csv)

        test_df[classcolname] = 0
        train_df_cleaned, test_df_cleaned = autoclean_cv(train_df, test_df,
                                                        ignore_update_check=True)
        del test_df[classcolname] # TODO: comment_these_xyz !?

        # features = np.delete(train_df_cleaned.view(np.float64).reshape(train_df_cleaned.size, -1), train_df_cleaned.dtype.names.index('class'), axis=1)

        train_class = train_df_cleaned[classcolname]

        del train_df_cleaned[classcolname]  # TODO: comment_these_xyz !?
        train_features = train_df_cleaned.as_matrix()

        training_features, testing_features, training_classes, testing_classes = \
            train_test_split(train_features, train_class, random_state=42)

        self.exported_pipeline = make_pipeline(
            make_union(
                make_union(VotingClassifier([('branch',
                    ElasticNet(alpha=1.0, l1_ratio=0.87)
                )]), FunctionTransformer(lambda X: X)),
                FunctionTransformer(lambda X: X)
            ),
            make_union(VotingClassifier([("est", GradientBoostingRegressor(learning_rate=0.02, max_features=0.02, n_estimators=500))]), FunctionTransformer(lambda X: X)),
            ExtraTreesRegressor(max_features=0.27, n_estimators=500)
        )

        self.exported_pipeline.fit(training_features, training_classes)

        self.data['train_df_cleaned'] = train_df_cleaned
        self.data['test_df_cleaned'] = test_df_cleaned
        self.data['train_features'] = train_features
        self.data['train_class'] = train_class

    def output_heuristics(self, predict_df):
        invalid_predict_df = predict_df[predict_df[0] <= 0]
        if len(invalid_predict_df):
            print(invalid_predict_df)
            raise Exception()

    def predict_with_test(self):
        test_df_cleaned = self.data['test_df_cleaned']
        results = self.exported_pipeline.predict(test_df_cleaned.as_matrix())
        predict_df = pd.DataFrame(results)
        predict_df.to_csv('../data/submission.csv')

        self.output_heuristics(predict_df)

    def predict_with_train(self):
        train_features = self.data['train_features']
        train_class = self.data['train_class']

        results = self.exported_pipeline.predict(train_features)
        train_results_df = pd.DataFrame(results, columns=['predicted'])
        train_results_df['actual'] = train_class
        train_results_df['diff'] = (train_results_df['predicted'] - train_results_df['actual'])
        # print(train_results_df['diff'])
        # print(train_results_df[train_results_df['diff'] != 0])
        class_sum = train_results_df['actual'].sum()
        print('class_sum:', class_sum)
        abs_error = train_results_df['diff'].abs().sum()
        print('abs error:', abs_error)
        percent_error = 100.0 * (1.0 - ((class_sum - abs_error) / class_sum))
        print('% error:  ', percent_error, '%')
        print('error**2: ', (train_results_df['diff']**2).sum())

    def run(self):
        self.fit()
        self.predict_with_test()
        self.predict_with_train()

def main():
    analysis = PredictionPipeline(
        train_csv='../data/train.csv',
        test_csv='../data/test.csv',
        predict_csv='../data/predict.csv',
        classcolname='SalePrice')
    analysis.run()
    return 0

if __name__ == "__main__":
    import sys
    sys.exit(main())
<commit_msg>SCH,BUG: tpot_house_prices__001__modified.py: (Id, SalePrice)<commit_after>
"""
https://github.com/westurner/house_prices/commit/3f586b57ac081a9bef27f43906b3df83a27f0b45
  - do_autoclean=True
  - do_get_dummies=False
"""
# import numpy as np
from collections import OrderedDict as odict

from datacleaner import autoclean_cv
import pandas as pd

from sklearn.ensemble import ExtraTreesRegressor, GradientBoostingRegressor, VotingClassifier
from sklearn.linear_model import ElasticNet
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline, make_union
from sklearn.preprocessing import FunctionTransformer

class PredictionPipeline(object):
    def __init__(self,
                 train_csv='train.csv',
                 test_csv='test.csv',
                 predict_csv='predict.csv',
                 classcolname='class'):
        self.cfg = odict()
        self.cfg['train_csv'] = train_csv
        self.cfg['test_csv'] = test_csv
        self.cfg['predict_csv'] = predict_csv
        self.cfg['classcolname'] = classcolname
        self.data = odict()

    def fit(self, **kwargs):
        train_csv = kwargs.get('train_csv', self.cfg['train_csv'])
        test_csv = kwargs.get('test_csv', self.cfg['test_csv'])
        classcolname = kwargs.get('classcolname', self.cfg['classcolname'])
        # TODO: log
        train_df = pd.read_csv(train_csv)
        test_df = pd.read_csv(test_csv)

        test_df[classcolname] = 0
        train_df_cleaned, test_df_cleaned = autoclean_cv(train_df, test_df,
                                                        ignore_update_check=True)
        del test_df[classcolname] # TODO: comment_these_xyz !?

        # features = np.delete(train_df_cleaned.view(np.float64).reshape(train_df_cleaned.size, -1), train_df_cleaned.dtype.names.index('class'), axis=1)

        train_class = train_df_cleaned[classcolname]

        del train_df_cleaned[classcolname]  # TODO: comment_these_xyz !?
        train_features = train_df_cleaned.as_matrix()

        training_features, testing_features, training_classes, testing_classes = \
            train_test_split(train_features, train_class, random_state=42)

        self.exported_pipeline = make_pipeline(
            make_union(
                make_union(VotingClassifier([('branch',
                    ElasticNet(alpha=1.0, l1_ratio=0.87)
                )]), FunctionTransformer(lambda X: X)),
                FunctionTransformer(lambda X: X)
            ),
            make_union(VotingClassifier([("est", GradientBoostingRegressor(learning_rate=0.02, max_features=0.02, n_estimators=500))]), FunctionTransformer(lambda X: X)),
            ExtraTreesRegressor(max_features=0.27, n_estimators=500)
        )

        self.exported_pipeline.fit(training_features, training_classes)

        self.data['train_df_cleaned'] = train_df_cleaned
        self.data['test_df_cleaned'] = test_df_cleaned
        self.data['train_features'] = train_features
        self.data['train_class'] = train_class

    def output_heuristics(self, predict_df):
        classcolname = self.cfg['classcolname']
        invalid_predict_df = predict_df[predict_df[classcolname] <= 0]
        if len(invalid_predict_df):
            print(invalid_predict_df)
            raise Exception()

    def predict_with_test(self):
        classcolname = self.cfg['classcolname']
        test_df_cleaned = self.data['test_df_cleaned']
        results = self.exported_pipeline.predict(test_df_cleaned.as_matrix())
        predict_df = pd.DataFrame(results, columns=[classcolname])
        predict_df.to_csv('../data/submission.csv',
                          index_label="Id")

        self.output_heuristics(predict_df)

    def predict_with_train(self):
        train_features = self.data['train_features']
        train_class = self.data['train_class']

        results = self.exported_pipeline.predict(train_features)
        train_results_df = pd.DataFrame(results, columns=['predicted'])
        train_results_df['actual'] = train_class
        train_results_df['diff'] = (train_results_df['predicted'] - train_results_df['actual'])
        # print(train_results_df['diff'])
        # print(train_results_df[train_results_df['diff'] != 0])
        class_sum = train_results_df['actual'].sum()
        print('class_sum:', class_sum)
        abs_error = train_results_df['diff'].abs().sum()
        print('abs error:', abs_error)
        percent_error = 100.0 * (1.0 - ((class_sum - abs_error) / class_sum))
        print('% error:  ', percent_error, '%')
        print('error**2: ', (train_results_df['diff']**2).sum())

    def run(self):
        self.fit()
        self.predict_with_test()
        self.predict_with_train()

def main():
    analysis = PredictionPipeline(
        train_csv='../data/train.csv',
        test_csv='../data/test.csv',
        predict_csv='../data/predict.csv',
        classcolname='SalePrice')
    analysis.run()
    return 0

if __name__ == "__main__":
    import sys
    sys.exit(main())
<|endoftext|>