<commit_before>import discord
import json
import requests
import wikipedia
import configparser
from discord.ext import commands
from sys import argv
from string import capwords
from urllib.parse import urlencode

#Read config for Google API Key
config = configparser.ConfigParser()
config.read("config.ini")
apiKey = config['Google']['API_Key']

class OnlineSearch:
    """
    Search stuff, or get stuff from the internet
    """
    def __init__(self, bot):
        self.bot = bot
        print('Addon "{}" loaded'.format(self.__class__.__name__))

    @commands.command()
    async def urban(self, *, term=None):
        """Lookup a term on Urban Dictionnary. If no term is specified, returns a random definition. Use a comma followed by a number to specify the definition that should be returned."""

        if term is None:
            r = requests.get("http://api.urbandictionary.com/v0/random")
        else:
            n = 1
            if "," in term:
                n = term.split(",", 1)[1]
                term = term.split(",", 1)[0]
            r = requests.get("http://api.urbandictionary.com/v0/define?term={}".format(term))

        js = r.json()

        if js["result_type"] != "no_results":
            try:
                firstResult = js["list"][int(n)-1]
                word = firstResult["word"]
                definition = firstResult["definition"]
                example = firstResult["example"]
                author = firstResult["author"]
                permalink = firstResult["permalink"]
                thumbsup = firstResult["thumbs_up"]
                thumbsdown = firstResult["thumbs_down"]

                chars = ['[', ']', '\\r\\n', "\\n"]
                for c in chars:
                    if c == chars[0] or c == chars[1]:
                        definition = definition.replace(c, '')
                        example = example.replace(c, '')
                    elif c == chars[2] or c == chars [3]:
                        definition = definition.replace(c, '\n')
                        example = example.replace(c, '\n')

                if example != "":
                    textExamples = example
                else:
                    textExamples = "None"

                try:
                    embed = discord.Embed(title="Definition of {}\n\n".format(word), colour=discord.Color.orange())
                    embed.set_thumbnail(url="http://i.imgur.com/B1gZbQz.png")
                    embed.url = permalink
                    embed.description = definition + "\n"
                    if textExamples != "None":
                        embed.add_field(name="__Example(s) :__", value=textExamples, inline=False)
                    embed.add_field(name="Upvotes", value="👍 **{}**".format(thumbsup), inline=True)
                    embed.add_field(name="Downvotes", value="👎 **{}**\n\n".format(thumbsdown), inline=True)
                    embed.set_footer(text="Defined by {0}".format(author))
                    await self.bot.say(embed=embed)
                except discord.errors.Forbidden:
                    await self.bot.say("**__Definition of {0}__**__ ({1})__\n\n\n".format(word, permalink) + definition + "\n\n" + "__Example(s) :__\n\n" + textExamples + "\n\n\n" + str(thumbsup) + " 👍\n\n" + str(thumbsdown) + " 👎\n\n\n\n" + "*Defined by " + author + "*")
            except ValueError:
                await self.bot.say("`Invalid syntax. If you want to specify which definition should be returned, use the following syntax :\n\".urban [term], [number]\"`")
            except IndexError:
                await self.bot.say("The specified definition does not exist! There are less than {} definitons for this term!".format(n))
        else:
            try:
                embed = discord.Embed(title="¯\_(ツ)_/¯", colour=discord.Color.orange())
                embed.url = "http://www.urbandictionary.com/define.php?term={}".format(term.replace(" ", "%20"))
                embed.description = "\nThere aren't any definitions for *{0}* yet.\n\n[Can you define it?](http://www.urbandictionary.com/add.php?word={1})\n".format(term, term.replace(" ", "%20"))
                embed.set_footer(text="Error 404", icon_url="http://i.imgur.com/w6TtWHK.png")
                await self.bot.say(embed=embed)
            except discord.errors.Forbidden:
                await self.bot.say("¯\_(ツ)_/¯\n\n\n" + "There are no definitions for *{}* yet\n\n".format(term) + "Can you define it ?\n( http://www.urbandictionary.com/add.php?word={} )".format(term.replace(" ", "%20")))

    @commands.command(name='whats', aliases=["what", "what's"])
    async def whats(self, *, term):
        """Defines / explains stuff"""

        if term[0:2] == "a " and term != "a":
            term = term[2:]
        elif term[0:3] == "an " and term != "an":
            term = term[3:]
        elif term[0:5] == "is a " and term != "is a":
            term = term[5:]
        elif term[0:6] == "is an " and term != "is an":
            term = term[6:]
        elif term[0:3] == "is " and term != "is":
            term = term[3:]
        elif term[0:4] == "are " and term != "are":
            term = term[4:]

        term = capwords(term)

        if term.lower() == "kai" or term.lower == "mitchy":
            kai = await self.bot.get_user_info("272908611255271425")
            try:
                embed = discord.Embed(title="Kai", colour=discord.Color.orange())
                embed.set_thumbnail(url=kai.avatar_url)
                embed.description = "An edgy kid that spends too much time on tumblr, previously named mitchy, previously named sans-serif"
                await self.bot.say(embed=embed)
            except discord.errors.Forbidden:
                await self.bot.say("**__Kai :__**\n\nAn edgy kid that spends too much time on tumblr, previously named mitchy, previously named sans-serif")
        elif term.lower() == "ubuntu":
                embed = discord.Embed(title="Ubuntu", colour=discord.Color.orange())
                embed.set_thumbnail(url="http://i.imgur.com/B1gZbQz.png")
                embed.url = "http://www.urbandictionary.com/define.php?term=ubuntu"
                embed.description = "Ubuntu is an ancient african word, meaning \"I can't configure Debian\"" + "\n"
                embed.add_field(name="__Example :__", value="I installed Ubuntu yesterday, it was way more easier than Debian", inline=False)
                embed.set_footer(text="Defined by oSuperDaveo")
                try:
                    await self.bot.say(embed=embed)
                except discord.errors.Forbidden:
                    await self.bot.say("**__Ubuntu :__**\n\n{}\n\n__Example :__\nI installed Ubuntu yesterday, it was way more easier than Debian.\n\n*Defined by oSuperDaveo*".format(embed.description))
        else:
            exception = False
            try:
                #Start wikipedia search
                wiki = wikipedia.page(term)
                permalink = "https://en.wikipedia.org/wiki/{}".format(term.replace(" ", "_"))
                embed = discord.Embed(title=term, colour=discord.Color.orange())
                embed.url = permalink
                if wiki.summary[-13:] == "may refer to:":
                    if len(wiki.content) > 1950:
                        embed.description = "{}...".format(wiki.content[0:1950])
                    else:
                        embed.description = wiki.content
                else:
                    if len(wiki.summary) > 1950:
                        embed.description = "{}...".format(wiki.summary[0:1950])
                    else:
                        embed.description = wiki.summary
                embed.set_footer(text="From Wikipedia", icon_url="http://i.imgur.com/DO4wDN4.png")
                try:
                    await self.bot.say(embed=embed)
                except discord.errors.Forbidden:
                    await self.bot.say("**__{}__**\n\n\n{}\n\n*Link : {}*".format(term, embed.description, permalink))
                #Images soon

            except wikipedia.exceptions.PageError:
                exception = True
                disambig = False
            except wikipedia.exceptions.DisambiguationError:
                exception = True
                disambig = True
            if exception is True:
                #Start Google Graph Knowledge search
                params = {
                    'query': term,
                    'limit': 1,
                    'indent': True,
                    'key': apiKey,
                }
                url = "https://kgsearch.googleapis.com/v1/entities:search?{}".format(urlencode(params))
                r = requests.get(url)
                js = r.json()

                try:
                    if js["itemListElement"]:
                        js = js["itemListElement"][0]["result"]

                        name = js["name"]
                        briefDescription = js["description"]
                        detailedDescription = js["detailedDescription"]["articleBody"]
                        if detailedDescription[-1] == " ":
                            detailedDescription = detailedDescription[:-1]
                        image = js["image"]["contentUrl"]
                        permalink = js["detailedDescription"]["url"]

                        embed = discord.Embed(title=name, colour=discord.Color.orange())
                        embed.url = permalink
                        embed.description = "\n{}\n\n\n*{}*\n".format(briefDescription, detailedDescription)
                        embed.set_image(url=image)
                        embed.set_footer(text="From Google Graph Knowledge", icon_url="http://i.imgur.com/2obljmu.png")

                        try:
                            await self.bot.say(embed=embed)
                        except discord.errors.Forbidden:
                            await self.bot.say("__**{}** ({})__\n\n{}\n\n\n*{}*\n\n{}\n".format(name, permalink, briefDescription, detailedDescription, image))
                        if disambig is True:
                            await self.bot.say("If this is not the definition you wanted, try being a bit more precise next time. {} can refer to many things!".format(term))
                    else:
                        await self.bot.say("Sorry, none of my sources have an explanation for this term :(")
                        #Coming soon : WikiData and urban dictionnary support

                except KeyError:
                    if js["error"]["code"] == 403: #Checks if API Key is specified
                        await self.bot.say("You did not mention a Google kgsearch API Key in the config.ini file! Please set up one here : https://console.developers.google.com/project/_/apiui/credential")
                    elif js["error"]["code"] == 400: #Checks if API Key is valid
                        await self.bot.say("The mentioned Google kgsearch API Key is invalid! Please set up a correct API Key here : https://console.developers.google.com/project/_/apiui/credential")

#TO-DO : google web and image search

#Load the extension
def setup(bot):
    bot.add_cog(OnlineSearch(bot))<commit_msg>Fix bug in onlinesearch.py<commit_after>import discord
import json
import requests
import wikipedia
import configparser
from discord.ext import commands
from sys import argv
from string import capwords
from urllib.parse import urlencode

#Read config for Google API Key
config = configparser.ConfigParser()
config.read("config.ini")
apiKey = config['Google']['API_Key']

class OnlineSearch:
    """
    Search stuff, or get stuff from the internet
    """
    def __init__(self, bot):
        self.bot = bot
        print('Addon "{}" loaded'.format(self.__class__.__name__))

    @commands.command()
    async def urban(self, *, term=None):
        """Lookup a term on Urban Dictionnary. If no term is specified, returns a random definition. Use a comma followed by a number to specify the definition that should be returned."""

        if term is None:
            r = requests.get("http://api.urbandictionary.com/v0/random")
        else:
            n = 1
            if "," in term:
                n = term.split(",", 1)[1]
                term = term.split(",", 1)[0]
            r = requests.get("http://api.urbandictionary.com/v0/define?term={}".format(term))

        js = r.json()

        if js["result_type"] != "no_results":
            try:
                firstResult = js["list"][int(n)-1]
                word = firstResult["word"]
                definition = firstResult["definition"]
                example = firstResult["example"]
                author = firstResult["author"]
                permalink = firstResult["permalink"]
                thumbsup = firstResult["thumbs_up"]
                thumbsdown = firstResult["thumbs_down"]

                chars = ['[', ']', '\\r\\n', "\\n"]
                for c in chars:
                    if c == chars[0] or c == chars[1]:
                        definition = definition.replace(c, '')
                        example = example.replace(c, '')
                    elif c == chars[2] or c == chars [3]:
                        definition = definition.replace(c, '\n')
                        example = example.replace(c, '\n')

                if example != "":
                    textExamples = example
                else:
                    textExamples = "None"

                try:
                    embed = discord.Embed(title="Definition of {}\n\n".format(word), colour=discord.Color.orange())
                    embed.set_thumbnail(url="http://i.imgur.com/B1gZbQz.png")
                    embed.url = permalink
                    embed.description = definition + "\n"
                    if textExamples != "None":
                        embed.add_field(name="__Example(s) :__", value=textExamples, inline=False)
                    embed.add_field(name="Upvotes", value="👍 **{}**".format(thumbsup), inline=True)
                    embed.add_field(name="Downvotes", value="👎 **{}**\n\n".format(thumbsdown), inline=True)
                    embed.set_footer(text="Defined by {0}".format(author))
                    await self.bot.say(embed=embed)
                except discord.errors.Forbidden:
                    await self.bot.say("**__Definition of {0}__**__ ({1})__\n\n\n".format(word, permalink) + definition + "\n\n" + "__Example(s) :__\n\n" + textExamples + "\n\n\n" + str(thumbsup) + " 👍\n\n" + str(thumbsdown) + " 👎\n\n\n\n" + "*Defined by " + author + "*")
            except ValueError:
                await self.bot.say("`Invalid syntax. If you want to specify which definition should be returned, use the following syntax :\n\".urban [term], [number]\"`")
            except IndexError:
                await self.bot.say("The specified definition does not exist! There are less than {} definitons for this term!".format(n))
        else:
            try:
                embed = discord.Embed(title="¯\_(ツ)_/¯", colour=discord.Color.orange())
                embed.url = "http://www.urbandictionary.com/define.php?term={}".format(term.replace(" ", "%20"))
                embed.description = "\nThere aren't any definitions for *{0}* yet.\n\n[Can you define it?](http://www.urbandictionary.com/add.php?word={1})\n".format(term, term.replace(" ", "%20"))
                embed.set_footer(text="Error 404", icon_url="http://i.imgur.com/w6TtWHK.png")
                await self.bot.say(embed=embed)
            except discord.errors.Forbidden:
                await self.bot.say("¯\_(ツ)_/¯\n\n\n" + "There are no definitions for *{}* yet\n\n".format(term) + "Can you define it ?\n( http://www.urbandictionary.com/add.php?word={} )".format(term.replace(" ", "%20")))

    @commands.command(name='whats', aliases=["what", "what's"])
    async def whats(self, *, term):
        """Defines / explains stuff"""

        if term[0:2] == "a " and term != "a":
            term = term[2:]
        elif term[0:3] == "an " and term != "an":
            term = term[3:]
        elif term[0:5] == "is a " and term != "is a":
            term = term[5:]
        elif term[0:6] == "is an " and term != "is an":
            term = term[6:]
        elif term[0:3] == "is " and term != "is":
            term = term[3:]
        elif term[0:4] == "are " and term != "are":
            term = term[4:]

        term = capwords(term)

        if term.lower() == "kai" or term.lower == "mitchy":
            kai = await self.bot.get_user_info("272908611255271425")
            try:
                embed = discord.Embed(title="Kai", colour=discord.Color.orange())
                embed.set_thumbnail(url=kai.avatar_url)
                embed.description = "An edgy kid that spends too much time on tumblr, previously named mitchy, previously named sans-serif"
                await self.bot.say(embed=embed)
            except discord.errors.Forbidden:
                await self.bot.say("**__Kai :__**\n\nAn edgy kid that spends too much time on tumblr, previously named mitchy, previously named sans-serif")
        elif term.lower() == "ubuntu":
                embed = discord.Embed(title="Ubuntu", colour=discord.Color.orange())
                embed.set_thumbnail(url="http://i.imgur.com/B1gZbQz.png")
                embed.url = "http://www.urbandictionary.com/define.php?term=ubuntu"
                embed.description = "Ubuntu is an ancient african word, meaning \"I can't configure Debian\"" + "\n"
                embed.add_field(name="__Example :__", value="I installed Ubuntu yesterday, it was way more easier than Debian", inline=False)
                embed.set_footer(text="Defined by oSuperDaveo")
                try:
                    await self.bot.say(embed=embed)
                except discord.errors.Forbidden:
                    await self.bot.say("**__Ubuntu :__**\n\n{}\n\n__Example :__\nI installed Ubuntu yesterday, it was way more easier than Debian.\n\n*Defined by oSuperDaveo*".format(embed.description))
        else:
            exception = False
            try:
                #Start wikipedia search
                wiki = wikipedia.page(term)
                permalink = "https://en.wikipedia.org/wiki/{}".format(term.replace(" ", "_"))
                embed = discord.Embed(title=term, colour=discord.Color.orange())
                embed.url = permalink
                if wiki.summary[-13:] == "may refer to:":
                    if len(wiki.content) > 1950:
                        embed.description = "{}...".format(wiki.content[0:1950])
                    else:
                        embed.description = wiki.content
                else:
                    if len(wiki.summary) > 1950:
                        embed.description = "{}...".format(wiki.summary[0:1950])
                    else:
                        embed.description = wiki.summary
                embed.set_footer(text="From Wikipedia", icon_url="http://i.imgur.com/DO4wDN4.png")
                try:
                    await self.bot.say(embed=embed)
                except discord.errors.Forbidden:
                    await self.bot.say("**__{}__**\n\n\n{}\n\n*Link : {}*".format(term, embed.description, permalink))
                #Images soon

            except wikipedia.exceptions.PageError:
                exception = True
                disambig = False
            except wikipedia.exceptions.DisambiguationError:
                exception = True
                disambig = True
            if exception is True:
                #Start Google Graph Knowledge search
                params = {
                    'query': term,
                    'limit': 1,
                    'indent': True,
                    'key': apiKey,
                }
                url = "https://kgsearch.googleapis.com/v1/entities:search?{}".format(urlencode(params))
                r = requests.get(url)
                js = r.json()

                try:
                    if js["itemListElement"]:
                        js = js["itemListElement"][0]["result"]

                        name = js["name"]

                        embed = discord.Embed(title=name, colour=discord.Color.orange())

                        try:
                            briefDescription = js["description"]
                            isBrieflyDetailed = True
                        except KeyError:
                            briefDescription = "No brief description available."
                            isBrieflyDetailed = False

                        try:
                            detailedDescription = js["detailedDescription"]["articleBody"]
                            isDetailed = True
                            if detailedDescription[-1] == " " or detailedDescription[-1] == "\n":
                                detailedDescription = detailedDescription[:-1]
                        except KeyError:
                            isDetailed = False
                            detailedDescription = "No detailed description available."

                        try:
                            permalink = js["detailedDescription"]["url"]
                            embed.url = permalink
                        except KeyError:
                            permalink = "no permalink available."

                        if isDetailed is True and isBrieflyDetailed is True:
                            embed.description = "\n{}\n\n\n*{}*\n".format(briefDescription, detailedDescription)
                        elif isDetailed is False and isBrieflyDetailed is True:
                            embed.description = "\n{}\n\n".format(briefDescription)
                        elif isDetailed is True and isBrieflyDetailed is False:
                            embed.description = "\n\n*{}*\n\n".format(detailedDescription)

                        try:
                            image = js["image"]["contentUrl"]
                            embed.set_image(url=image)
                        except KeyError:
                            image = "No image available."

                        embed.set_footer(text="From Google Graph Knowledge", icon_url="http://i.imgur.com/2obljmu.png")

                        try:
                            await self.bot.say(embed=embed)
                        except discord.errors.Forbidden:
                            await self.bot.say("__**{}** ({})__\n\n{}\n\n\n*{}*\n\n{}\n".format(name, permalink, briefDescription, detailedDescription, image))

                        if disambig is True:
                            await self.bot.say("If this is not the definition you wanted, try being a bit more precise next time. {} can refer to many things!".format(term))
                    else:
                        await self.bot.say("Sorry, none of my sources have an explanation for this term :(")
                        #Coming soon : WikiData and urban dictionnary support

                except KeyError:
                    if js["error"]["code"] == 403: #Checks if API Key is specified
                        await self.bot.say("You did not mention a Google kgsearch API Key in the config.ini file! Please set up one here : https://console.developers.google.com/project/_/apiui/credential")
                    elif js["error"]["code"] == 400: #Checks if API Key is valid
                        await self.bot.say("The mentioned Google kgsearch API Key is invalid! Please set up a correct API Key here : https://console.developers.google.com/project/_/apiui/credential")

#TO-DO : google web and image search

#Load the extension
def setup(bot):
    bot.add_cog(OnlineSearch(bot))<|endoftext|>


<commit_before>from __future__ import unicode_literals

from django import forms
from django.conf.urls import url
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as Admin
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import Group
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext_lazy as _

from .filters import ChoiceDropdownFilter
from administrator.models import DishCategory
from administrator.models import Role
from administrator.models import User
from restaurant.models import Dish
from restaurant.models import Restaurant

class RegistrationForm(UserCreationForm):

    """A form for users creation.

    Email, name, password and role are given.
    """

    email = forms.EmailField(required=True)

    class Meta:

        """Give some options (metadata) attached to the form."""

        model = User
        fields = ("role",)

    def save(self, commit=True):
        """Save a new user.

        Return a User object.
        """
        user = super(RegistrationForm, self).save(commit=False)
        user.email = self.cleaned_data["email"]
        user.role = self.cleaned_data["role"]
        user.set_is_staff(user.role)
        user.save()
        user.set_permissions(user.role)
        if commit:
            user.save()
        return user

class UserChangeForm(forms.ModelForm):

    """A form for users modification."""

    class Meta:

        """Give some options (metadata) attached to the form."""

        model = User
        fields = ("name", "email", "phone", "role", "status",)

    def save(self, commit=True):
        """Save the provided password in a hashed format and put
        is_active into an appropriate value (according to the
        user's status)

        Return a User object.
        """
        user = super(UserChangeForm, self).save(commit=False)
        user.set_is_active(user.status)
        user.set_is_staff(user.role)
        user.set_permissions(user.role)
        if commit:
            user.save()
        return user

def delete_selected_users(modeladmin, request, queryset):
    """Block selected users instead of dropping them."""
    for obj in queryset:
        obj.delete()

delete_selected_users.short_description = "Delete selested users"

class UserAdmin(Admin):

    """Represent a model in the admin interface."""

    form = UserChangeForm
    add_form = RegistrationForm

    search_fields = ("name", "email", "phone")
    list_display = ("name", "email", "phone", "role", "status")
    ordering = ["name"]
    list_per_page = 10
    list_filter = [
        ("status", ChoiceDropdownFilter),
        ("role", ChoiceDropdownFilter)
    ]

    fieldsets = (
        (None, {"fields": ("name", "email",)}),
        (_("Personal info"), {"fields": ("phone",)}),
        (_("Status"), {"fields": ("status",)}),
        (_("Permissions"), {"fields": ("role",)}),
    )

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        """Function, that shows only Manager and Sub-manager roles
            when Manager adds Sub-manager.
            """
        if db_field.name == "role":
            if request.user.role == Role.objects.get(name="Manager"):
                kwargs["queryset"] = Role.objects.filter(name="Manager") \
                    | Role.objects.filter(name="Sub-manager")
        return super(UserAdmin, self).formfield_for_foreignkey(
            db_field, request, **kwargs)

    # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
    # overrides get_fieldsets to use this attribute when creating a user.
    add_fieldsets = (
        (None, {
            "fields": ("email", "name", "password1", "password2", "role")}
         ),
    )

    actions = [delete_selected_users]

def soft_delete(modeladmin, request, queryset):
    """Soft delete function for QuerySet list."""
    for obj in queryset:
        obj.delete()

soft_delete.short_description = "Delete selected items"

def clone(modeladmin, queryset):
    """Clone instance"""
    for object in queryset:
        object.id = None
        object.save()

clone.short_description = "Clone"

class RestaurantForm(forms.ModelForm):

    """A form for restaurants modifications."""

    class Meta:

        """Give some options (metadata) attached to the form."""

        model = Restaurant
        fields = ("name", "logo", "location", "restaurant_type",
                  "tables_count", "description", "status",
                  "manager", "parent_restaurant")

    def __init__(self, *args, **kwargs):
        super(RestaurantForm, self).__init__(*args, **kwargs)
        users = User.objects.all()
        restaurants = Restaurant.objects.all()

        manager_choices = [(None, "---------")]

        for user in users:
            if (user.status != 1 and
                    user.role == Role.objects.get(name="Manager")):
                manager_choices.append((user.pk, user.get_full_name()))

        self.fields["manager"].choices = manager_choices

        parent_restaurant_choices = [(None, "---------")]

        if self.current_user.role != Role.objects.get(name="Admin"):
            for restaurant in restaurants:
                if restaurant.status != 1 \
                        and restaurant.manager_id == self.current_user.id:
                    parent_restaurant_choices.append(
                        (restaurant.id, restaurant.name)
                    )
        else:
            for restaurant in restaurants:
                parent_restaurant_choices.append(
                    (restaurant.id, restaurant.name)
                )

        self.fields["parent_restaurant"].choices = parent_restaurant_choices

    def save(self, commit=True):
        """Save the restaurant.

        Return a Restaurant object.
        """
        restaurant = super(RestaurantForm, self).save(commit=False)

        if restaurant.manager:
            restaurant.set_manager(restaurant.manager)

        if commit:
            restaurant.save()
        return restaurant

class RestaurantAdmin(admin.ModelAdmin):

    """Custom display in restaurant's list."""

    def get_queryset(self, request):
        """ Represent the objects.

        Return a QuerySet of all model instances that can be edited
        by the admin site.
        """
        qs = super(RestaurantAdmin, self).get_queryset(request)
        if request.user.role == Role.objects.get(name="Admin"):
            return qs
        return qs.filter(manager=request.user.id)

    def get_form(self, request, *args, **kwargs):
        """Return a ModelForm class with current user id for using in the
        admin"""
        form = super(RestaurantAdmin, self).get_form(request, *args, **kwargs)
        form.current_user = request.user
        return form

    form = RestaurantForm
    list_display = ("name", "restaurant_type", "status",
                    "tables_count", "manager")
    list_per_page = 15
    actions = [soft_delete, clone]
    admin.site.disable_action('delete_selected')
    list_filter = [('status', ChoiceDropdownFilter)]

    def change_view(self, request, object_id, form_url='', extra_context=None):
        """View for the model instance editing page."""
        extra_context = {
            "clone_url": request.path.replace("change/", "clone/")
        }

        return super(RestaurantAdmin, self).change_view(
            request,
            object_id,
            form_url,
            extra_context=extra_context,
        )

    def get_urls(self):
        """Return the URLs to be used for that ModelAdmin."""
        urls = super(RestaurantAdmin, self).get_urls()

        custom_urls = [
            url(
                r'^(?P<restaurant>.+)/clone/$',
                self.admin_site.admin_view(self.clone_view),
                name='clone',
            )
        ]

        return custom_urls + urls

    def clone_view(self, request, *args, **kwargs):
        """View for cloning instance."""
        new_restaurant = Restaurant.objects.get(id=kwargs["restaurant"])
        new_restaurant.id = None
        new_restaurant.save()

        return HttpResponseRedirect("/admin/restaurant/restaurant/")

    def _type_id(self, obj):
        return obj.type_id
    _type_id.short_description = "restaurant type"

class DishCategoryAdmin(admin.ModelAdmin):

    """Custom display dishes categories list."""

    list_display = ("name", "id", "is_deleted")
    list_per_page = 15

    def has_add_permission(self, request):
        return request.user.role == Role.objects.get(name='Admin')

    def has_change_permission(self, request, obj=None):
        return request.user.role == Role.objects.get(name='Admin')

    def has_delete_permission(self, request, obj=None):
        return request.user.role == Role.objects.get(name='Admin')

class DishAdmin(admin.ModelAdmin):

    """Custom display dishes list."""

    list_display = ("name", "category", "price", "weight", "available")

    def has_add_permission(self, request):
        return (request.user.role == Role.objects.get(name='Admin') or
                request.user.role == Role.objects.get(name='Manager'))

    def has_change_permission(self, request, obj=None):
        return (request.user.role == Role.objects.get(name='Admin') or
                request.user.role == Role.objects.get(name='Manager'))

    def has_delete_permission(self, request, obj=None):
        return (request.user.role == Role.objects.get(name='Admin') or
                request.user.role == Role.objects.get(name='Manager'))

admin.site.register(User, UserAdmin)
admin.site.register(Restaurant, RestaurantAdmin)
admin.site.register(DishCategory, DishCategoryAdmin)
admin.site.register(Dish, DishAdmin)
admin.site.unregister(Group)
<commit_msg>fix docsting pep257<commit_after>from __future__ import unicode_literals

from django import forms
from django.conf.urls import url
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as Admin
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import Group
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext_lazy as _

from .filters import ChoiceDropdownFilter
from administrator.models import DishCategory
from administrator.models import Role
from administrator.models import User
from restaurant.models import Dish
from restaurant.models import Restaurant

class RegistrationForm(UserCreationForm):

    """A form for users creation.

    Email, name, password and role are given.
    """

    email = forms.EmailField(required=True)

    class Meta:

        """Give some options (metadata) attached to the form."""

        model = User
        fields = ("role",)

    def save(self, commit=True):
        """Save a new user.

        Return a User object.
        """
        user = super(RegistrationForm, self).save(commit=False)
        user.email = self.cleaned_data["email"]
        user.role = self.cleaned_data["role"]
        user.set_is_staff(user.role)
        user.save()
        user.set_permissions(user.role)
        if commit:
            user.save()
        return user

class UserChangeForm(forms.ModelForm):

    """A form for users modification."""

    class Meta:

        """Give some options (metadata) attached to the form."""

        model = User
        fields = ("name", "email", "phone", "role", "status",)

    def save(self, commit=True):
        """Save the provided password in a hashed format and put
        is_active into an appropriate value (according to the
        user's status)

        Return a User object.
        """
        user = super(UserChangeForm, self).save(commit=False)
        user.set_is_active(user.status)
        user.set_is_staff(user.role)
        user.set_permissions(user.role)
        if commit:
            user.save()
        return user

def delete_selected_users(modeladmin, request, queryset):
    """Block selected users instead of dropping them."""
    for obj in queryset:
        obj.delete()

delete_selected_users.short_description = "Delete selested users"

class UserAdmin(Admin):

    """Represent a model in the admin interface."""

    form = UserChangeForm
    add_form = RegistrationForm

    search_fields = ("name", "email", "phone")
    list_display = ("name", "email", "phone", "role", "status")
    ordering = ["name"]
    list_per_page = 10
    list_filter = [
        ("status", ChoiceDropdownFilter),
        ("role", ChoiceDropdownFilter)
    ]

    fieldsets = (
        (None, {"fields": ("name", "email",)}),
        (_("Personal info"), {"fields": ("phone",)}),
        (_("Status"), {"fields": ("status",)}),
        (_("Permissions"), {"fields": ("role",)}),
    )

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        """Function, that shows only Manager and Sub-manager roles
        when Manager adds Sub-manager.
        """
        if db_field.name == "role":
            if request.user.role == Role.objects.get(name="Manager"):
                kwargs["queryset"] = Role.objects.filter(name="Manager") \
                    | Role.objects.filter(name="Sub-manager")
        return super(UserAdmin, self).formfield_for_foreignkey(
            db_field, request, **kwargs)

    # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
    # overrides get_fieldsets to use this attribute when creating a user.
    add_fieldsets = (
        (None, {
            "fields": ("email", "name", "password1", "password2", "role")}
         ),
    )

    actions = [delete_selected_users]

def soft_delete(modeladmin, request, queryset):
    """Soft delete function for QuerySet list."""
    for obj in queryset:
        obj.delete()

soft_delete.short_description = "Delete selected items"

def clone(modeladmin, queryset):
    """Clone instance"""
    for object in queryset:
        object.id = None
        object.save()

clone.short_description = "Clone"

class RestaurantForm(forms.ModelForm):

    """A form for restaurants modifications."""

    class Meta:

        """Give some options (metadata) attached to the form."""

        model = Restaurant
        fields = ("name", "logo", "location", "restaurant_type",
                  "tables_count", "description", "status",
                  "manager", "parent_restaurant")

    def __init__(self, *args, **kwargs):
        super(RestaurantForm, self).__init__(*args, **kwargs)
        users = User.objects.all()
        restaurants = Restaurant.objects.all()

        manager_choices = [(None, "---------")]

        for user in users:
            if (user.status != 1 and
                    user.role == Role.objects.get(name="Manager")):
                manager_choices.append((user.pk, user.get_full_name()))

        self.fields["manager"].choices = manager_choices

        parent_restaurant_choices = [(None, "---------")]

        if self.current_user.role != Role.objects.get(name="Admin"):
            for restaurant in restaurants:
                if restaurant.status != 1 \
                        and restaurant.manager_id == self.current_user.id:
                    parent_restaurant_choices.append(
                        (restaurant.id, restaurant.name)
                    )
        else:
            for restaurant in restaurants:
                parent_restaurant_choices.append(
                    (restaurant.id, restaurant.name)
                )

        self.fields["parent_restaurant"].choices = parent_restaurant_choices

    def save(self, commit=True):
        """Save the restaurant.

        Return a Restaurant object.
        """
        restaurant = super(RestaurantForm, self).save(commit=False)

        if restaurant.manager:
            restaurant.set_manager(restaurant.manager)

        if commit:
            restaurant.save()
        return restaurant

class RestaurantAdmin(admin.ModelAdmin):

    """Custom display in restaurant's list."""

    def get_queryset(self, request):
        """ Represent the objects.

        Return a QuerySet of all model instances that can be edited
        by the admin site.
        """
        qs = super(RestaurantAdmin, self).get_queryset(request)
        if request.user.role == Role.objects.get(name="Admin"):
            return qs
        return qs.filter(manager=request.user.id)

    def get_form(self, request, *args, **kwargs):
        """Return a ModelForm class with current user id for using in the
        admin"""
        form = super(RestaurantAdmin, self).get_form(request, *args, **kwargs)
        form.current_user = request.user
        return form

    form = RestaurantForm
    list_display = ("name", "restaurant_type", "status",
                    "tables_count", "manager")
    list_per_page = 15
    actions = [soft_delete, clone]
    admin.site.disable_action('delete_selected')
    list_filter = [('status', ChoiceDropdownFilter)]

    def change_view(self, request, object_id, form_url='', extra_context=None):
        """View for the model instance editing page."""
        extra_context = {
            "clone_url": request.path.replace("change/", "clone/")
        }

        return super(RestaurantAdmin, self).change_view(
            request,
            object_id,
            form_url,
            extra_context=extra_context,
        )

    def get_urls(self):
        """Return the URLs to be used for that ModelAdmin."""
        urls = super(RestaurantAdmin, self).get_urls()

        custom_urls = [
            url(
                r'^(?P<restaurant>.+)/clone/$',
                self.admin_site.admin_view(self.clone_view),
                name='clone',
            )
        ]

        return custom_urls + urls

    def clone_view(self, request, *args, **kwargs):
        """View for cloning instance."""
        new_restaurant = Restaurant.objects.get(id=kwargs["restaurant"])
        new_restaurant.id = None
        new_restaurant.save()

        return HttpResponseRedirect("/admin/restaurant/restaurant/")

    def _type_id(self, obj):
        return obj.type_id
    _type_id.short_description = "restaurant type"

class DishCategoryAdmin(admin.ModelAdmin):

    """Custom display dishes categories list."""

    list_display = ("name", "id", "is_deleted")
    list_per_page = 15

    def has_add_permission(self, request):
        return request.user.role == Role.objects.get(name='Admin')

    def has_change_permission(self, request, obj=None):
        return request.user.role == Role.objects.get(name='Admin')

    def has_delete_permission(self, request, obj=None):
        return request.user.role == Role.objects.get(name='Admin')

class DishAdmin(admin.ModelAdmin):

    """Custom display dishes list."""

    list_display = ("name", "category", "price", "weight", "available")

    def has_add_permission(self, request):
        return (request.user.role == Role.objects.get(name='Admin') or
                request.user.role == Role.objects.get(name='Manager'))

    def has_change_permission(self, request, obj=None):
        return (request.user.role == Role.objects.get(name='Admin') or
                request.user.role == Role.objects.get(name='Manager'))

    def has_delete_permission(self, request, obj=None):
        return (request.user.role == Role.objects.get(name='Admin') or
                request.user.role == Role.objects.get(name='Manager'))

admin.site.register(User, UserAdmin)
admin.site.register(Restaurant, RestaurantAdmin)
admin.site.register(DishCategory, DishCategoryAdmin)
admin.site.register(Dish, DishAdmin)
admin.site.unregister(Group)
<|endoftext|>


<commit_before>from discord.ext import commands
from random import choice
import aiohttp
import re
import urllib
from cogs.utils.chat_formatting import box

class AdvancedGoogle:

    def __init__(self, bot):
        self.bot = bot

    @commands.command(name="advgoogle", pass_context=True, no_pm=True)
    @commands.cooldown(5, 60, commands.BucketType.user)
    async def _advgoogle(self, ctx, text):
        """Its google, you search with it.
        Example: google A magical pug

        Special search options are available; Image, Images, Maps
        Example: google image You know, for kids! > Returns first image
        Another example: google maps New York
        Another example: google images cats > Returns a random image
        based on the query
        LEGACY EDITION! SEE HERE!
        https://twentysix26.github.io/Red-Docs/red_cog_approved_repos/#refactored-cogs

        Originally made by Kowlin https://github.com/Kowlin/refactored-cogs
        edited by Aioxas"""
        search_type = ctx.message.content[len(ctx.prefix+ctx.command.name)+1:].lower().split(" ")
        option = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1'
        }
        regex = [
            re.compile(",\"ou\":\"([^`]*?)\""),
            re.compile("
<a href=\"\/url\?url=([^`]*?)&"),
            re.compile("
<a href=\"([^`]*?)\""),
            re.compile("\/url?url=")
            ]

        # Start of Image
        if search_type[0] == "image":
            search_valid = str(ctx.message.content
                               [len(ctx.prefix+ctx.command.name)+1:].lower())
            if search_valid == "image":
                await self.bot.say("Please actually search something")
            else:
                uri = "https://www.google.com/search?tbm=isch&tbs=isz:m&q="
                quary = str(ctx.message.content
                            [len(ctx.prefix+ctx.command.name)+7:].lower())
                encode = urllib.parse.quote_plus(quary, encoding='utf-8',
                                                 errors='replace')
                uir = uri+encode

                async with aiohttp.get(uir, headers=option) as resp:
                    test = await resp.content.read()
                    unicoded = test.decode("unicode_escape")
                    query_find = regex[0].findall(unicoded)
                    try:
                        url = query_find[0]
                        await self.bot.say(url)
                    except IndexError:
                        await self.bot.say("Your search yielded no results.")
            # End of Image
        # Start of Image random
        elif search_type[0] == "images":
            search_valid = str(ctx.message.content
                               [len(ctx.prefix+ctx.command.name)+1:].lower())
            if search_valid == "image":
                await self.bot.say("Please actually search something")
            else:
                uri = "https://www.google.com/search?tbm=isch&tbs=isz:m&q="
                quary = str(ctx.message.content
                            [len(ctx.prefix+ctx.command.name)+7:].lower())
                encode = urllib.parse.quote_plus(quary, encoding='utf-8',
                                                 errors='replace')
                uir = uri+encode
                async with aiohttp.get(uir, headers=option) as resp:
                    test = await resp.content.read()
                    unicoded = test.decode("unicode_escape")
                    query_find = regex[0].findall(unicoded)
                    try:
                        url = choice(query_find)
                        await self.bot.say(url)
                    except IndexError:
                        await self.bot.say("Your search yielded no results.")
            # End of Image random
        # Start of Maps
        elif search_type[0] == "maps":
            search_valid = str(ctx.message.content
                               [len(ctx.prefix+ctx.command.name)+1:].lower())
            if search_valid == "maps":
                await self.bot.say("Please actually search something")
            else:
                uri = "https://www.google.com/maps/search/"
                quary = str(ctx.message.content
                            [len(ctx.prefix+ctx.command.name)+6:].lower())
                encode = urllib.parse.quote_plus(quary, encoding='utf-8',
                                                 errors='replace')
                uir = uri+encode
                await self.bot.say(uir)
            # End of Maps
        # Start of generic search
        else:
            uri = "https://www.google.com/search?q="
            quary = str(ctx.message.content
                        [len(ctx.prefix+ctx.command.name)+1:])
            encode = urllib.parse.quote_plus(quary, encoding='utf-8',
                                             errors='replace')
            uir = uri+encode
            async with aiohttp.get(uir, headers=option) as resp:
                test = str(await resp.content.read())
                query_find = regex[1].findall(test)
                if not query_find:
                    query_find = regex[2].findall(test)
                    try:
                        for r in query_find[:6]:
                            if regex[3].search(r):
                                m = regex[3].search(r)
                                r = r[:m.start()]
                                + r[m.end():]
                                r = self.unescape(r)
                            else:
                                r = self.unescape(r)
                        for i in range(len(query_find[:6])):
                            query_find[i] = "{}. <".format(i+1) + query_find[i] + ">"
                        await self.bot.say("Please type a number from the following list:\n"
                                           + box("{}".format("\n".join(query_find[:6]))))
                        answer = await self.bot.wait_for_message(timeout=15,
                                                                 author=ctx.message.author)
                        if answer is None:
                            query_find = query_find[0].split()[1]
                        elif answer.content.lower().strip().isdigit():
                            answer = answer.content.lower().strip()
                            answer = int(answer)
                            query_find = query_find[answer-1].split()[1]
                        else:
                            query_find = query_find[0].split()[1]
                        await self.bot.say("Here's your link: {}".format(query_find))
                    except IndexError:
                        await self.bot.say("Your search yielded no results.")
                elif regex[3].search(query_find[0]):
                        for r in query_find[:6]:
                            if regex[3].search(r):
                                m = regex[3].search(r)
                                r = r[:m.start()]
                                + r[m.end():]
                                r = self.unescape(r)
                            else:
                                r = self.unescape(r)
                        for i in range(len(query_find[:6])):
                            query_find[i] = "{}. <".format(i+1) + query_find[i] + ">"
                        await self.bot.say("Please type a number from the following list:\n"
                                           + box("{}".format("\n".join(query_find[:6]))))
                        answer = await self.bot.wait_for_message(timeout=15,
                                                                 author=ctx.message.author)
                        if answer is None:
                            query_find = query_find[0].split()[1]
                        elif answer.content.lower().strip().isdigit():
                            answer = answer.content.lower().strip()
                            answer = int(answer)
                            query_find = query_find[answer-1].split()[1]
                        else:
                            query_find = query_find[0].split()[1]
                        await self.bot.say("Here's your link: {}".format(query_find))

                else:
                    for r in query_find[:6]:
                        r = self.unescape(r)
                        r = "<"+r+">"
                    for i in range(len(query_find[:6])):
                        query_find[i] = "{}. <".format(i+1) + query_find[i] + ">"
                    await self.bot.say("Please type a number from the following list:\n"
                                       + box("{}".format("\n".join(query_find[:6]))))
                    answer = await self.bot.wait_for_message(timeout=15,
                                                             author=ctx.message.author)
                    if answer is None:
                        query_find = query_find[0].split()[1]
                    elif answer.content.lower().strip().isdigit():
                        answer = answer.content.lower().strip()
                        answer = int(answer)
                        query_find = query_find[answer-1].split()[1]
                    else:
                        query_find = query_find[0].split()[1]
                    await self.bot.say("Here's your link: {}".format(query_find))

            # End of generic search

    def unescape(self, msg):
        regex = ["
", "(?:\\\\[rn])", "(?:\\\\['])", "%25", "\(", "\)"]
        subs = ["\n", "", "'", "%", "%28", "%29"]

        for i in range(len(regex)):
            sub = re.sub(regex[i], subs[i], msg)
            msg = sub
        return msg

def setup(bot):
    n = AdvancedGoogle(bot)
    bot.add_cog(n)
<commit_msg>[advgoogle] Took off response box<commit_after>from discord.ext import commands
from random import choice
import aiohttp
import re
import urllib

class AdvancedGoogle:

    def __init__(self, bot):
        self.bot = bot

    @commands.command(name="advgoogle", pass_context=True, no_pm=True)
    @commands.cooldown(5, 60, commands.BucketType.user)
    async def _advgoogle(self, ctx, text):
        """Its google, you search with it.
        Example: google A magical pug

        Special search options are available; Image, Images, Maps
        Example: google image You know, for kids! > Returns first image
        Another example: google maps New York
        Another example: google images cats > Returns a random image
        based on the query
        LEGACY EDITION! SEE HERE!
        https://twentysix26.github.io/Red-Docs/red_cog_approved_repos/#refactored-cogs

        Originally made by Kowlin https://github.com/Kowlin/refactored-cogs
        edited by Aioxas"""
        search_type = ctx.message.content[len(ctx.prefix+ctx.command.name)+1:].lower().split(" ")
        option = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1'
        }
        regex = [
            re.compile(",\"ou\":\"([^`]*?)\""),
            re.compile("
<a href=\"\/url\?url=([^`]*?)&"),
            re.compile("
<a href=\"([^`]*?)\""),
            re.compile("\/url?url=")
            ]

        # Start of Image
        if search_type[0] == "image":
            search_valid = str(ctx.message.content
                               [len(ctx.prefix+ctx.command.name)+1:].lower())
            if search_valid == "image":
                await self.bot.say("Please actually search something")
            else:
                uri = "https://www.google.com/search?hl=en&tbm=isch&tbs=isz:m&q="
                quary = str(ctx.message.content
                            [len(ctx.prefix+ctx.command.name)+7:].lower())
                encode = urllib.parse.quote_plus(quary, encoding='utf-8',
                                                 errors='replace')
                uir = uri+encode

                async with aiohttp.get(uir, headers=option) as resp:
                    test = await resp.content.read()
                    unicoded = test.decode("unicode_escape")
                    query_find = regex[0].findall(unicoded)
                    try:
                        url = query_find[0]
                        await self.bot.say(url)
                    except IndexError:
                        await self.bot.say("Your search yielded no results.")
            # End of Image
        # Start of Image random
        elif search_type[0] == "images":
            search_valid = str(ctx.message.content
                               [len(ctx.prefix+ctx.command.name)+1:].lower())
            if search_valid == "image":
                await self.bot.say("Please actually search something")
            else:
                uri = "https://www.google.com/search?hl=en&tbm=isch&tbs=isz:m&q="
                quary = str(ctx.message.content
                            [len(ctx.prefix+ctx.command.name)+7:].lower())
                encode = urllib.parse.quote_plus(quary, encoding='utf-8',
                                                 errors='replace')
                uir = uri+encode
                async with aiohttp.get(uir, headers=option) as resp:
                    test = await resp.content.read()
                    unicoded = test.decode("unicode_escape")
                    query_find = regex[0].findall(unicoded)
                    try:
                        url = choice(query_find)
                        await self.bot.say(url)
                    except IndexError:
                        await self.bot.say("Your search yielded no results.")
            # End of Image random
        # Start of Maps
        elif search_type[0] == "maps":
            search_valid = str(ctx.message.content
                               [len(ctx.prefix+ctx.command.name)+1:].lower())
            if search_valid == "maps":
                await self.bot.say("Please actually search something")
            else:
                uri = "https://www.google.com/maps/search/"
                quary = str(ctx.message.content
                            [len(ctx.prefix+ctx.command.name)+6:].lower())
                encode = urllib.parse.quote_plus(quary, encoding='utf-8',
                                                 errors='replace')
                uir = uri+encode
                await self.bot.say(uir)
            # End of Maps
        # Start of generic search
        else:
            uri = "https://www.google.com/search?hl=en&q="
            quary = str(ctx.message.content
                        [len(ctx.prefix+ctx.command.name)+1:])
            encode = urllib.parse.quote_plus(quary, encoding='utf-8',
                                             errors='replace')
            uir = uri+encode
            async with aiohttp.get(uir, headers=option) as resp:
                test = str(await resp.content.read())
                query_find = regex[1].findall(test)
                if not query_find:
                    query_find = regex[2].findall(test)
                    try:
                        for r in query_find[:6]:
                            if regex[3].search(r):
                                m = regex[3].search(r)
                                r = r[:m.start()]
                                + r[m.end():]
                                r = self.unescape(r)
                            else:
                                r = self.unescape(r)
                        for i in range(len(query_find[:6])):
                            query_find[i] = "{}. <".format(i+1) + query_find[i] + ">"
                        await self.bot.say("Here are the first five results: {}".format("\n".join(query_find[:6])))
                    except IndexError:
                        await self.bot.say("Your search yielded no results.")
                elif regex[3].search(query_find[0]):
                        for r in query_find[:6]:
                            if regex[3].search(r):
                                m = regex[3].search(r)
                                r = r[:m.start()]
                                + r[m.end():]
                                r = self.unescape(r)
                            else:
                                r = self.unescape(r)
                        for i in range(len(query_find[:6])):
                            query_find[i] = "{}. <".format(i+1) + query_find[i] + ">"
                        await self.bot.say("Here are the first five results: {}".format("\n".join(query_find[:6])))

                else:
                    for r in query_find[:6]:
                        r = self.unescape(r)
                    for i in range(len(query_find[:6])):
                            query_find[i] = "{}. <".format(i+1) + query_find[i] + ">"
                    await self.bot.say("Here are the first five results: {}".format("\n".join(query_find[:6])))

            # End of generic search

    def unescape(self, msg):
        regex = ["
", "(?:\\\\[rn])", "(?:\\\\['])", "%25", "\(", "\)"]
        subs = ["\n", "", "'", "%", "%28", "%29"]

        for i in range(len(regex)):
            sub = re.sub(regex[i], subs[i], msg)
            msg = sub
        return msg

def setup(bot):
    n = AdvancedGoogle(bot)
    bot.add_cog(n)
<|endoftext|>


<commit_before>import logging
import re

from agent.Calculator import Calculator

logger = logging.getLogger()

class AgentReporter:
    def __init__(self, client, logger, args):
        self.client = client
        self.logger = logger
        self.args = args
        self.calculator = Calculator()
        self.attrs = {}
        self.daemon_name = self.client.info()["Name"]
        self.local_cache = {}

        for attr in args.attrs:
            kv = attr.split("=")
            if len(kv) > 1:
                self.attrs[kv[0]] = kv[1]
            else:
                self.attrs[kv[0]] = ""

    def export_events(self):
        """Send events to Logmatic.io"""
        try:
            events = self.client.events(decode=True)
            for event in events:
                if event["Type"] == "container":
                    meta = self._build_meta_from_event(event)
                    meta["@marker"] = ["docker", "docker-events"]
                    meta[self.args.ns]["event"] = event["Action"]
                    meta[self.args.ns]["status"] = event["status"]

                    # send it to Logmatic.io
                    self.logger.info("[Docker event] name:{} >> event:{} (image={})"
                                     .format(event["Actor"]["Attributes"]["name"],
                                             event["Action"],
                                             event["Actor"]["Attributes"]["image"]),
                                     extra=meta)

        except Exception:
            logger.exception("Unexpected error during the processing of events")

    def export_stats(self, container, detailed):
        """Send container stats to Logmatic.io"""
        try:
            meta = self._build_context(container)
            meta["@marker"] = ["docker", "docker-stats"]

            # call the API
            stats = container.stats(stream=False, decode=True)
            computed_stats = self.calculator.compute_human_stats(container, stats, detailed)
            meta[self.args.ns]["stats"] = computed_stats

            # format the event message
            message = ""
            if "error" not in computed_stats["cpu_stats"]:
                message += " cpu:{:.2f}%".format(computed_stats["cpu_stats"]["total_usage_pct"] * 100.0)
            if "error" not in computed_stats["memory_stats"]:
                message += " mem:{:.2f}%".format(computed_stats["memory_stats"]["usage_pct"] * 100.0)
            if "error" not in computed_stats["blkio_stats"]:
                message += " io:{:.2f}MB/s".format(computed_stats["blkio_stats"]["total_bps"] / 1000000.0)
            if "error" not in computed_stats["networks"]:
                message += " net:{:.2f}MB/s".format(
                    (computed_stats["networks"]["all"]["tx_bytes_ps"] + computed_stats["networks"]["all"][
                        "rx_bytes_ps"]) / 1000000.0)

            # send to Logmatic.io
            self.logger.info("[Docker stats] name:{} >> {} (host:{} image:{})"
                             .format(meta[self.args.ns]["name"],
                                     message,
                                     meta[self.args.ns]["hostname"],
                                     meta[self.args.ns]["image"]),
                             extra=meta)

        except Exception:
            logger.exception("Unexpected error during the processing of stats")

    def export_logs(self, container):
        """Send all container logs to Logmatic.io"""
        if container.attrs["Config"]["Image"].startswith("logmatic/logmatic-docker"):
            return
        try:
            line = ""
            meta = self._build_context(container)
            meta["@marker"] = ["docker", "docker-logs"]
            logs = container.logs(stream=True, stderr=True, stdout=True, follow=True, tail=0)
            for chunk in logs:
                # Append all char into a string until a \n
                if type(chunk) is not str:
                    chunk = chunk.decode()
                if chunk is not '\n':
                    line = line + chunk
                else:
                    self.logger.info(line, extra=meta)
                    line = ""

        except Exception:
            logger.exception("Unexpected error during the processing of stats")

    def _build_context(self, container):
        """Internal method, build the container context"""
        try:

            # Concatenate all labels
            labels = {}
            if len(container.attrs["Config"]["Labels"]) > 0:
                labels["all"] = []
                for label in container.attrs["Config"]["Labels"]:
                    labels["all"].append(label)
                    if container.attrs["Config"]["Labels"][label] != "":
                        labels[label] = container.attrs["Config"]["Labels"][label]

            # Add all container/image information
            meta = {
                self.args.ns: {
                    "id": container.id,
                    "short_id": container.short_id,
                    "name": container.name,
                    "status": container.status,
                    "daemon_name": self.daemon_name,
                    "labels": labels,
                    "hostname": container.attrs["Config"]["Hostname"],
                    "image": container.attrs["Config"]["Image"],
                    "created": container.attrs["Created"],
                    "pid": container.attrs["State"]["Pid"]
                },
                "severity": "INFO"
            }

            # Add all attributes
            if len(self.attrs):
                meta["attr"] = self.attrs

            return meta.copy()

        except Exception:
            logger.exception("Unexpected error during the processing of stats")

    def filter(self, containers):
        """Expose only the containers and the images that match the rules set
            - skip_image: exclude all images matching the regex
            - match_image: keep all images matching the regex
            - skip_container: exclude all containers matching the regex
            - match_container: keep all containers matching the regex
        """
        filtered = []

        for c in containers:
            # skip container by image name
            if self.args.skip_image and re.search(self.args.skip_image, c.attrs["Config"]["Image"]):
                continue
            # skip container by container name
            if self.args.skip_name and re.search(self.args.skip_name, c.name):
                continue
            # match container by image name
            if self.args.match_image and not re.search(self.args.match_image, c.attrs["Config"]["Image"]):
                continue
            # match container by name name
            if self.args.match_name and not re.search(self.args.match_name, c.name):
                continue

            # build local meta for each container
            self.local_cache[c.id] = self._build_context(c)

            filtered.append(c)

        logger.debug("Filter // Keeping containers: {}".format(filtered))
        return filtered

    def _build_meta_from_event(self, event):

        if event["id"] in self.local_cache:
            return self.local_cache[event["id"]].copy()

        try:
            container = self.client.containers.get(event["id"])
            meta = self._build_context(container)
            return meta
        except:
            logger.exception("Could not find container {}".format(event["id"]))
            # fallback

            # Add all container/image information
            meta = {
                self.args.ns: {
                    "id": event["id"],
                    "short_id": event["id"][:12],
                    "name": event["Actor"]["Attributes"]["name"],
                    "daemon_name": self.daemon_name,
                    "image": event["Actor"]["Attributes"]["image"]
                },
                "severity": "INFO"
            }
            return meta
<commit_msg>improve errors<commit_after>import logging
import re

from agent.Calculator import Calculator

logger = logging.getLogger()

class AgentReporter:
    def __init__(self, client, logger, args):
        self.client = client
        self.logger = logger
        self.args = args
        self.calculator = Calculator()
        self.attrs = {}
        self.daemon_name = self.client.info()["Name"]
        self.local_cache = {}

        for attr in args.attrs:
            kv = attr.split("=")
            if len(kv) > 1:
                self.attrs[kv[0]] = kv[1]
            else:
                self.attrs[kv[0]] = ""

    def export_events(self):
        """Send events to Logmatic.io"""
        try:
            events = self.client.events(decode=True)
            for event in events:
                if event["Type"] == "container":
                    meta = self._build_meta_from_event(event)
                    meta["@marker"] = ["docker", "docker-events"]
                    meta[self.args.ns]["event"] = event["Action"]
                    meta[self.args.ns]["status"] = event["status"]

                    # send it to Logmatic.io
                    self.logger.info("[Docker event] name:{} >> event:{} (image={})"
                                     .format(event["Actor"]["Attributes"]["name"],
                                             event["Action"],
                                             event["Actor"]["Attributes"]["image"]),
                                     extra=meta)

        except Exception as e:
            logger.exception("Unexpected error during the processing of events: {}".format(e))

    def export_stats(self, container, detailed):
        """Send container stats to Logmatic.io"""
        try:
            meta = self._build_context(container)
            meta["@marker"] = ["docker", "docker-stats"]

            # call the API
            stats = container.stats(stream=False, decode=True)
            computed_stats = self.calculator.compute_human_stats(container, stats, detailed)
            meta[self.args.ns]["stats"] = computed_stats

            # format the event message
            message = ""
            if "error" not in computed_stats["cpu_stats"]:
                message += " cpu:{:.2f}%".format(computed_stats["cpu_stats"]["total_usage_pct"] * 100.0)
            if "error" not in computed_stats["memory_stats"]:
                message += " mem:{:.2f}%".format(computed_stats["memory_stats"]["usage_pct"] * 100.0)
            if "error" not in computed_stats["blkio_stats"]:
                message += " io:{:.2f}MB/s".format(computed_stats["blkio_stats"]["total_bps"] / 1000000.0)
            if "error" not in computed_stats["networks"]:
                message += " net:{:.2f}MB/s".format(
                    (computed_stats["networks"]["all"]["tx_bytes_ps"] + computed_stats["networks"]["all"][
                        "rx_bytes_ps"]) / 1000000.0)

            # send to Logmatic.io
            self.logger.info("[Docker stats] name:{} >> {} (host:{} image:{})"
                             .format(meta[self.args.ns]["name"],
                                     message,
                                     meta[self.args.ns]["hostname"],
                                     meta[self.args.ns]["image"]),
                             extra=meta)

        except Exception as e:
            logger.exception("Unexpected error during the processing of stats: {}".format(e))

    def export_logs(self, container):
        """Send all container logs to Logmatic.io"""
        if container.attrs["Config"]["Image"].startswith("logmatic/logmatic-docker"):
            return
        try:
            line = ""
            meta = self._build_context(container)
            meta["@marker"] = ["docker", "docker-logs"]
            logs = container.logs(stream=True, stderr=True, stdout=True, follow=True, tail=0)
            for chunk in logs:
                # Append all char into a string until a \n
                if type(chunk) is not str:
                    chunk = chunk.decode()
                if chunk is not '\n':
                    line = line + chunk
                else:
                    self.logger.info(line, extra=meta)
                    line = ""

        except Exception as e:
            logger.exception("Unexpected error during the processing of logs: {}".format(e))

    def _build_context(self, container):
        """Internal method, build the container context"""
        try:

            # Concatenate all labels
            labels = {}
            if len(container.attrs["Config"]["Labels"]) > 0:
                labels["all"] = []
                for label in container.attrs["Config"]["Labels"]:
                    labels["all"].append(label)
                    if container.attrs["Config"]["Labels"][label] != "":
                        labels[label] = container.attrs["Config"]["Labels"][label]

            # Add all container/image information
            meta = {
                self.args.ns: {
                    "id": container.id,
                    "short_id": container.short_id,
                    "name": container.name,
                    "status": container.status,
                    "daemon_name": self.daemon_name,
                    "labels": labels,
                    "hostname": container.attrs["Config"]["Hostname"],
                    "image": container.attrs["Config"]["Image"],
                    "created": container.attrs["Created"],
                    "pid": container.attrs["State"]["Pid"]
                },
                "severity": "INFO"
            }

            # Add all attributes
            if len(self.attrs):
                meta["attr"] = self.attrs

            return meta.copy()

        except Exception as e:
            logger.exception("Unexpected error during the processing of context: {}".format(e))

    def filter(self, containers):
        """Expose only the containers and the images that match the rules set
            - skip_image: exclude all images matching the regex
            - match_image: keep all images matching the regex
            - skip_container: exclude all containers matching the regex
            - match_container: keep all containers matching the regex
        """
        filtered = []

        for c in containers:
            # skip container by image name
            if self.args.skip_image and re.search(self.args.skip_image, c.attrs["Config"]["Image"]):
                continue
            # skip container by container name
            if self.args.skip_name and re.search(self.args.skip_name, c.name):
                continue
            # match container by image name
            if self.args.match_image and not re.search(self.args.match_image, c.attrs["Config"]["Image"]):
                continue
            # match container by name name
            if self.args.match_name and not re.search(self.args.match_name, c.name):
                continue

            # build local meta for each container
            self.local_cache[c.id] = self._build_context(c)

            filtered.append(c)

        logger.debug("Filter // Keeping containers: {}".format(filtered))
        return filtered

    def _build_meta_from_event(self, event):

        if event["id"] in self.local_cache:
            return self.local_cache[event["id"]].copy()

        try:
            container = self.client.containers.get(event["id"])
            meta = self._build_context(container)
            return meta
        except:
            logger.exception("Could not find container {}".format(event["id"]))
            # fallback

            # Add all container/image information
            meta = {
                self.args.ns: {
                    "id": event["id"],
                    "short_id": event["id"][:12],
                    "name": event["Actor"]["Attributes"]["name"],
                    "daemon_name": self.daemon_name,
                    "image": event["Actor"]["Attributes"]["image"]
                },
                "severity": "INFO"
            }
            return meta
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""The mailroom script will do stuff."""

import sys

donor_1 = {'name': 'Steven', 'donations': [3, 6]}
donor_2 = {'name': 'David', 'donations': [1, 10, 9]}
donor_3 = {'name': 'Zach', 'donations': [1, 2]}
donor_list = [donor_1, donor_2, donor_3]

donor_dict = {
    'Steven': [3, 6],
    'David': [1, 5, 7],
    'Zach': [5, 3, 6]
}

def main():
    while True:
        welcome_message()
        user_input = input("Selection: ")
        if not is_valid_input(user_input, ['1', '2', '3']):
            continue
        handle_input(user_input)()

def welcome_message():
    print("Welcome to the donor database. Please enter 1, 2 or 3")

def is_valid_input(user_input, my_list):
    """Promt user input check if in a list."""
    if user_input in my_list:
        return True
    return False

def handle_input(user_input):
    actions = {'1': send_thanks, '2':list_donors, '3': exit}
    return actions[user_input]

def generate_thankyou(donor, amount):
    print('Thank you, {0} for your donation of {1} dollars.'.format(donor, amount))

def send_thanks():
    donor = input ("Enter Donor name or list > ")
    if donor == 'list':
        print(donor_dict)
        send_thanks()
    elif donor != 'return':
        amount = ''
        while type(amount) is str:
            amount = input("Input donation amount: ")
            try:
                amount = int(amount)
            except ValueError:
                continue
            else:
                donor_dict.setdefault(donor, []).append(amount)
        generate_thankyou(donor, amount)
    elif donor == 'return':
        return

def list_donors():
    pass

def exit():
    sys.exit(0)

if __name__ == '__main__':
    main()
<commit_msg>Add table to view past donations.<commit_after># -*- coding: utf-8 -*-
"""The mailroom script will do stuff."""

import sys

donor_dict = {
    'Steven': [3, 6],
    'David': [1, 5, 7],
    'Zach': [5, 3, 6]
}

def main():
    while True:
        welcome_message()
        user_input = input("Selection: ")
        if not is_valid_input(user_input, ['1', '2', '3']):
            continue
        handle_input(user_input)()

def welcome_message():
    print("Welcome to the donor database.")
    print("Enter 1 to send thank you note")
    print("Enter 2 to view report")
    print("Enter 3 to exit\n")

def is_valid_input(user_input, my_list):
    """Promt user input check if in a list."""
    if user_input in my_list:
        return True
    return False

def handle_input(user_input):
    actions = {'1': send_thanks, '2': report_donors, '3': exit}
    return actions[user_input]

def generate_thankyou(donor, amount):
    print('Thank you, {0} for your donation of {1} dollars.\n'.format(
        donor, amount)
    )

def send_thanks():
    donor = input("Enter Donor name or list > ")
    if donor == 'list':
        print(donor_dict)
        send_thanks()
    elif donor != 'return':
        amount = ''
        while type(amount) is str:
            amount = input("Input donation amount: ")
            try:
                amount = int(amount)
            except ValueError:
                continue
            else:
                donor_dict.setdefault(donor, []).append(amount)
        generate_thankyou(donor, amount)
    elif donor == 'return':
        return

def report_donors():
    donor_list = []
    for donor in donor_dict:
        donor_list.append((donor, sum(donor_dict[donor])))
    donor_list.sort(key=lambda x: -x[1])
    print('-' * 24 + '|' + '-' * 10)
    print('{0}{1}|{2}'.format('Name', ' ' * 20, 'Total'))
    print('-' * 24 + '|' + '-' * 10)
    for donor in donor_list:
        print('{0}{1}|{2}'.format(
            donor[0], ' ' * (24 - len(donor[0])), donor[1])
        )
    print('-' * 24 + '|' + '-' * 10 + '\n')

def exit():
    sys.exit(0)

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


<commit_before>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 10 16:43:52 2017

@author: leminen
"""
import os
import tensorflow as tf
import src.utils as utils

class BasicModel(object):
    def __init__(self):
        self.model = 'BasicModel'
        self.dir_logs        = 'models/' + self.model + '/logs'
        self.dir_checkpoints = 'models/' + self.model + '/checkpoints'
        self.dir_results     = 'models/' + self.model + '/results'

        utils.checkfolder(self.dir_checkpoints)
        utils.checkfolder(self.dir_logs)
        utils.checkfolder(self.dir_results)

    def _create_inference(self):
        """ Define the inference model for the network
        Args:

        Returns:
        """
        ### self.output = f(self.input) ## define f

    def _create_losses(self):
        """ Define loss function[s] for the network
        Args:

        Returns:
        """
        ### self.loss = f(self.output, self.input) ## define f

    def _create_optimizer(self):
        """ Create optimizer for the network
        Args:

        Returns:
        """
        ### self.optimizer_op = f(self.loss) ## define f

    def _create_summaries(self):
        """ Create summaries for the network
        Args:

        Returns:
        """

        ### Add summaries

        self.summary_op = tf.summary.merge_all()

    def train(self, dataset_str, epoch_N, batch_N):
        """ Run training of the network
        Args:

        Returns:
        """

        # Use dataset for loading in datasamples from .tfrecord (https://www.tensorflow.org/programmers_guide/datasets#consuming_tfrecord_data)
        # The iterator will get a new batch from the dataset each time a sess.run() is executed on the graph.
        filenames = tf.placeholder(tf.string, shape=[None])

        dataset = tf.contrib.data.TFRecordDataset(filenames)
        dataset = dataset.shuffle(buffer_size = 10000, seed = None)
        dataset = dataset.batch(batch_size = batch_N)
        iterator = dataset.make_initializable_iterator()
        self.input = iterator.get_next()

        # define model, loss, optimizer and summaries.
        self._create_inference()
        self._create_losses()
        self._create_optimizer()
        self._create_summaries()

        with tf.Session() as sess:

            # Initialize all model Variables.
            sess.run(tf.global_variables_initializer())

            # Create Saver object for loading and storing checkpoints
            saver = tf.train.Saver()

            # Create Writer object for storing graph and summaries for TensorBoard
            writer = tf.summary.FileWriter(self.dir_logs, sess.graph)

            # Reload Tensor values from latest checkpoint
            ckpt = tf.train.get_checkpoint_state(self.dir_checkpoints)
            epoch_start = 0
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)
                ckpt_name = os.path.basename(ckpt.model_checkpoint_path)
                epoch_start = int(ckpt_name.split('-')[-1])

            counter = 0

            # Do training loops
            for epoch_n in range(epoch_start, epoch_N):

                training_filenames = ["/data/dataset/file1.tfrecord", "/data/dataset/file2.tfrecord"] # EXAMPLE !
                sess.run(iterator.initializer, feed_dict={filenames: training_filenames})

                while True:
                    try:
                        _, summary = sess.run([self.optimizer_op, self.summary_op])

                        writer.add_summary(summary, global_step=counter)
                        counter =+ 1

                    except tf.errors.OutOfRangeError:
                        break

                if epoch_n % 1 == 0:
                    saver.save(sess,os.path.join(self.dir_checkpoints, self.model + '.model'), global_step=epoch_n)

    def predict(self):
        """ Run prediction of the network
        Args:

        Returns:
        """

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())

<commit_msg>minor fix<commit_after>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 10 16:43:52 2017

@author: leminen
"""
import os
import tensorflow as tf
import src.models.ops_util as ops
import src.utils as utils

class BasicModel(object):
    def __init__(self):
        self.model = 'BasicModel'
        self.dir_logs        = 'models/' + self.model + '/logs'
        self.dir_checkpoints = 'models/' + self.model + '/checkpoints'
        self.dir_results     = 'models/' + self.model + '/results'

        utils.checkfolder(self.dir_checkpoints)
        utils.checkfolder(self.dir_logs)
        utils.checkfolder(self.dir_results)

    def _create_inference(self):
        """ Define the inference model for the network
        Args:

        Returns:
        """
        ### self.output = f(self.input) ## define f

    def _create_losses(self):
        """ Define loss function[s] for the network
        Args:

        Returns:
        """
        ### self.loss = f(self.output, self.input) ## define f

    def _create_optimizer(self):
        """ Create optimizer for the network
        Args:

        Returns:
        """
        ### self.optimizer_op = f(self.loss) ## define f

    def _create_summaries(self):
        """ Create summaries for the network
        Args:

        Returns:
        """

        ### Add summaries

        self.summary_op = tf.summary.merge_all()

    def train(self, dataset_str, epoch_N, batch_N):
        """ Run training of the network
        Args:

        Returns:
        """

        # Use dataset for loading in datasamples from .tfrecord (https://www.tensorflow.org/programmers_guide/datasets#consuming_tfrecord_data)
        # The iterator will get a new batch from the dataset each time a sess.run() is executed on the graph.
        filenames = tf.placeholder(tf.string, shape=[None])

        dataset = tf.contrib.data.TFRecordDataset(filenames)
        dataset = dataset.shuffle(buffer_size = 10000, seed = None)
        dataset = dataset.batch(batch_size = batch_N)
        iterator = dataset.make_initializable_iterator()
        self.input = iterator.get_next()

        # define model, loss, optimizer and summaries.
        self._create_inference()
        self._create_losses()
        self._create_optimizer()
        self._create_summaries()

        with tf.Session() as sess:

            # Initialize all model Variables.
            sess.run(tf.global_variables_initializer())

            # Create Saver object for loading and storing checkpoints
            saver = tf.train.Saver()

            # Create Writer object for storing graph and summaries for TensorBoard
            writer = tf.summary.FileWriter(self.dir_logs, sess.graph)

            # Reload Tensor values from latest checkpoint
            ckpt = tf.train.get_checkpoint_state(self.dir_checkpoints)
            epoch_start = 0
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)
                ckpt_name = os.path.basename(ckpt.model_checkpoint_path)
                epoch_start = int(ckpt_name.split('-')[-1])

            counter = 0

            # Do training loops
            for epoch_n in range(epoch_start, epoch_N):

                training_filenames = ["/data/dataset/file1.tfrecord", "/data/dataset/file2.tfrecord"] # EXAMPLE !
                sess.run(iterator.initializer, feed_dict={filenames: training_filenames})

                while True:
                    try:
                        _, summary = sess.run([self.optimizer_op, self.summary_op])

                        writer.add_summary(summary, global_step=counter)
                        counter =+ 1

                    except tf.errors.OutOfRangeError:
                        break

                if epoch_n % 1 == 0:
                    saver.save(sess,os.path.join(self.dir_checkpoints, self.model + '.model'), global_step=epoch_n)

    def predict(self):
        """ Run prediction of the network
        Args:

        Returns:
        """

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())

<|endoftext|>