<commit_before>import asyncio
import logging
from typing import Optional

from collections import defaultdict

import enum
from random import randint, choice

import discord
from discord.ext.commands import guild_only
from lifesaver.bot import Cog, Context, command

class MafiaGameState(enum.Enum):
    WAITING = enum.auto()
    STARTED = enum.auto()

class MafiaGame:
    def __init__(self, bot, *, master: discord.Member, channel: discord.TextChannel):
        self.bot = bot
        # the person who started the game
        self.master = master
        # the channel the lobby started in
        self.channel = channel
        # the main game channel where both mafia and town can talk
        self.game_channel: Optional[discord.TextChannel] = None
        # guild where the game started
        self.guild: discord.Guild = channel.guild
        # set of alive players
        self.players = {master}
        # set of all players who joined at the beginning
        self.participants = set()
        # set of alive mafia
        self.mafia = set()
        # mafia text channel
        self.mafia_chat: Optional[discord.TextChannel] = None
        # current game state (unused)
        self.state = MafiaGameState.WAITING
        # day counter
        self.day = 1
        # currently in daytime?
        self.daytime = True
        # dict of hanging votes during the daytime:
        # key: user id of person to hang - value: list of user ids who voted to hang that person
        # 1: [2, 3] - users 2 and 3 are voting for 1 to be hanged
        self.hanging_votes = defaultdict(list)
        # victim that will be killed tonight, decided by mafia. can be none if they decide to spare.
        self.victim_tonight: Optional[discord.Member] = None
        self.log = logging.getLogger(__name__)

    # number of players required before game autostarts
    REQUIRED_PLAYERS = 6
    # debug mode: arbitrarily shorten some wait times
    DEBUG = False

    async def gather_players(self):
        m = await self.channel.send('Created a lobby. Join by clicking \N{RAISED HAND}!')
        await m.add_reaction('\N{RAISED HAND}')

        def reaction_check(r, u):
            return r.message.id == m.id and not u.bot

        while True:
            reaction, user = await self.bot.wait_for('reaction_add', check=reaction_check)

            # ignore custom emoji reactions
            if not isinstance(reaction.emoji, str):
                continue

            if reaction.emoji == '\N{RAISED HAND}':
                self.players.add(user)

                # send notice that someone has joined
                await self.channel.send(
                    f'**{user}** has joined the lobby. There are now {len(self.players)} player(s) in the lobby.'
                    f' {self.REQUIRED_PLAYERS - len(self.players)} more player(s) are/is required for the game to start.'
                )

                # update lobby message with list of active players
                players_formatted = ', '.join(str(player) for player in self.players)
                await m.edit(
                    content='Join by clicking \N{RAISED HAND}! Current players: ' + players_formatted
                )

            if len(self.players) == self.REQUIRED_PLAYERS or (reaction.emoji == '\N{OK HAND SIGN}' and user == self.master):
                if len(self.players) < 3:
                    await self.channel.send("Override failed. At least 3 players are required for a game.")
                    continue
                await m.edit(content='Game started!')
                try:
                    await m.clear_reactions()
                except discord.HTTPException:
                    pass
                await self.channel.send('Okay, game starting!')
                break

        # players set will be modified as the game goes on, so let's keep a set of participants
        # when we alltalk later
        self.participants = self.players.copy()
        overwrites = {
            self.guild.default_role: discord.PermissionOverwrite(read_messages=False),
            self.guild.me: discord.PermissionOverwrite(read_messages=True, send_messages=True),
        }
        for player in self.players:
            overwrites[player] = discord.PermissionOverwrite(read_messages=True)
        self.game_channel = await self.guild.create_text_channel(
            f'd-mafia-game-{randint(0, 10000) + 10000}', overwrites=overwrites
        )

    async def pick_mafia(self):
        first_mafia = choice(list(self.players))
        second_mafia = choice(list(self.players.difference({first_mafia})))
        self.log.info('Picked 2 mafia. %s, %s', first_mafia, second_mafia)
        self.mafia = {first_mafia, second_mafia}

        rid = randint(0, 10000) + 10000
        self.mafia_chat = await self.guild.create_text_channel(f'd-mafia-chat-{rid}', overwrites={
            self.guild.default_role: discord.PermissionOverwrite(read_messages=False),
            first_mafia: discord.PermissionOverwrite(read_messages=True),
            second_mafia: discord.PermissionOverwrite(read_messages=True),
            self.guild.me: discord.PermissionOverwrite(read_messages=True, send_messages=True),
        })
        await self.mafia_chat.send(
            f'{first_mafia.mention}, {second_mafia.mention}: Greet each other, mafia! Here you may coordinate with '
            'your evil partner.'
        )

    async def notify_roles(self):
        for player in self.players:
            # mafia are notified when they are picked <3
            if player not in self.mafia:
                await player.send(
                    f'**You are innocent!** Your goal is to hang the mafia. Now go to: {self.game_channel.mention}, '
                    "that's where the game will unfold."
                )

    async def pick_victim(self):
        while True:
            message = await self.bot.wait_for(
                'message',
                check=lambda m: m.channel == self.mafia_chat and m.author in self.mafia and m.author in self.players
            )
            if message.content.startswith('!kill '):
                target_name = message.content[len('!kill '):]
                target = discord.utils.find(lambda player: player.name.lower() == target_name.lower() or player.mention == target_name, self.players)
                if not target or target in self.mafia:
                    await self.mafia_chat.send(f'{message.author.mention}: Invalid choice.')
                    continue
                self.victim_tonight = target
                await self.mafia_chat.send(
                    f"Okay, **{target}** will be killed tonight."
                )

    async def alltalk(self):
        # let all people who joined speak again at the end because we're nice <3
        for participant in self.participants:
            self.log.info('Alltalk: Allowing %s to speak in #%s.', participant, self.game_channel.name)
            await self.game_channel.set_permissions(participant, read_messages=True, send_messages=True)

    async def gather_hanging_votes(self):
        # TODO: these are bad
        def has_voted(voter):
            for target_id, voter_ids in self.hanging_votes.items():
                if voter.id in voter_ids:
                    return True
            return False

        def get_vote(voter):
            for target_id, voter_ids in self.hanging_votes.items():
                if voter.id in voter_ids:
                    return target_id

        while True:
            message = await self.bot.wait_for(
                'message',
                check=lambda m: m.channel == self.game_channel and m.author in self.players
            )

            if not message.content.startswith('!vote '):
                continue

            try:
                target_name = message.content[len('!vote '):]
                target = discord.utils.find(lambda player: player.name.lower() == target_name.lower() or player.mention == target_name, self.players)
                if not target:
                    await self.game_channel.send(f'{message.author.mention}: Invalid choice.')
                    continue
                if target == message.author:
                    await self.game_channel.send(f'{message.author.mention}: Are you trying to commit suicide? No.')
                    continue
                self.log.info('Received VALID !vote command: %s wants to hang %s.', message.author, target)
                if has_voted(message.author):
                    previous_target = get_vote(message.author)
                    if previous_target == target.id:
                        # voting for same person?
                        self.log.info('Detected duplicate vote (%s to %s), discarding.', message.author, target)
                        await self.game_channel.send(f'{message.author.mention}: You already voted for that person.')
                        continue
                    self.log.info('%s has already voted for %d, removing their vote.', message.author, previous_target)
                    self.hanging_votes[previous_target].remove(message.author.id)
                self.log.info("Registering %s's vote to hang %s.", message.author, target)
                # because this defaultdict gave me a keyerror.
                self.log.info('hanging_votes: (%s): %s', type(self.hanging_votes).__name__, repr(self.hanging_votes))
                self.hanging_votes[target.id].append(message.author.id)
                await self.game_channel.send(
                    f'{message.author} has voted for {target} to be hanged.\n\n' +
                    '\n'.join(f'<@{key}>: {len(value)} vote(s)' for key, value in self.hanging_votes.items() if len(value) != 0)
                )
            except Exception:
                self.log.exception('Error has occurred while processing votes:')

    async def lock(self):
        await self.game_channel.set_permissions(self.guild.default_role, send_messages=False)

    async def unlock(self):
        await self.game_channel.set_permissions(self.guild.default_role, send_messages=None)

    async def game_over(self, *, mafia_won: bool):
        if mafia_won:
            mafia_alive = ', '.join(mafia.mention for mafia in self.mafia)
            await self.game_channel.send(f'**Currently Alive Mafia:** {mafia_alive}')
        else:
            townies_alive = ', '.join(player.mention for player in self.players if player not in self.mafia)
            await self.game_channel.send(f'**Currently Alive Town:** {townies_alive}')

        await asyncio.sleep(2.0)
        await self.alltalk()
        await asyncio.sleep(8.0)

    async def game_loop(self):
        mentions = ', '.join(player.mention for player in self.players)
        await self.game_channel.send(f'{mentions}: The main game will be conducted here! Make sure to have fun!')
        await asyncio.sleep(5.0)

        while True:
            # send current day/daytime state to players
            emoji = '\N{BLACK SUN WITH RAYS}' if self.daytime else '\N{NIGHT WITH STARS}'
            await self.game_channel.send(f'{emoji} **{"Day" if self.daytime else "Night"} {self.day}** {emoji}')
            self.log.info('>> Time progression. Day %d, %s.', self.day, 'day' if self.daytime else 'night')

            # if we are on D1, send some directions and just move onto N1.
            if self.daytime and self.day == 1:
                self.log.info('Tutorial section, this will take a bit.')
                await self.game_channel.send(
                    '**Welcome to the game!**\n\n'
                    f'There are {len(self.mafia)} mafia hiding within a town of innocents. '
                    'If you are an innocent, your goal is to lynch the mafia. '
                    'If you are a mafia, your goal is to work with your partner to wipe out the innocents before '
                    'they find out about you!'
                )
                await asyncio.sleep(3 if self.DEBUG else 15)
                self.daytime = False
                continue

            if self.daytime:
                if self.victim_tonight:
                    self.players.remove(self.victim_tonight)
                    await self.game_channel.set_permissions(self.victim_tonight, read_messages=True, send_messages=False)
                    await self.game_channel.send(
                        f'**{self.victim_tonight}** was unfortunately found dead in their home last night.'
                    )
                    await asyncio.sleep(3.0)
                    await self.game_channel.send('They were **innocent.**')
                    await asyncio.sleep(5.0)
                    self.victim_tonight = None

                    if all(player in self.mafia for player in self.players):
                        await self.game_channel.send('\U0001f52a **Mafia win!** \U0001f52a')
                        await self.game_over(mafia_won=True)
                        break

                votes_required = round(len(self.players) / 3)
                self.log.info('It is now discussion time. (%d votes required for hanging.)', votes_required)
                await self.game_channel.send(
                    'Discussion time! Alive town members can now vote who to hang. To vote, type `!vote <username>` in '
                    f'chat. You have 30 seconds, and {votes_required} vote(s) are required to hang someone.'

                    '\n\n**Alive Players:**\n' +
                    '\n'.join(f'- {user.name}' for user in self.players)
                )

                # gather hanging votes.
                task = self.bot.loop.create_task(self.gather_hanging_votes())
                await asyncio.sleep(20.0)
                await self.game_channel.send('**10 seconds of voting remaining!**')
                await asyncio.sleep(5.0)
                await self.game_channel.send('**5 seconds of voting remaining!**')
                await asyncio.sleep(5.0)
                task.cancel()

                self.hanging_votes = {target_id: len(votes) for target_id, votes in self.hanging_votes.items()}
                self.log.info('Hanging votes (postprocessed): %s', self.hanging_votes)
                sorted_votes = sorted(list(self.hanging_votes.items()), key=lambda e: e[1], reverse=True)
                vote_board = list(filter(
                    lambda e: e[1] >= votes_required, sorted_votes
                ))
                self.log.info('Final voting board: %s', vote_board)
                if not vote_board:
                    await self.game_channel.send('A verdict was not reached in time. Oh well!')
                else:
                    hanged = discord.utils.get(self.players, id=vote_board[0][0])
                    await self.lock()
                    await self.game_channel.set_permissions(hanged, read_messages=True, send_messages=True)
                    await self.game_channel.send(
                        f'\N{SKULL} {hanged.mention}, you have been voted to be hanged. Do you have any last words '
                        'before your death? You have 15 seconds.'
                    )
                    await asyncio.sleep(3.0 if self.DEBUG else 15.0)
                    await self.game_channel.set_permissions(hanged, read_messages=True, send_messages=False)
                    await self.unlock()
                    await self.game_channel.send(f'\N{SKULL} **Rest in peace, {hanged}. You will be missed.** \N{SKULL}')
                    self.players.remove(hanged)
                    await asyncio.sleep(3)
                    was_mafia = hanged in self.mafia
                    if was_mafia:
                        self.mafia.remove(hanged)
                        await self.mafia_chat.set_permissions(hanged, read_messages=True, send_messages=False)
                    await self.game_channel.send(f'{hanged} was **{"mafia" if was_mafia else "innocent"}.**')
                    await asyncio.sleep(5)

                # reset
                self.hanging_votes = defaultdict(list)
            else:
                await self.game_channel.send(
                    "Night time! Sleep tight, and don't let the bed bugs bite!"
                )
                await self.lock()
                alive_mafia = ', '.join(mafia.mention for mafia in self.mafia)
                await self.mafia_chat.send(
                    f"{alive_mafia}: It's time to kill! \N{HOCHO} Discuss someone to kill, then type "
                    "`!kill <username>` in chat when you have decided on someone to stab. You have 30 seconds! "
                    "Alternatively, you can do nothing to stay low. "
                    "Once you choose someone to kill, you can't go back to killing nobody!\n\n" +
                    '\n'.join(f'- {player.name}' for player in self.players if player not in self.mafia)
                )
                task = self.bot.loop.create_task(self.pick_victim())
                await asyncio.sleep(2.0 if self.DEBUG else 30.0)
                task.cancel()
                await self.unlock()

            if len(self.mafia) == 0:
                await self.game_channel.send('\U0001f64f **Innocents win!** \U0001f64f')
                await self.game_over(mafia_won=False)
                break
            elif all(player in self.mafia for player in self.players):
                await self.game_channel.send('\U0001f52a **Mafia win!** \U0001f52a')
                await self.game_over(mafia_won=True)
                break

            if not self.daytime:
                # it's night, so move onto next
                self.day += 1
                self.daytime = True
            else:
                # it's day, so move onto night
                self.daytime = False

    async def start(self):
        self.log.info('Game started. Now gathering players (minimum of %d).', self.REQUIRED_PLAYERS)
        await self.gather_players()
        await self.pick_mafia()
        await self.notify_roles()
        await asyncio.sleep(5.0)
        self.state = MafiaGameState.STARTED
        await self.game_loop()

        await self.game_channel.send('Did you have fun? This channel will self destruct in **10 seconds.**')
        await asyncio.sleep(10.0)

        await self.mafia_chat.delete()
        await self.game_channel.delete()
        await self.channel.send('Game over!')

class Mafia(Cog):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.sessions = set()

    @command()
    @guild_only()
    async def mafia(self, ctx: Context):
        if ctx.channel.id in self.sessions:
            await ctx.send('Game already started here.')
            return
        self.sessions.add(ctx.channel.id)
        game = MafiaGame(ctx.bot, master=ctx.author, channel=ctx.channel)
        await game.start()
        self.sessions.remove(ctx.channel.id)

def setup(bot):
    bot.add_cog(Mafia(bot))
<commit_msg>remove mafia :x<commit_after><|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import requests

reload(sys)
sys.setdefaultencoding('utf-8')

def abstract(text, start, end):
    if text is None or text == '':
        return ''
    res = ''
    if start is not None and start != '':
        if start not in text:
            return res
        else:
            text = text[text.index(start) + len(start):]
    if end is not None and end != '':
        if end not in text:
            return res
        else:
            res = text[0:text.index(end)]
    else:
        res = text
    return res

dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.resourceTimeout"] = 10
dcap["phantomjs.page.settings.loadImages"] = True
dcap[
    "phantomjs.page.settings.userAgent"] = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'
web = webdriver.PhantomJS(executable_path='C:/Python27/phantomjs.exe'
                          , desired_capabilities=dcap)
#
# web.get("https://passport.jd.com/new/login.aspx?ReturnUrl=http%3A%2F%2Fhome.jd.com%2F")
# time.sleep(10)
# cookies = web.get_cookies()
# cookie_dict = dict()
# for c in cookies:
#     cookie_dict[c['name']] = c['value']
# web.save_screenshot('qrcode.png')

headers = dict()
headers[
    "User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"
headers["Accept"] = "*/*"
headers["Accept-Encoding"] = "gzip, deflate"
headers["Accept-Language"] = "zh-CN,en,*"
headers["Referer"] = "https://passport.jd.com/new/login.aspx?ReturnUrl=http%3A%2F%2Fhome.jd.com%2F"
session = requests.Session()

response = session.get("https://qr.m.jd.com/show?appid=133&size=147&t=1486614526653")
with open("qrcode.png", 'wb') as fs:
    fs.write(response.content)

response = session.get("https://qr.m.jd.com/check?callback=jQuery6172296&appid=133&_=1486609849337",
                       headers=headers)

ticket = abstract(response.content, '\"ticket\" : \"', '\"')
print ticket

headers['X-Requested-With'] = 'XMLHttpRequest'
response = session.get("https://passport.jd.com/uc/qrCodeTicketValidation?t=" + ticket, headers=headers)

cookie_dict = session.cookies.get_dict()

cookie_list = [{'name': c[0], 'value': c[1], 'path': '/', 'domain': '.jd.com'} for c in cookie_dict.items()]

for c in cookie_list:
    web.add_cookie(c)

web.get('https://home.jd.com')

web.save_screenshot('test2.png')
print response.headers
print response.content
<commit_msg>sasila<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import requests

reload(sys)
sys.setdefaultencoding('utf-8')

def abstract(text, start, end):
    if text is None or text == '':
        return ''
    res = ''
    if start is not None and start != '':
        if start not in text:
            return res
        else:
            text = text[text.index(start) + len(start):]
    if end is not None and end != '':
        if end not in text:
            return res
        else:
            res = text[0:text.index(end)]
    else:
        res = text
    return res

dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.resourceTimeout"] = 10
dcap["phantomjs.page.settings.loadImages"] = True
dcap[
    "phantomjs.page.settings.userAgent"] = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'
web = webdriver.PhantomJS(executable_path='C:/Python27/phantomjs.exe'
                          , desired_capabilities=dcap)
#
# web.get("https://passport.jd.com/new/login.aspx?ReturnUrl=http%3A%2F%2Fhome.jd.com%2F")
# time.sleep(10)
# cookies = web.get_cookies()
# cookie_dict = dict()
# for c in cookies:
#     cookie_dict[c['name']] = c['value']
# web.save_screenshot('qrcode.png')

headers = dict()
headers[
    "User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"
headers["Accept"] = "*/*"
headers["Accept-Encoding"] = "gzip, deflate"
headers["Accept-Language"] = "zh-CN,en,*"
headers["Referer"] = "https://passport.jd.com/new/login.aspx?ReturnUrl=http%3A%2F%2Fhome.jd.com%2F"
session = requests.Session()

response = session.get("https://qr.m.jd.com/show?appid=133&size=147&t=1486614526653")
with open("qrcode.png", 'wb') as fs:
    fs.write(response.content)

response = session.get("https://qr.m.jd.com/check?callback=jQuery6172296&appid=133&_=1486609849337",
                       headers=headers)

ticket = abstract(response.content, '\"ticket\" : \"', '\"')
print ticket

headers['X-Requested-With'] = 'XMLHttpRequest'
response = session.get("https://passport.jd.com/uc/qrCodeTicketValidation?t=" + ticket, headers=headers)

cookie_dict = session.cookies.get_dict()

cookie_list = [{'name': c[0], 'value': c[1], 'path': '/', 'domain': '.jd.com'} for c in cookie_dict.items()]

for c in cookie_list:
    web.add_cookie(c)

web.get('https://home.jd.com')

web.save_screenshot('test3.png')
print response.headers
print response.content
<|endoftext|>


<commit_before>####################################################################
# Second step in the order process - capture the billing method and shipping type
#####################################################################

from django import http
from django.conf import settings
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.utils.translation import ugettext_lazy as _
from satchmo.contact.models import Contact
from satchmo.contact.models import Order, OrderPayment
from satchmo.payment.common.forms import CreditPayShipForm, SimplePayShipForm
from satchmo.payment.common.pay_ship import pay_ship_save
from satchmo.payment.config import payment_live
from satchmo.payment.models import CreditCardDetail
from satchmo.shop.utils.dynamic import lookup_url, lookup_template
from satchmo.shop.models import Cart

selection = _("Please Select")

def pay_ship_info_verify(request, payment_module):
    """Verify customer and cart.
    Returns:
    True, contact, cart on success
    False, destination of failure
    """
    #First verify that the customer exists
    contact = Contact.from_request(request, create=False)
    if contact is None:
        url = lookup_url(payment_module, 'satchmo_checkout-step1')
        return (False, http.HttpResponseRedirect(url))

    #Verify we still have items in the cart
    if request.session.get('cart', False):
        tempCart = Cart.objects.get(id=request.session['cart'])
        if tempCart.numItems == 0:
            template = lookup_template(payment_module, 'checkout/empty_cart.html')
            return (False, render_to_response(template, RequestContext(request)))
    else:
        return (False, render_to_response('checkout/empty_cart.html', RequestContext(request)))

    return (True, contact, tempCart)

def credit_pay_ship_process_form(request, contact, working_cart, payment_module):
    """Handle the form information.
    Returns:
        (True, destination) on success
        (False, form) on failure
    """
    if request.POST:
        new_data = request.POST.copy()
        form = CreditPayShipForm(request, payment_module, new_data)
        if form.is_valid():
            data = form.cleaned_data

            # Create a new order
            newOrder = Order(contact=contact)
            pay_ship_save(newOrder, working_cart, contact,
                shipping=data['shipping'], discount=data['discount'])
            request.session['orderID'] = newOrder.id

            #TODO: allow partial-pay here, which will mean that not all payments are on new orders.
            orderpayment = OrderPayment(order=newOrder, amount=newOrder.balance,
                payment=unicode(payment_module.KEY.value))
            orderpayment.save()
            # Save the credit card information
            cc = CreditCardDetail(orderpayment=orderpayment, ccv=data['ccv'],
                expireMonth=data['month_expires'],
                expireYear=data['year_expires'],
                creditType=data['credit_type'])
            cc.storeCC(data['credit_number'])
            cc.save()

            url = lookup_url(payment_module, 'satchmo_checkout-step3')
            return (True, http.HttpResponseRedirect(url))
    else:
        form = CreditPayShipForm(request, payment_module)

    return (False, form)

def simple_pay_ship_process_form(request, contact, working_cart, payment_module, create_payment=True):
    if request.POST:
        new_data = request.POST.copy()
        form = SimplePayShipForm(request, payment_module, new_data)
        if form.is_valid():
            data = form.cleaned_data

            # Create a new order
            newOrder = Order(contact=contact)
            pay_ship_save(newOrder, working_cart, contact,
                shipping=data['shipping'], discount=data['discount'])
            request.session['orderID'] = newOrder.id

            if create_payment:
                orderpayment = OrderPayment(order=newOrder, amount=newOrder.balance, payment=payment_module.KEY.value)
                orderpayment.save()

            url = lookup_url(payment_module, 'satchmo_checkout-step3')
            return (True, http.HttpResponseRedirect(url))
    else:
        form = SimplePayShipForm(request, payment_module)

    return (False, form)

def pay_ship_render_form(request, form, template, payment_module):
    template = lookup_template(payment_module, template)
    ctx = RequestContext(request, {
        'form' : form,
        'PAYMENT_LIVE' : payment_live(payment_module)
    })
    return render_to_response(template, ctx)

def base_pay_ship_info(request, payment_module, form_handler, template):
    results = pay_ship_info_verify(request, payment_module)
    if not results[0]:
        return results[1]

    contact = results[1]
    working_cart = results[2]

    results = form_handler(request, contact, working_cart, payment_module)
    if results[0]:
        return results[1]

    form = results[1]
    return pay_ship_render_form(request, form, template, payment_module)

def credit_pay_ship_info(request, payment_module, template='checkout/pay_ship.html'):
    """A pay_ship view which uses a credit card"""
    return base_pay_ship_info(request, payment_module, credit_pay_ship_process_form, template)

def simple_pay_ship_info(request, payment_module, template):
    """A pay_ship view which doesn't require a credit card"""
    return base_pay_ship_info(request, payment_module, simple_pay_ship_process_form, template)

# def credit_pay_ship_info(request, payment_module):
#     #First verify that the customer exists
#     contact = Contact.from_request(request, create=False)
#     if contact is None:
#         url = lookup_url(payment_module, 'satchmo_checkout-step1')
#         return http.HttpResponseRedirect(url)
#
#     #Verify we still have items in the cart
#     if request.session.get('cart', False):
#         tempCart = Cart.objects.get(id=request.session['cart'])
#         if tempCart.numItems == 0:
#             template = lookup_template(payment_module, 'checkout/empty_cart.html')
#             return render_to_response(template, RequestContext(request))
#     else:
#         return render_to_response('checkout/empty_cart.html', RequestContext(request))
#     #Verify order info is here
#     if request.POST:
#         new_data = request.POST.copy()
#         form = CreditPayShipForm(request, payment_module, new_data)
#         if form.is_valid():
#             data = form.cleaned_data
#
#             # Create a new order
#             newOrder = Order(contact=contact)
#             pay_ship_save(newOrder, tempCart, contact,
#                 shipping=data['shipping'], discount=data['discount'])
#             request.session['orderID'] = newOrder.id
#
#             #TODO: allow partial-pay here, which will mean that not all payments are on new orders.
#             orderpayment = OrderPayment(order=newOrder, amount=newOrder.balance,
#                 payment=unicode(payment_module.KEY.value))
#             orderpayment.save()
#             # Save the credit card information
#             cc = CreditCardDetail(orderpayment=orderpayment, ccv=data['ccv'],
#                 expireMonth=data['month_expires'],
#                 expireYear=data['year_expires'],
#                 creditType=data['credit_type'])
#             cc.storeCC(data['credit_number'])
#             cc.save()
#
#             url = lookup_url(payment_module, 'satchmo_checkout-step3')
#             return http.HttpResponseRedirect(url)
#     else:
#         form = CreditPayShipForm(request, payment_module)
#
#     template = lookup_template(payment_module, 'checkout/pay_ship.html')
#     ctx = {
#         'form' : form,
#         'PAYMENT_LIVE' : payment_live(payment_module)
#     }
#     return render_to_response(template, ctx, RequestContext(request))
#
# def simple_pay_ship_info(request, payment_module, template):
#     """A pay_ship view which doesn't require a credit card"""
#     #First verify that the customer exists
#     contact = Contact.from_request(request, create=False)
#     if contact is None:
#         url = lookup_url(payment_module, 'satchmo_checkout-step1')
#         return http.HttpResponseRedirect(url)
#     #Verify we still have items in the cart
#     if request.session.get('cart', False):
#         tempCart = Cart.objects.get(id=request.session['cart'])
#         if tempCart.numItems == 0:
#             template = lookup_template(payment_module, 'checkout/empty_cart.html')
#             return render_to_response(template, RequestContext(request))
#     else:
#         template = lookup_template(payment_module, 'checkout/empty_cart.html')
#         return render_to_response(template, RequestContext(request))
#
#     #Verify order info is here
#     if request.POST:
#         new_data = request.POST.copy()
#         form = SimplePayShipForm(request, payment_module, new_data)
#         if form.is_valid():
#             data = form.cleaned_data
#
#             # Create a new order
#             newOrder = Order(contact=contact)
#             pay_ship_save(newOrder, tempCart, contact,
#                 shipping=data['shipping'], discount=data['discount'])
#             request.session['orderID'] = newOrder.id
#
#             #TODO: allow partial-pay here, which will mean that not all payments are on new orders.
#             orderpayment = OrderPayment(order=newOrder, amount=newOrder.balance, payment=payment_module.KEY.value)
#             orderpayment.save()
#
#             url = lookup_url(payment_module, 'satchmo_checkout-step3')
#             return http.HttpResponseRedirect(url)
#     else:
#         form = SimplePayShipForm(request, payment_module)
#
#     template = lookup_template(payment_module, template)
#     ctx = {
#         'form' : form,
#         'PAYMENT_LIVE' : payment_live(payment_module)
#     }
#     return render_to_response(template, ctx, RequestContext(request))
<commit_msg>Clean up the common payship views. Remove a lot of commented code.<commit_after>####################################################################
# Second step in the order process: Capture the billing method and shipping type
#####################################################################

from django import http
from django.conf import settings
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.utils.translation import ugettext_lazy as _

from satchmo.contact.models import Contact
from satchmo.contact.models import Order, OrderPayment
from satchmo.payment.common.forms import CreditPayShipForm, SimplePayShipForm
from satchmo.payment.common.pay_ship import pay_ship_save
from satchmo.payment.config import payment_live
from satchmo.payment.models import CreditCardDetail
from satchmo.shop.utils.dynamic import lookup_url, lookup_template
from satchmo.shop.models import Cart

selection = _("Please Select")

def pay_ship_info_verify(request, payment_module):
    """Verify customer and cart.
    Returns:
    True, contact, cart on success
    False, destination of failure
    """
    # Verify that the customer exists.
    contact = Contact.from_request(request, create=False)
    if contact is None:
        url = lookup_url(payment_module, 'satchmo_checkout-step1')
        return (False, http.HttpResponseRedirect(url))

    # Verify that we still have items in the cart.
    if request.session.get('cart', False):
        tempCart = Cart.objects.get(id=request.session['cart'])
        if tempCart.numItems == 0:
            template = lookup_template(payment_module, 'checkout/empty_cart.html')
            return (False, render_to_response(template, RequestContext(request)))
    else:
        return (False, render_to_response('checkout/empty_cart.html', RequestContext(request)))

    return (True, contact, tempCart)

def credit_pay_ship_process_form(request, contact, working_cart, payment_module):
    """Handle the form information.
    Returns:
        (True, destination) on success
        (False, form) on failure
    """
    if request.POST:
        new_data = request.POST.copy()
        form = CreditPayShipForm(request, payment_module, new_data)
        if form.is_valid():
            data = form.cleaned_data

            # Create a new order.
            newOrder = Order(contact=contact)
            pay_ship_save(newOrder, working_cart, contact,
                shipping=data['shipping'], discount=data['discount'])
            request.session['orderID'] = newOrder.id

            orderpayment = OrderPayment(order=newOrder, amount=newOrder.balance,
                payment=unicode(payment_module.KEY.value))
            orderpayment.save()
            # Save the credit card information.
            cc = CreditCardDetail(orderpayment=orderpayment, ccv=data['ccv'],
                expireMonth=data['month_expires'],
                expireYear=data['year_expires'],
                creditType=data['credit_type'])
            cc.storeCC(data['credit_number'])
            cc.save()

            url = lookup_url(payment_module, 'satchmo_checkout-step3')
            return (True, http.HttpResponseRedirect(url))
    else:
        form = CreditPayShipForm(request, payment_module)

    return (False, form)

def simple_pay_ship_process_form(request, contact, working_cart, payment_module, create_payment=True):
    if request.POST:
        new_data = request.POST.copy()
        form = SimplePayShipForm(request, payment_module, new_data)
        if form.is_valid():
            data = form.cleaned_data

            # Create a new order.
            newOrder = Order(contact=contact)
            pay_ship_save(newOrder, working_cart, contact,
                shipping=data['shipping'], discount=data['discount'])
            request.session['orderID'] = newOrder.id

            if create_payment:
                orderpayment = OrderPayment(order=newOrder, amount=newOrder.balance, payment=payment_module.KEY.value)
                orderpayment.save()

            url = lookup_url(payment_module, 'satchmo_checkout-step3')
            return (True, http.HttpResponseRedirect(url))
    else:
        form = SimplePayShipForm(request, payment_module)

    return (False, form)

def pay_ship_render_form(request, form, template, payment_module):
    template = lookup_template(payment_module, template)
    ctx = RequestContext(request, {
        'form': form,
        'PAYMENT_LIVE': payment_live(payment_module)})
    return render_to_response(template, ctx)

def base_pay_ship_info(request, payment_module, form_handler, template):
    results = pay_ship_info_verify(request, payment_module)
    if not results[0]:
        return results[1]

    contact = results[1]
    working_cart = results[2]

    results = form_handler(request, contact, working_cart, payment_module)
    if results[0]:
        return results[1]

    form = results[1]
    return pay_ship_render_form(request, form, template, payment_module)

def credit_pay_ship_info(request, payment_module, template='checkout/pay_ship.html'):
    """A pay_ship view which uses a credit card."""
    return base_pay_ship_info(request, payment_module, credit_pay_ship_process_form, template)

def simple_pay_ship_info(request, payment_module, template):
    """A pay_ship view which doesn't require a credit card."""
    return base_pay_ship_info(request, payment_module, simple_pay_ship_process_form, template)
<|endoftext|>


<commit_before># vim:ts=4:sts=4:sw=4:expandtab

from satori.core.models import PrivilegeTimes

PageInfo = Struct('PageInfo', [
    ('contest', DjangoStruct('Contest'), False),
    ('contestant', DjangoStruct('Contestant'), False),
    ('role', DjangoStruct('Role'), False),
    ('user', DjangoStruct('User'), False),
    ('subpages', DjangoStructList('Subpage'), False),
    ('rankings', DjangoStructList('Ranking'), False),
    ('is_admin', bool, False),
    ('is_problemsetter', bool, False),
    ('contest_is_admin', bool, False),
    ('contest_can_ask_questions', bool, False),
    ('contest_answers_exist', bool, False),
    ('contest_submittable_problems_exist', bool, False),
    ('contest_viewable_problems_exist', bool, False),
    ])

ContestInfo = Struct('ContestInfo', [
    ('contest', DjangoStruct('Contest'), False),
    ('contestant', DjangoStruct('Contestant'), False),
    ('can_apply', bool, False),
    ('can_join', bool, False),
    ('is_admin', bool, False),
    ])

SizedContestantList = Struct('SizedContestantList', [
    ('contestants', TypedList(DjangoStruct('Contestant')), False),
    ('count', int, False),
    ])

SubpageInfo = Struct('SubpageInfo', [
    ('subpage', DjangoStruct('Subpage'), False),
    ('html', unicode, False),
    ('is_admin', bool, False),
    ])

ProblemMappingInfo = Struct('ProblemMappingInfo', [
    ('problem_mapping', DjangoStruct('ProblemMapping'), False),
    ('can_submit', bool, False),
    ('is_admin', bool, False),
    ('has_pdf', bool, False),
    ('html', unicode, False),
    ('contestant_role_view_times', PrivilegeTimes, False),
    ('contestant_role_submit_times', PrivilegeTimes, False),
    ])

ResultInfo = Struct('ResultInfo', [
    ('submit', DjangoStruct('Submit'), False),
    ('contestant', DjangoStruct('Contestant'), False),
    ('problem_mapping', DjangoStruct('ProblemMapping'), False),
    ('status', unicode, False),
    ('report', unicode, False),
    ('data', unicode, False),
    ('data_filename', unicode, False),
    ('test_results', TypedMap(DjangoStruct('TestResult'), TypedMap(unicode, AnonymousAttribute)), False),
    ('test_suite_results', TypedMap(DjangoStruct('TestSuiteResult'), TypedMap(unicode, AnonymousAttribute)), False),
    ])

SizedResultList = Struct('SizedResultList', [
    ('results', TypedList(ResultInfo), False),
    ('count', int, False),
    ])

@ExportClass
class Web(object):

    @ExportMethod(PageInfo, [DjangoId('Contest')], PCPermit())
    @staticmethod
    def get_page_info(contest=None):
        ret = PageInfo()
        ret.role = Security.whoami()
        ret.user = Security.whoami_user()
        ret.is_admin = Privilege.global_demand('ADMIN')
        ret.is_problemsetter = Privilege.global_demand('MANAGE_PROBLEMS')
        if contest:
            ret.contest = contest
            if ret.role:
                ret.contestant = contest.find_contestant(ret.role)
            else:
                ret.contestant = None
            ret.subpages = Subpage.get_for_contest(contest, False)
            ret.rankings = contest.rankings
            ret.contest_is_admin = Privilege.demand(contest, 'MANAGE')
            ret.contest_can_ask_questions = Privilege.demand(contest, 'ASK_QUESTIONS')
            ret.contest_answers_exist = bool(Privilege.where_can(contest.questions.all(), 'VIEW'))
            ret.contest_submittable_problems_exist = bool(Privilege.where_can(contest.problem_mappings.all(), 'SUBMIT'))
            ret.contest_viewable_problems_exist = bool(Privilege.where_can(contest.problem_mappings.all(), 'VIEW'))
        else:
            ret.subpages = Subpage.get_global(False)
        return ret

    @ExportMethod(TypedList(ContestInfo), [], PCPermit())
    @staticmethod
    def get_contest_list():
        ret = []
        whoami = Security.whoami()
        for contest in Privilege.where_can(Contest.objects.all(), 'VIEW'):
            ret_c = ContestInfo()
            ret_c.contest = contest
            if whoami:
                ret_c.contestant = contest.find_contestant(whoami)
            else:
                ret_c.contestant = None
            ret_c.can_apply = Privilege.demand(contest, 'APPLY')
            ret_c.can_join = Privilege.demand(contest, 'JOIN')
            ret_c.is_admin = Privilege.demand(contest, 'MANAGE')
            ret.append(ret_c)
        return ret

    @ExportMethod(ProblemMappingInfo, [DjangoId('ProblemMapping')], PCPermit())
    @staticmethod
    def get_problem_mapping_info(problem):
        ret_p = ProblemMappingInfo()
        ret_p.problem_mapping = problem
        ret_p.has_pdf = problem.statement_files_get('_pdf') is not None
        reader = problem.statement_files_get_blob('_html')
        if reader:
            ret_p.html = reader.read()
            reader.close()
        ret_p.can_submit = Privilege.demand(problem, 'SUBMIT')
        ret_p.is_admin = Privilege.demand(problem, 'MANAGE')
        if ret_p.is_admin:
            ret_p.contestant_role_view_times = Privilege.get(problem.contest.contestant_role, problem, 'VIEW')
            ret_p.contestant_role_submit_times = Privilege.get(problem.contest.contestant_role, problem, 'SUBMIT')
        return ret_p;

    @ExportMethod(TypedList(ProblemMappingInfo), [DjangoId('Contest')], PCPermit())
    @staticmethod
    def get_problem_mapping_list(contest):
        ret = []
        for problem in Privilege.where_can(contest.problem_mappings.all(), 'VIEW'):
            ret.append(Web.get_problem_mapping_info(problem))
        return ret

    @ExportMethod(SubpageInfo, [DjangoId('Subpage')], PCPermit())
    @staticmethod
    def get_subpage_info(subpage):
        ret_s = SubpageInfo()
        ret_s.subpage = subpage
        reader = subpage.content_files_get_blob('_html')
        if reader:
            ret_s.html = reader.read()
            reader.close()
        ret_s.is_admin = Privilege.demand(subpage, 'MANAGE')
        return ret_s

    @ExportMethod(TypedList(SubpageInfo), [bool], PCPermit())
    @staticmethod
    def get_subpage_list_global(announcements):
        ret = []
        for subpage in Privilege.where_can(Subpage.get_global(announcements), 'VIEW'):
            ret.append(Web.get_subpage_info(subpage))
        return ret

    @ExportMethod(TypedList(SubpageInfo), [DjangoId('Contest'), bool], PCPermit())
    @staticmethod
    def get_subpage_list_for_contest(contest, announcements):
        ret = []
        for subpage in Privilege.where_can(Subpage.get_for_contest(contest, announcements), 'VIEW'):
            ret.append(Web.get_subpage_info(subpage))
        return ret

    @ExportMethod(SizedContestantList, [DjangoId('Contest'), int, int], PCArg('contest', 'VIEW'))
    @staticmethod
    def get_accepted_contestants(contest, limit=20, offset=0):
        result = Contestant.objects.filter(contest=contest, accepted=True).exclude(parents=contest.admin_role)
        return SizedContestantList(count=len(result), contestants=result[offset:offset+limit])

    @ExportMethod(SizedContestantList, [DjangoId('Contest'), int, int], PCArg('contest', 'MANAGE'))
    @staticmethod
    def get_pending_contestants(contest, limit=20, offset=0):
        result = Contestant.objects.filter(contest=contest, accepted=False).exclude(parents=contest.admin_role)
        return SizedContestantList(count=len(result), contestants=result[offset:offset+limit])

    @ExportMethod(SizedContestantList, [DjangoId('Contest'), int, int], PCArg('contest', 'MANAGE'))
    @staticmethod
    def get_contest_admins(contest, limit=20, offset=0):
        result = Contestant.objects.filter(parents=contest.admin_role)
        return SizedContestantList(count=len(result), contestants=result[offset:offset+limit])

    @ExportMethod(SizedResultList, [DjangoId('Contest'), DjangoId('Contestant'), DjangoId('ProblemMapping'), int, int], PCPermit())
    def get_results(contest, contestant=None, problem=None, limit=20, offset=0):
        if contestant:
            contestant_list = Contestant.objects.filter(id=contestant.id)
        else:
            contestant_list = Contestant.objects.filter(contest=contest)
        contestant_list = list(Privilege.where_can(contestant_list, 'OBSERVE'))
        q = Submit.objects.filter(contestant__in=contestant_list)
#        if contestant:
#            q = Submit.objects.filter(contestant=contestant)
#        else:
#            q = Submit.objects.filter(contestant__contest=contest)
        if problem:
            q = q.filter(problem=problem)
#        q = Privilege.where_can(q, 'OBSERVE')
        ret = []
        for submit in q.order_by('-id')[offset:offset+limit]:
            ret_r = ResultInfo()
            ret_r.submit = submit
            ret_r.contestant = submit.contestant
            ret_r.problem_mapping = submit.problem
            ret_r.status = submit.get_test_suite_status()
            ret_r.report = submit.get_test_suite_report()
            ret.append(ret_r)
        return SizedResultList(
            count=len(q),
            results=ret
            )

    @ExportMethod(ResultInfo, [DjangoId('Submit')], PCArg('submit', 'OBSERVE'))
    def get_result_details(submit):
        ret = ResultInfo()
        ret.submit = submit
        ret.contestant = submit.contestant
        ret.problem_mapping = submit.problem
        ret.status = submit.get_test_suite_status()
        ret.report = submit.get_test_suite_report()
        reader = submit.data_get_blob('content')
        data = reader.read(min(100000, reader.length))
        ret.data_filename = reader.filename
        reader.close()
        try:
            ret.data = unicode(data, 'utf8')
        except:
            ret.data = None
        if Privilege.demand(submit, 'MANAGE'):
            ret_tsr = {}
            for tsr in TestSuiteResult.objects.filter(submit=submit):
                ret_tsr[tsr] = tsr.oa_get_map()
            ret_ts = {}
            for ts in TestResult.objects.filter(submit=submit):
                ret_ts[ts] = ts.oa_get_map()
            ret.test_results = ret_ts
            ret.test_suite_results = ret_tsr
        return ret

<commit_msg>Enhanced Web.get_result_details().<commit_after># vim:ts=4:sts=4:sw=4:expandtab

from satori.core.models import PrivilegeTimes

PageInfo = Struct('PageInfo', [
    ('contest', DjangoStruct('Contest'), False),
    ('contestant', DjangoStruct('Contestant'), False),
    ('role', DjangoStruct('Role'), False),
    ('user', DjangoStruct('User'), False),
    ('subpages', DjangoStructList('Subpage'), False),
    ('rankings', DjangoStructList('Ranking'), False),
    ('is_admin', bool, False),
    ('is_problemsetter', bool, False),
    ('contest_is_admin', bool, False),
    ('contest_can_ask_questions', bool, False),
    ('contest_answers_exist', bool, False),
    ('contest_submittable_problems_exist', bool, False),
    ('contest_viewable_problems_exist', bool, False),
    ])

ContestInfo = Struct('ContestInfo', [
    ('contest', DjangoStruct('Contest'), False),
    ('contestant', DjangoStruct('Contestant'), False),
    ('can_apply', bool, False),
    ('can_join', bool, False),
    ('is_admin', bool, False),
    ])

SizedContestantList = Struct('SizedContestantList', [
    ('contestants', TypedList(DjangoStruct('Contestant')), False),
    ('count', int, False),
    ])

SubpageInfo = Struct('SubpageInfo', [
    ('subpage', DjangoStruct('Subpage'), False),
    ('html', unicode, False),
    ('is_admin', bool, False),
    ])

ProblemMappingInfo = Struct('ProblemMappingInfo', [
    ('problem_mapping', DjangoStruct('ProblemMapping'), False),
    ('can_submit', bool, False),
    ('is_admin', bool, False),
    ('has_pdf', bool, False),
    ('html', unicode, False),
    ('contestant_role_view_times', PrivilegeTimes, False),
    ('contestant_role_submit_times', PrivilegeTimes, False),
    ])

TestResultInfo = Struct('TestResultInfo', [
    ('test', DjangoStruct('Test'), False),
    ('test_result', DjangoStruct('TestResult'), False),
    ('attributes', TypedMap(unicode, AnonymousAttribute)),
    ]

TestSuiteResultInfo = Struct('TestResultInfo', [
    ('test_suite', DjangoStruct('TestSuite'), False),
    ('test_suite_result', DjangoStruct('TestSuiteResult'), False),
    ('attributes', TypedMap(unicode, AnonymousAttribute)),
    ]

ResultInfo = Struct('ResultInfo', [
    ('submit', DjangoStruct('Submit'), False),
    ('contestant', DjangoStruct('Contestant'), False),
    ('problem_mapping', DjangoStruct('ProblemMapping'), False),
    ('status', unicode, False),
    ('report', unicode, False),
    ('data', unicode, False),
    ('data_filename', unicode, False),
    ('test_results', TypedList(TestResultInfo), False),
    ('test_suite_results', TypedList(TestSuiteResultInfo), False),
    ])

SizedResultList = Struct('SizedResultList', [
    ('results', TypedList(ResultInfo), False),
    ('count', int, False),
    ])

@ExportClass
class Web(object):

    @ExportMethod(PageInfo, [DjangoId('Contest')], PCPermit())
    @staticmethod
    def get_page_info(contest=None):
        ret = PageInfo()
        ret.role = Security.whoami()
        ret.user = Security.whoami_user()
        ret.is_admin = Privilege.global_demand('ADMIN')
        ret.is_problemsetter = Privilege.global_demand('MANAGE_PROBLEMS')
        if contest:
            ret.contest = contest
            if ret.role:
                ret.contestant = contest.find_contestant(ret.role)
            else:
                ret.contestant = None
            ret.subpages = Subpage.get_for_contest(contest, False)
            ret.rankings = contest.rankings
            ret.contest_is_admin = Privilege.demand(contest, 'MANAGE')
            ret.contest_can_ask_questions = Privilege.demand(contest, 'ASK_QUESTIONS')
            ret.contest_answers_exist = bool(Privilege.where_can(contest.questions.all(), 'VIEW'))
            ret.contest_submittable_problems_exist = bool(Privilege.where_can(contest.problem_mappings.all(), 'SUBMIT'))
            ret.contest_viewable_problems_exist = bool(Privilege.where_can(contest.problem_mappings.all(), 'VIEW'))
        else:
            ret.subpages = Subpage.get_global(False)
        return ret

    @ExportMethod(TypedList(ContestInfo), [], PCPermit())
    @staticmethod
    def get_contest_list():
        ret = []
        whoami = Security.whoami()
        for contest in Privilege.where_can(Contest.objects.all(), 'VIEW'):
            ret_c = ContestInfo()
            ret_c.contest = contest
            if whoami:
                ret_c.contestant = contest.find_contestant(whoami)
            else:
                ret_c.contestant = None
            ret_c.can_apply = Privilege.demand(contest, 'APPLY')
            ret_c.can_join = Privilege.demand(contest, 'JOIN')
            ret_c.is_admin = Privilege.demand(contest, 'MANAGE')
            ret.append(ret_c)
        return ret

    @ExportMethod(ProblemMappingInfo, [DjangoId('ProblemMapping')], PCPermit())
    @staticmethod
    def get_problem_mapping_info(problem):
        ret_p = ProblemMappingInfo()
        ret_p.problem_mapping = problem
        ret_p.has_pdf = problem.statement_files_get('_pdf') is not None
        reader = problem.statement_files_get_blob('_html')
        if reader:
            ret_p.html = reader.read()
            reader.close()
        ret_p.can_submit = Privilege.demand(problem, 'SUBMIT')
        ret_p.is_admin = Privilege.demand(problem, 'MANAGE')
        if ret_p.is_admin:
            ret_p.contestant_role_view_times = Privilege.get(problem.contest.contestant_role, problem, 'VIEW')
            ret_p.contestant_role_submit_times = Privilege.get(problem.contest.contestant_role, problem, 'SUBMIT')
        return ret_p;

    @ExportMethod(TypedList(ProblemMappingInfo), [DjangoId('Contest')], PCPermit())
    @staticmethod
    def get_problem_mapping_list(contest):
        ret = []
        for problem in Privilege.where_can(contest.problem_mappings.all(), 'VIEW'):
            ret.append(Web.get_problem_mapping_info(problem))
        return ret

    @ExportMethod(SubpageInfo, [DjangoId('Subpage')], PCPermit())
    @staticmethod
    def get_subpage_info(subpage):
        ret_s = SubpageInfo()
        ret_s.subpage = subpage
        reader = subpage.content_files_get_blob('_html')
        if reader:
            ret_s.html = reader.read()
            reader.close()
        ret_s.is_admin = Privilege.demand(subpage, 'MANAGE')
        return ret_s

    @ExportMethod(TypedList(SubpageInfo), [bool], PCPermit())
    @staticmethod
    def get_subpage_list_global(announcements):
        ret = []
        for subpage in Privilege.where_can(Subpage.get_global(announcements), 'VIEW'):
            ret.append(Web.get_subpage_info(subpage))
        return ret

    @ExportMethod(TypedList(SubpageInfo), [DjangoId('Contest'), bool], PCPermit())
    @staticmethod
    def get_subpage_list_for_contest(contest, announcements):
        ret = []
        for subpage in Privilege.where_can(Subpage.get_for_contest(contest, announcements), 'VIEW'):
            ret.append(Web.get_subpage_info(subpage))
        return ret

    @ExportMethod(SizedContestantList, [DjangoId('Contest'), int, int], PCArg('contest', 'VIEW'))
    @staticmethod
    def get_accepted_contestants(contest, limit=20, offset=0):
        result = Contestant.objects.filter(contest=contest, accepted=True).exclude(parents=contest.admin_role)
        return SizedContestantList(count=len(result), contestants=result[offset:offset+limit])

    @ExportMethod(SizedContestantList, [DjangoId('Contest'), int, int], PCArg('contest', 'MANAGE'))
    @staticmethod
    def get_pending_contestants(contest, limit=20, offset=0):
        result = Contestant.objects.filter(contest=contest, accepted=False).exclude(parents=contest.admin_role)
        return SizedContestantList(count=len(result), contestants=result[offset:offset+limit])

    @ExportMethod(SizedContestantList, [DjangoId('Contest'), int, int], PCArg('contest', 'MANAGE'))
    @staticmethod
    def get_contest_admins(contest, limit=20, offset=0):
        result = Contestant.objects.filter(parents=contest.admin_role)
        return SizedContestantList(count=len(result), contestants=result[offset:offset+limit])

    @ExportMethod(SizedResultList, [DjangoId('Contest'), DjangoId('Contestant'), DjangoId('ProblemMapping'), int, int], PCPermit())
    def get_results(contest, contestant=None, problem=None, limit=20, offset=0):
        if contestant:
            contestant_list = Contestant.objects.filter(id=contestant.id)
        else:
            contestant_list = Contestant.objects.filter(contest=contest)
        contestant_list = list(Privilege.where_can(contestant_list, 'OBSERVE'))
        q = Submit.objects.filter(contestant__in=contestant_list)
#        if contestant:
#            q = Submit.objects.filter(contestant=contestant)
#        else:
#            q = Submit.objects.filter(contestant__contest=contest)
        if problem:
            q = q.filter(problem=problem)
#        q = Privilege.where_can(q, 'OBSERVE')
        ret = []
        for submit in q.order_by('-id')[offset:offset+limit]:
            ret_r = ResultInfo()
            ret_r.submit = submit
            ret_r.contestant = submit.contestant
            ret_r.problem_mapping = submit.problem
            ret_r.status = submit.get_test_suite_status()
            ret_r.report = submit.get_test_suite_report()
            ret.append(ret_r)
        return SizedResultList(
            count=len(q),
            results=ret
            )

    @ExportMethod(ResultInfo, [DjangoId('Submit')], PCArg('submit', 'OBSERVE'))
    def get_result_details(submit):
        ret = ResultInfo()
        ret.submit = submit
        ret.contestant = submit.contestant
        ret.problem_mapping = submit.problem
        ret.status = submit.get_test_suite_status()
        ret.report = submit.get_test_suite_report()
        reader = submit.data_get_blob('content')
        data = reader.read(min(100000, reader.length))
        ret.data_filename = reader.filename
        reader.close()
        try:
            ret.data = unicode(data, 'utf8')
        except:
            ret.data = None
        if Privilege.demand(submit, 'MANAGE'):
            ret_tsrs = []
            for tsr in TestSuiteResult.objects.filter(submit=submit):
                ret_tsr = TestSuiteResultInfo()
                ret_tsr.test_suite = tsr.test_suite
                ret_tsr.test_suite_result = tsr
                ret_tsr.attributes = tsr.oa_get_map()
                ret_tsrs.append(ret_tsr)
            ret_trs = []
            for tr in TestResult.objects.filter(submit=submit):
                ret_tr = TestResultInfo()
                ret_tr.test = tr.test
                ret_tr.test_result = tr
                ret_tr.attributes = tr.oa_get_map()
                ret_trs.append(ret_tr)
            ret.test_results = ret_tsrs
            ret.test_suite_results = ret_trs
        return ret

<|endoftext|>


<commit_before>import sys
import subprocess

# noiseList = [0, 0.02, 0.04, 0.06, 0.08, 0.1]
# noiseList = [0.04, 0.06, 0.08]
noiseList = [0.005, 0.01]
for noise in noiseList:
  process = subprocess.Popen(
    [sys.executable,
          "run_tm_model_sdr_classifier.py",
          "-d", "nyc_taxi",
          "--noise={:.2f}".format(noise)])
<commit_msg>updated noisy taxi experiment with TM<commit_after>import sys
import subprocess

noiseList = [0, 0.02, 0.04, 0.06, 0.08, 0.1]
for noise in noiseList:
  process = subprocess.Popen(
    [sys.executable,
          "run_tm_model_sdr_classifier.py",
          "-d", "nyc_taxi",
          "--noise={:.2f}".format(noise)])
<|endoftext|>


<commit_before><commit_msg>Add data type to function parameter<commit_after><|endoftext|>


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

import unittest

from pychord import Chord
from pychord.chord import as_chord

class TestChordCreations(unittest.TestCase):

    def test_normal_chord(self):
        c = Chord("C")
        self.assertEqual(c.root, "C")
        self.assertEqual(c.quality.quality, "")

    def test_invalid_chord(self):
        self.assertRaises(ValueError, Chord, "H")

    def test_minor_chord(self):
        c = Chord("Am")
        self.assertEqual(c.root, "A")
        self.assertEqual(c.quality.quality, "m")

    def test_minor_chord_with_minus(self):
        c = Chord("A-")
        self.assertEqual(c.root, "A")
        self.assertEqual(c.quality.quality, "-")

    def test_69_chord(self):
        c = Chord("C69")
        self.assertEqual(c.root, "C")
        self.assertEqual(c.quality.quality, "69")

    def test_m75_chord(self):
        c = Chord("Bm7-5")
        self.assertEqual(c.root, "B")
        self.assertEqual(c.quality.quality, "m7-5")

    def test_invalid_quality_chord(self):
        self.assertRaises(ValueError, Chord, "Csus3")

    def test_slash_chord(self):
        c = Chord("F/G")
        self.assertEqual(c.root, "F")
        self.assertEqual(c.quality.quality, "")
        self.assertEqual(c.on, "G")

    def test_minor_slash_chord(self):
        c = Chord("Dm/G")
        self.assertEqual(c.root, "D")
        self.assertEqual(c.quality.quality, "m")
        self.assertEqual(c.on, "G")

    def test_minor_7b5_chord(self):
        c = Chord("Dm7b5")
        self.assertEqual(c.root, "D")
        self.assertEqual(c.quality.quality, "m7b5")

    def test_invalid_slash_chord(self):
        self.assertRaises(ValueError, Chord, "C/H")

    def test_eq(self):
        c1 = Chord("C")
        c2 = Chord("C")
        self.assertEqual(c1, c2)

    def test_eq_alias(self):
        c1 = Chord("Cmaj7")
        c2 = Chord("CM7")
        self.assertEqual(c1, c2)

    def test_invalid_eq(self):
        c = Chord("C")
        with self.assertRaises(TypeError):
            print(c == 0)

class TestAsChord(unittest.TestCase):

    def test_as_chord_chord_input(self):
        c = Chord("C")
        ac = as_chord(c)
        self.assertEqual(c, ac)

    def test_as_chord_str_input(self):
        chord = "C"
        c = Chord(chord)
        ac = as_chord(chord)
        self.assertEqual(c, ac)

    def test_as_chord_invalid_input(self):
        with self.assertRaises(TypeError):
            as_chord(1)

class TestChordFromNoteIndex(unittest.TestCase):

    def test_note_1(self):
        chord = Chord.from_note_index(note=1, quality="", scale="Cmaj")
        self.assertEqual(chord, Chord("C"))

    def test_note_2(self):
        chord = Chord.from_note_index(note=2, quality="m7", scale="F#min")
        self.assertEqual(chord, Chord("G#m7"))

    def test_note_3(self):
        chord = Chord.from_note_index(note=3, quality="sus2", scale="Cmin")
        self.assertEqual(chord, Chord("Ebsus2"))

    def test_note_7(self):
        chord = Chord.from_note_index(note=7, quality="7", scale="Amin")
        self.assertEqual(chord, Chord("G7"))

    def test_note_8(self):
        chord = Chord.from_note_index(note=8, quality="", scale="Emaj")
        self.assertEqual(chord, Chord("E"))

    def test_note_0(self):
        with self.assertRaises(ValueError):
            Chord.from_note_index(note=0, quality="", scale="Cmaj")

    def test_note_9(self):
        with self.assertRaises(ValueError):
            Chord.from_note_index(note=9, quality="", scale="Fmaj")

if __name__ == '__main__':
    unittest.main()
<commit_msg>Add test cases for Chord.from_note_index(diatonic=True)<commit_after># -*- coding: utf-8 -*-

import unittest

from pychord import Chord
from pychord.chord import as_chord

class TestChordCreations(unittest.TestCase):

    def test_normal_chord(self):
        c = Chord("C")
        self.assertEqual(c.root, "C")
        self.assertEqual(c.quality.quality, "")

    def test_invalid_chord(self):
        self.assertRaises(ValueError, Chord, "H")

    def test_minor_chord(self):
        c = Chord("Am")
        self.assertEqual(c.root, "A")
        self.assertEqual(c.quality.quality, "m")

    def test_minor_chord_with_minus(self):
        c = Chord("A-")
        self.assertEqual(c.root, "A")
        self.assertEqual(c.quality.quality, "-")

    def test_69_chord(self):
        c = Chord("C69")
        self.assertEqual(c.root, "C")
        self.assertEqual(c.quality.quality, "69")

    def test_m75_chord(self):
        c = Chord("Bm7-5")
        self.assertEqual(c.root, "B")
        self.assertEqual(c.quality.quality, "m7-5")

    def test_invalid_quality_chord(self):
        self.assertRaises(ValueError, Chord, "Csus3")

    def test_slash_chord(self):
        c = Chord("F/G")
        self.assertEqual(c.root, "F")
        self.assertEqual(c.quality.quality, "")
        self.assertEqual(c.on, "G")

    def test_minor_slash_chord(self):
        c = Chord("Dm/G")
        self.assertEqual(c.root, "D")
        self.assertEqual(c.quality.quality, "m")
        self.assertEqual(c.on, "G")

    def test_minor_7b5_chord(self):
        c = Chord("Dm7b5")
        self.assertEqual(c.root, "D")
        self.assertEqual(c.quality.quality, "m7b5")

    def test_invalid_slash_chord(self):
        self.assertRaises(ValueError, Chord, "C/H")

    def test_eq(self):
        c1 = Chord("C")
        c2 = Chord("C")
        self.assertEqual(c1, c2)

    def test_eq_alias(self):
        c1 = Chord("Cmaj7")
        c2 = Chord("CM7")
        self.assertEqual(c1, c2)

    def test_invalid_eq(self):
        c = Chord("C")
        with self.assertRaises(TypeError):
            print(c == 0)

class TestAsChord(unittest.TestCase):

    def test_as_chord_chord_input(self):
        c = Chord("C")
        ac = as_chord(c)
        self.assertEqual(c, ac)

    def test_as_chord_str_input(self):
        chord = "C"
        c = Chord(chord)
        ac = as_chord(chord)
        self.assertEqual(c, ac)

    def test_as_chord_invalid_input(self):
        with self.assertRaises(TypeError):
            as_chord(1)

class TestChordFromNoteIndex(unittest.TestCase):

    def test_note_1(self):
        chord = Chord.from_note_index(note=1, quality="", scale="Cmaj")
        self.assertEqual(chord, Chord("C"))

    def test_note_2(self):
        chord = Chord.from_note_index(note=2, quality="m7", scale="F#min")
        self.assertEqual(chord, Chord("G#m7"))

    def test_note_3(self):
        chord = Chord.from_note_index(note=3, quality="sus2", scale="Cmin")
        self.assertEqual(chord, Chord("Ebsus2"))

    def test_note_7(self):
        chord = Chord.from_note_index(note=7, quality="7", scale="Amin")
        self.assertEqual(chord, Chord("G7"))

    def test_note_8(self):
        chord = Chord.from_note_index(note=8, quality="", scale="Emaj")
        self.assertEqual(chord, Chord("E"))

    def test_note_0(self):
        with self.assertRaises(ValueError):
            Chord.from_note_index(note=0, quality="", scale="Cmaj")

    def test_note_9(self):
        with self.assertRaises(ValueError):
            Chord.from_note_index(note=9, quality="", scale="Fmaj")

    def test_diatonic_note_1(self):
        chord = Chord.from_note_index(note=1, quality="", diatonic=True, scale="Dmaj")
        self.assertEqual(chord, Chord("D"))

    def test_diatonic_note_2_mode(self):
        chord = Chord.from_note_index(note=2, quality="7", diatonic=True, scale="BLoc")
        self.assertEqual(chord, Chord("Cmaj7"))

    def test_diatonic_note_3_mode(self):
        chord = Chord.from_note_index(note=3, quality="m", diatonic=True, scale="G#Mix")
        self.assertEqual(chord, Chord("Cdim"))

    def test_diatonic_note_4_mode(self):
        chord = Chord.from_note_index(note=4, quality="-", diatonic=True, scale="AbDor")
        self.assertEqual(chord, Chord("C#"))

    def test_diatonic_note_nongeneric(self):
        with self.assertRaises(NotImplementedError):
            Chord.from_note_index(note=5, quality="sus", diatonic=True, scale="Fmaj")

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


<commit_before>import unittest

import psqlparse

class TestParse(unittest.TestCase):

    def test_is_select_statement(self):
        query = "SELECT * FROM my_table"
        select_stmt = psqlparse.parse(query).pop()
        self.assertEqual(select_stmt.type, "SelectStmt")

    def test_has_insert_and_select_statement(self):
        query = "INSERT INTO my_table(id) VALUES(1); SELECT * FROM my_table"
        insert_and_select_stmt = psqlparse.parse(query)
        stmt_types = [stmt.type for stmt in insert_and_select_stmt]
        self.assertListEqual(stmt_types, ['InsertStmt', 'SelectStmt'])

    def test_syntax_error_select_statement(self):
        query = "SELECT * FRO my_table"
        try:
            psqlparse.parse(query)
        except Exception as e:
            exception = e
        self.assertIsInstance(exception, psqlparse.exceptions.PSqlParseError)
<commit_msg>import only parse and psql exception because is the only scope of the test<commit_after>import unittest

from psqlparse import parse
from psqlparse.exceptions import PSqlParseError

class TestParse(unittest.TestCase):

    def test_is_select_statement(self):
        query = "SELECT * FROM my_table"
        select_stmt = parse(query).pop()
        self.assertEqual(select_stmt.type, "SelectStmt")

    def test_has_insert_and_select_statement(self):
        query = "INSERT INTO my_table(id) VALUES(1); SELECT * FROM my_table"
        insert_and_select_stmt = parse(query)
        stmt_types = [stmt.type for stmt in insert_and_select_stmt]
        self.assertListEqual(stmt_types, ['InsertStmt', 'SelectStmt'])

    def test_syntax_error_select_statement(self):
        query = "SELECT * FRO my_table"
        try:
            parse(query)
        except Exception as e:
            exception = e
        self.assertIsInstance(exception, PSqlParseError)
<|endoftext|>


<commit_before>from collections import OrderedDict
import logging

from scanpointgenerator.compat import range_
from scanpointgenerator import Generator
from scanpointgenerator import Point
from scanpointgenerator.excluder import Excluder
from scanpointgenerator.mutator import Mutator

@Generator.register_subclass("CompoundGenerator")
class CompoundGenerator(Generator):
    """Nest N generators, apply exclusion regions to relevant generator pairs
     and apply any mutators before yielding points"""

    def __init__(self, generators, excluders, mutators):
        """
        Args:
            generators(list(Generator)): List of Generators to nest
            excluders(list(Excluder)): List of Excluders to filter points by
            mutators(list(Mutator)): List of Mutators to apply to each point
        """

        self.generators = generators
        self.excluders = excluders
        self.mutators = mutators

        self.num = 1
        self.periods = []
        self.alternate_direction = []
        self.point_sets = []
        for generator in self.generators:
            logging.debug("Generator passed to Compound init")
            logging.debug(generator.to_dict())
            self.alternate_direction.append(generator.alternate_direction)
            self.point_sets.append(list(generator.iterator()))
        for generator in self.generators[::-1]:
            self.num *= generator.num
            self.periods.insert(0, self.num)

        logging.debug("CompoundGenerator periods")
        logging.debug(self.periods)

        self.position_units = generators[0].position_units.copy()
        for generator in generators[1:]:
            self.position_units.update(generator.position_units)

        self.index_dims = []
        for self.generator in self.generators:
            self.index_dims += self.generator.index_dims

        if self.excluders:  # Calculate number of remaining points and flatten
                            # index dimensions
            remaining_points = 0
            for _ in self._filtered_base_iterator():
                # TODO: Faster with enumerate()?
                remaining_points += 1
            self.index_dims = [remaining_points]

        self.index_names = []
        for self.generator in self.generators:
            self.index_names += self.generator.index_names

        self.axes = []
        for self.generator in self.generators:
            self.axes += self.generator.axes

        if len(self.index_names) != len(set(self.index_names)):
            raise ValueError("Axis names cannot be duplicated; names was %s" % self.index_names)

    def _base_iterator(self):
        """
        Iterator to generate points by nesting each generator in self.generators

        Yields:
            Point: Base points
        """

        for point_num in range_(self.num):

            point = Point()
            for gen_index, points in enumerate(self.point_sets):
                axis_period = self.periods[gen_index]
                axis_length = len(points)
                # Can't use index_dims in case they have been flattened
                # by an excluder

                point_index = \
                    (point_num / (axis_period / axis_length)) % axis_length
                loop_number = point_num / axis_period

                # Floor floats to ints for indexing
                point_index = int(point_index)
                loop_number = int(loop_number)
                if self.alternate_direction[gen_index] and loop_number % 2:
                    point_index = (axis_length - 1) - point_index
                    reverse = True
                else:
                    reverse = False

                current_point = points[point_index]

                # If innermost generator, use bounds
                if gen_index == len(self.point_sets) - 1:
                    point.positions.update(current_point.positions)
                    if reverse:  # Swap bounds if reversing
                        point.upper.update(current_point.lower)
                        point.lower.update(current_point.upper)
                    else:
                        point.upper.update(current_point.upper)
                        point.lower.update(current_point.lower)
                else:
                    point.positions.update(current_point.positions)
                    point.upper.update(current_point.positions)
                    point.lower.update(current_point.positions)

                point.indexes += current_point.indexes

                logging.debug("Current point positions and indexes")
                logging.debug([current_point.positions, current_point.indexes])

            yield point

    def _filtered_base_iterator(self):
        """
        Iterator to filter out points based on Excluders

        Yields:
            Point: Filtered points
        """

        for point in self._base_iterator():
            if self.contains_point(point):
                yield point

    def iterator(self):
        """
        Top level iterator to mutate points and yield them

        Yields:
            Point: Mutated points
        """

        iterator = self._filtered_base_iterator()
        for mutator in self.mutators:
            iterator = mutator.mutate(iterator)

        if self.excluders:
            point_index = 0
            for point in iterator:
                point.indexes = [point_index]
                point_index += 1
                yield point
        else:
            for point in iterator:
                yield point

    def contains_point(self, point):
        """
        Filter a Point through all Excluders

        Args:
            point(Point): Point to check

        Returns:
            bool: Whether point is contained by all Excluders
        """

        contains_point = True

        for excluder in self.excluders:
            coord_1 = point.positions[excluder.scannables[0]]
            coord_2 = point.positions[excluder.scannables[1]]
            coordinate = [coord_1, coord_2]

            if not excluder.roi.contains_point(coordinate):
                contains_point = False
                break

        return contains_point

    def to_dict(self):
        """Convert object attributes into a dictionary"""

        d = OrderedDict()
        d['type'] = "CompoundGenerator"

        d['generators'] = []
        for generator in self.generators:
            d['generators'].append(generator.to_dict())

        d['excluders'] = []
        for excluder in self.excluders:
            d['excluders'].append(excluder.to_dict())

        d['mutators'] = []
        for mutator in self.mutators:
            d['mutators'].append(mutator.to_dict())

        return d

    @classmethod
    def from_dict(cls, d):
        """
        Create a CompoundGenerator instance from a serialised dictionary

        Args:
            d(dict): Dictionary of attributes

        Returns:
            CompoundGenerator: New CompoundGenerator instance
        """

        generators = []
        for generator in d['generators']:
            generators.append(Generator.from_dict(generator))

        excluders = []
        for excluder in d['excluders']:
            excluders.append(Excluder.from_dict(excluder))

        mutators = []
        for mutator in d['mutators']:
            mutators.append(Mutator.from_dict(mutator))

        return cls(generators, excluders, mutators)
<commit_msg>Fix weird for loops<commit_after>from collections import OrderedDict
import logging

from scanpointgenerator.compat import range_
from scanpointgenerator import Generator
from scanpointgenerator import Point
from scanpointgenerator.excluder import Excluder
from scanpointgenerator.mutator import Mutator

@Generator.register_subclass("CompoundGenerator")
class CompoundGenerator(Generator):
    """Nest N generators, apply exclusion regions to relevant generator pairs
     and apply any mutators before yielding points"""

    def __init__(self, generators, excluders, mutators):
        """
        Args:
            generators(list(Generator)): List of Generators to nest
            excluders(list(Excluder)): List of Excluders to filter points by
            mutators(list(Mutator)): List of Mutators to apply to each point
        """

        self.generators = generators
        self.excluders = excluders
        self.mutators = mutators

        self.num = 1
        self.periods = []
        self.alternate_direction = []
        self.point_sets = []
        for generator in self.generators:
            logging.debug("Generator passed to Compound init")
            logging.debug(generator.to_dict())
            self.alternate_direction.append(generator.alternate_direction)
            self.point_sets.append(list(generator.iterator()))
        for generator in self.generators[::-1]:
            self.num *= generator.num
            self.periods.insert(0, self.num)

        logging.debug("CompoundGenerator periods")
        logging.debug(self.periods)

        self.position_units = generators[0].position_units.copy()
        for generator in generators[1:]:
            self.position_units.update(generator.position_units)

        self.index_dims = []
        for generator in self.generators:
            self.index_dims += generator.index_dims

        if self.excluders:  # Calculate number of remaining points and flatten
                            # index dimensions
            remaining_points = 0
            for _ in self._filtered_base_iterator():
                # TODO: Faster with enumerate()?
                remaining_points += 1
            self.index_dims = [remaining_points]

        self.index_names = []
        for generator in self.generators:
            self.index_names += generator.index_names

        self.axes = []
        for generator in self.generators:
            self.axes += generator.axes

        if len(self.index_names) != len(set(self.index_names)):
            raise ValueError("Axis names cannot be duplicated; names was %s" % self.index_names)

    def _base_iterator(self):
        """
        Iterator to generate points by nesting each generator in self.generators

        Yields:
            Point: Base points
        """

        for point_num in range_(self.num):

            point = Point()
            for gen_index, points in enumerate(self.point_sets):
                axis_period = self.periods[gen_index]
                axis_length = len(points)
                # Can't use index_dims in case they have been flattened
                # by an excluder

                point_index = \
                    (point_num / (axis_period / axis_length)) % axis_length
                loop_number = point_num / axis_period

                # Floor floats to ints for indexing
                point_index = int(point_index)
                loop_number = int(loop_number)
                if self.alternate_direction[gen_index] and loop_number % 2:
                    point_index = (axis_length - 1) - point_index
                    reverse = True
                else:
                    reverse = False

                current_point = points[point_index]

                # If innermost generator, use bounds
                if gen_index == len(self.point_sets) - 1:
                    point.positions.update(current_point.positions)
                    if reverse:  # Swap bounds if reversing
                        point.upper.update(current_point.lower)
                        point.lower.update(current_point.upper)
                    else:
                        point.upper.update(current_point.upper)
                        point.lower.update(current_point.lower)
                else:
                    point.positions.update(current_point.positions)
                    point.upper.update(current_point.positions)
                    point.lower.update(current_point.positions)

                point.indexes += current_point.indexes

                logging.debug("Current point positions and indexes")
                logging.debug([current_point.positions, current_point.indexes])

            yield point

    def _filtered_base_iterator(self):
        """
        Iterator to filter out points based on Excluders

        Yields:
            Point: Filtered points
        """

        for point in self._base_iterator():
            if self.contains_point(point):
                yield point

    def iterator(self):
        """
        Top level iterator to mutate points and yield them

        Yields:
            Point: Mutated points
        """

        iterator = self._filtered_base_iterator()
        for mutator in self.mutators:
            iterator = mutator.mutate(iterator)

        if self.excluders:
            point_index = 0
            for point in iterator:
                point.indexes = [point_index]
                point_index += 1
                yield point
        else:
            for point in iterator:
                yield point

    def contains_point(self, point):
        """
        Filter a Point through all Excluders

        Args:
            point(Point): Point to check

        Returns:
            bool: Whether point is contained by all Excluders
        """

        contains_point = True

        for excluder in self.excluders:
            coord_1 = point.positions[excluder.scannables[0]]
            coord_2 = point.positions[excluder.scannables[1]]
            coordinate = [coord_1, coord_2]

            if not excluder.roi.contains_point(coordinate):
                contains_point = False
                break

        return contains_point

    def to_dict(self):
        """Convert object attributes into a dictionary"""

        d = OrderedDict()
        d['type'] = "CompoundGenerator"

        d['generators'] = []
        for generator in self.generators:
            d['generators'].append(generator.to_dict())

        d['excluders'] = []
        for excluder in self.excluders:
            d['excluders'].append(excluder.to_dict())

        d['mutators'] = []
        for mutator in self.mutators:
            d['mutators'].append(mutator.to_dict())

        return d

    @classmethod
    def from_dict(cls, d):
        """
        Create a CompoundGenerator instance from a serialised dictionary

        Args:
            d(dict): Dictionary of attributes

        Returns:
            CompoundGenerator: New CompoundGenerator instance
        """

        generators = []
        for generator in d['generators']:
            generators.append(Generator.from_dict(generator))

        excluders = []
        for excluder in d['excluders']:
            excluders.append(Excluder.from_dict(excluder))

        mutators = []
        for mutator in d['mutators']:
            mutators.append(Mutator.from_dict(mutator))

        return cls(generators, excluders, mutators)
<|endoftext|>


<commit_before>import json
import logging
from couchdbkit.exceptions import ResourceConflict, ResourceNotFound
from django.core.management.base import BaseCommand
from restkit import RequestFailed
from casexml.apps.case.cleanup import rebuild_case
from casexml.apps.case.const import CASE_ACTION_REBUILD
from casexml.apps.case.models import CommCareCase
from couchlog.models import ExceptionRecord

SEARCH_KEY = 'CASE XFORM MISMATCH'
FULL_SEARCH = '{} AND NOT archived'.format(SEARCH_KEY)

class SearchPaginator(object):
    def __init__(self, database, query, page_size=100):
        self.database = database
        self.query = query
        self.page_size = page_size

        self.bookmark = None

    def next_page(self):
        extra = {}
        if self.bookmark:
            extra['bookmark'] = self.bookmark

        result = ExceptionRecord.get_db().search(
            'couchlog/_search/search',
            handler='_design',
            q=self.query,
            include_docs=True,
            limit=self.page_size,
            **extra
        )

        try:
            self.total_rows = result.total_rows
            self.bookmark = result._result_cache.get('bookmark')
            return result
        except RequestFailed:
            # ignore for now
            return []

def row_to_record(row):
    doc = ExceptionRecord.wrap(row["doc"])
    domain = doc.domain if hasattr(doc, "domain") else ""
    try:
        domain, case_id = domain.split(',')
    except ValueError:
        return {'exception': doc.to_json()}
    return {
        'domain': domain,
        'case_id': case_id,
        'exception': doc.to_json()
    }

def dump_logs_to_file(filename):
    paginator = SearchPaginator(ExceptionRecord.get_db(), FULL_SEARCH)

    records_written = 0
    with open(filename, 'w') as log_file:
        while True:
            for page in paginator.next_page():
                if not page:
                    return

                for row in page:
                    record = row_to_record(row)
                    if 'case_id' not in record and SEARCH_KEY not in record['exception']['message']:
                        # search results are no longer relevant
                        return

                    log_file.write('{}\n'.format(json.dumps(record)))
                    records_written += 1
                    if records_written % 100 == 0:
                        print '{} of {} records writen to file'.format(records_written, paginator.total_rows)

    return paginator.total_rows

def archive_exception(exception):
    try:
        exception.archive('system')
    except ResourceConflict:
        pass

def should_rebuild(case_id):
    try:
        case = CommCareCase.get(case_id)
        return case.actions[-1].action_type != CASE_ACTION_REBUILD
    except ResourceNotFound:
        return True

class Command(BaseCommand):
    help = ('Rebuild cases that have been identified to have mismatched forms and actions.'
            'See http://manage.dimagi.com/default.asp?158779 for details.')

    def handle(self, *args, **options):
        count = 1
        filename = 'cases_with_mismatched_forms.log'
        total = dump_logs_to_file(filename)

        seen_cases = set()
        with open(filename, 'r') as file:
            while True:
                line = file.readline()
                if not line:
                    return

                print 'Processing record {} of {}'.format(count, total)
                record = json.loads(line)
                exception = ExceptionRecord.wrap(record['exception'])
                case_id = record.get('case_id', None)
                if not case_id or case_id in seen_cases:
                    count += 1
                    archive_exception(exception)
                    continue

                try:
                    seen_cases.add(case_id)
                    if should_rebuild(case_id):
                        rebuild_case(case_id)
                        print 'rebuilt case {}'.format(case_id)
                    archive_exception(exception)
                except Exception, e:
                    logging.exception("couldn't rebuild case {id}. {msg}".format(id=case_id, msg=str(e)))
                finally:
                    count += 1
<commit_msg>tweaks<commit_after>import json
import logging
from couchdbkit.exceptions import ResourceConflict, ResourceNotFound
from django.core.management.base import BaseCommand
from restkit import RequestFailed
from casexml.apps.case.cleanup import rebuild_case
from casexml.apps.case.const import CASE_ACTION_REBUILD
from casexml.apps.case.models import CommCareCase
from couchlog.models import ExceptionRecord

SEARCH_KEY = 'CASE XFORM MISMATCH'
FULL_SEARCH = '{} AND NOT archived'.format(SEARCH_KEY)

class SearchPaginator(object):
    def __init__(self, database, query, page_size=100):
        self.database = database
        self.query = query
        self.page_size = page_size

        self.bookmark = None

    def next_page(self):
        extra = {}
        if self.bookmark:
            extra['bookmark'] = self.bookmark

        result = ExceptionRecord.get_db().search(
            'couchlog/_search/search',
            handler='_design',
            q=self.query,
            include_docs=True,
            limit=self.page_size,
            **extra
        )

        try:
            result.fetch()
            self.bookmark = result._result_cache.get('bookmark')
            return result
        except RequestFailed:
            # ignore for now
            return []

def row_to_record(row):
    doc = ExceptionRecord.wrap(row["doc"])
    domain = doc.domain if hasattr(doc, "domain") else ""
    try:
        domain, case_id = domain.split(',')
    except ValueError:
        return {'exception': doc.to_json()}
    return {
        'domain': domain,
        'case_id': case_id,
        'exception': doc.to_json()
    }

def dump_logs_to_file(filename):
    print 'Writing logs to file'
    paginator = SearchPaginator(ExceptionRecord.get_db(), FULL_SEARCH)

    records_written = 0
    try:
        with open(filename, 'w') as log_file:
            while True:
                page = paginator.next_page()
                if not page:
                    return

                for row in page:
                    record = row_to_record(row)
                    if record['exception']['archived']:
                        continue
                    if 'case_id' not in record and SEARCH_KEY not in record['exception']['message']:
                        # search results are no longer relevant
                        return

                    log_file.write('{}\n'.format(json.dumps(record)))
                    records_written += 1
                    if records_written % 100 == 0:
                        print '{} records writen to file'.format(records_written)
    finally:
        print '{} records writen to file'.format(records_written)

def archive_exception(exception):
    try:
        exception.archive('system')
    except ResourceConflict:
        pass

def should_rebuild(case_id):
    try:
        case = CommCareCase.get(case_id)
        return case.actions[-1].action_type != CASE_ACTION_REBUILD
    except ResourceNotFound:
        return True

class Command(BaseCommand):
    help = ('Rebuild cases that have been identified to have mismatched forms and actions.'
            'See http://manage.dimagi.com/default.asp?158779 for details.')

    def handle(self, *args, **options):
        count = 1
        filename = 'cases_with_mismatched_forms.log'
        dump_logs_to_file(filename)

        seen_cases = set()
        cases_rebuilt = set()
        with open(filename, 'r') as file:
            while True:
                line = file.readline()
                if not line:
                    return

                print 'Processing record {}'.format(count)
                record = json.loads(line)
                exception = ExceptionRecord.wrap(record['exception'])
                case_id = record.get('case_id', None)
                if not case_id or case_id in seen_cases:
                    count += 1
                    archive_exception(exception)
                    continue

                try:
                    seen_cases.add(case_id)
                    if should_rebuild(case_id):
                        cases_rebuilt.add(case_id)
                        rebuild_case(case_id)
                        print 'rebuilt case {}'.format(case_id)
                    archive_exception(exception)
                except Exception, e:
                    logging.exception("couldn't rebuild case {id}. {msg}".format(id=case_id, msg=str(e)))
                finally:
                    count += 1

        print "Cases rebuilt: {}".format(cases_rebuilt)
<|endoftext|>