<commit_before>#!/usr/bin/env python
# Copyright (c) PLUMgrid, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")

from bcc import BPF
import ctypes as ct
import random
import time
import subprocess
from bcc.utils import get_online_cpus
from unittest import main, TestCase

class TestArray(TestCase):
    def test_simple(self):
        b = BPF(text="""BPF_ARRAY(table1, u64, 128);""")
        t1 = b["table1"]
        t1[ct.c_int(0)] = ct.c_ulonglong(100)
        t1[ct.c_int(127)] = ct.c_ulonglong(1000)
        for i, v in t1.items():
            if i.value == 0:
                self.assertEqual(v.value, 100)
            if i.value == 127:
                self.assertEqual(v.value, 1000)
        self.assertEqual(len(t1), 128)

    def test_native_type(self):
        b = BPF(text="""BPF_ARRAY(table1, u64, 128);""")
        t1 = b["table1"]
        t1[0] = ct.c_ulonglong(100)
        t1[-2] = ct.c_ulonglong(37)
        t1[127] = ct.c_ulonglong(1000)
        for i, v in t1.items():
            if i.value == 0:
                self.assertEqual(v.value, 100)
            if i.value == 127:
                self.assertEqual(v.value, 1000)
        self.assertEqual(len(t1), 128)
        self.assertEqual(t1[-2].value, 37)
        self.assertEqual(t1[-1].value, t1[127].value)

    def test_perf_buffer(self):
        self.counter = 0

        class Data(ct.Structure):
            _fields_ = [("ts", ct.c_ulonglong)]

        def cb(cpu, data, size):
            self.assertGreater(size, ct.sizeof(Data))
            event = ct.cast(data, ct.POINTER(Data)).contents
            self.counter += 1

        def lost_cb(lost):
            self.assertGreater(lost, 0)

        text = """
BPF_PERF_OUTPUT(events);
int do_sys_nanosleep(void *ctx) {
    struct {
        u64 ts;
    } data = {bpf_ktime_get_ns()};
    events.perf_submit(ctx, &data, sizeof(data));
    return 0;
}
"""
        b = BPF(text=text)
        b.attach_kprobe(event=b.get_syscall_fnname("nanosleep"),
                        fn_name="do_sys_nanosleep")
        b["events"].open_perf_buffer(cb, lost_cb=lost_cb)
        subprocess.call(['sleep', '0.1'])
        b.perf_buffer_poll()
        self.assertGreater(self.counter, 0)
        b.cleanup()

    def test_perf_buffer_for_each_cpu(self):
        self.events = []

        class Data(ct.Structure):
            _fields_ = [("cpu", ct.c_ulonglong)]

        def cb(cpu, data, size):
            self.assertGreater(size, ct.sizeof(Data))
            event = ct.cast(data, ct.POINTER(Data)).contents
            self.events.append(event)

        def lost_cb(lost):
            self.assertGreater(lost, 0)

        text = """
BPF_PERF_OUTPUT(events);
int do_sys_nanosleep(void *ctx) {
    struct {
        u64 cpu;
    } data = {bpf_get_smp_processor_id()};
    events.perf_submit(ctx, &data, sizeof(data));
    return 0;
}
"""
        b = BPF(text=text)
        b.attach_kprobe(event=b.get_syscall_fnname("nanosleep"),
                        fn_name="do_sys_nanosleep")
        b["events"].open_perf_buffer(cb, lost_cb=lost_cb)
        online_cpus = get_online_cpus()
        for cpu in online_cpus:
            subprocess.call(['taskset', '-c', str(cpu), 'sleep', '0.1'])
        b.perf_buffer_poll()
        b.cleanup()
        self.assertGreaterEqual(len(self.events), len(online_cpus), 'Received only {}/{} events'.format(len(self.events), len(online_cpus)))

if __name__ == "__main__":
    main()
<commit_msg>tests/test_array.py: add clock_nanosleep to attach point<commit_after>#!/usr/bin/env python
# Copyright (c) PLUMgrid, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")

from bcc import BPF
import ctypes as ct
import random
import time
import subprocess
from bcc.utils import get_online_cpus
from unittest import main, TestCase

class TestArray(TestCase):
    def test_simple(self):
        b = BPF(text="""BPF_ARRAY(table1, u64, 128);""")
        t1 = b["table1"]
        t1[ct.c_int(0)] = ct.c_ulonglong(100)
        t1[ct.c_int(127)] = ct.c_ulonglong(1000)
        for i, v in t1.items():
            if i.value == 0:
                self.assertEqual(v.value, 100)
            if i.value == 127:
                self.assertEqual(v.value, 1000)
        self.assertEqual(len(t1), 128)

    def test_native_type(self):
        b = BPF(text="""BPF_ARRAY(table1, u64, 128);""")
        t1 = b["table1"]
        t1[0] = ct.c_ulonglong(100)
        t1[-2] = ct.c_ulonglong(37)
        t1[127] = ct.c_ulonglong(1000)
        for i, v in t1.items():
            if i.value == 0:
                self.assertEqual(v.value, 100)
            if i.value == 127:
                self.assertEqual(v.value, 1000)
        self.assertEqual(len(t1), 128)
        self.assertEqual(t1[-2].value, 37)
        self.assertEqual(t1[-1].value, t1[127].value)

    def test_perf_buffer(self):
        self.counter = 0

        class Data(ct.Structure):
            _fields_ = [("ts", ct.c_ulonglong)]

        def cb(cpu, data, size):
            self.assertGreater(size, ct.sizeof(Data))
            event = ct.cast(data, ct.POINTER(Data)).contents
            self.counter += 1

        def lost_cb(lost):
            self.assertGreater(lost, 0)

        text = """
BPF_PERF_OUTPUT(events);
int do_sys_nanosleep(void *ctx) {
    struct {
        u64 ts;
    } data = {bpf_ktime_get_ns()};
    events.perf_submit(ctx, &data, sizeof(data));
    return 0;
}
"""
        b = BPF(text=text)
        b.attach_kprobe(event=b.get_syscall_fnname("nanosleep"),
                        fn_name="do_sys_nanosleep")
        b.attach_kprobe(event=b.get_syscall_fnname("clock_nanosleep"),
                        fn_name="do_sys_nanosleep")
        b["events"].open_perf_buffer(cb, lost_cb=lost_cb)
        subprocess.call(['sleep', '0.1'])
        b.perf_buffer_poll()
        self.assertGreater(self.counter, 0)
        b.cleanup()

    def test_perf_buffer_for_each_cpu(self):
        self.events = []

        class Data(ct.Structure):
            _fields_ = [("cpu", ct.c_ulonglong)]

        def cb(cpu, data, size):
            self.assertGreater(size, ct.sizeof(Data))
            event = ct.cast(data, ct.POINTER(Data)).contents
            self.events.append(event)

        def lost_cb(lost):
            self.assertGreater(lost, 0)

        text = """
BPF_PERF_OUTPUT(events);
int do_sys_nanosleep(void *ctx) {
    struct {
        u64 cpu;
    } data = {bpf_get_smp_processor_id()};
    events.perf_submit(ctx, &data, sizeof(data));
    return 0;
}
"""
        b = BPF(text=text)
        b.attach_kprobe(event=b.get_syscall_fnname("nanosleep"),
                        fn_name="do_sys_nanosleep")
        b.attach_kprobe(event=b.get_syscall_fnname("clock_nanosleep"),
                        fn_name="do_sys_nanosleep")
        b["events"].open_perf_buffer(cb, lost_cb=lost_cb)
        online_cpus = get_online_cpus()
        for cpu in online_cpus:
            subprocess.call(['taskset', '-c', str(cpu), 'sleep', '0.1'])
        b.perf_buffer_poll()
        b.cleanup()
        self.assertGreaterEqual(len(self.events), len(online_cpus), 'Received only {}/{} events'.format(len(self.events), len(online_cpus)))

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


<commit_before>##############################################################################
#
#    Copyright (C) 2015 Compassion CH (http://www.compassion.ch)
#    Releasing children from poverty in Jesus' name
#    @author: Albert SHENOUDA <albert.shenouda@efrei.net>
#
#    The licence is in the file __manifest__.py
#
##############################################################################

from odoo.tests import common
from datetime import datetime
from odoo import netsvc
import logging
logger = logging.getLogger(__name__)

class TestSplitInvoice(common.TransactionCase):
    """ Test Project split invoice. 2 cases are tested :
           - open invoices
           - draft invoices
        This test check if the original invoice is well splitted in 2 invoices
        with the same informations and if the amount are matched """
    def setUp(self):
        super().setUp()
        self.invoice_id = self._create_invoice('SAJ/2015/0002')
        self.invoice_id1 = self._create_invoice('SAJ/2015/0003')
        self.invoice_line_id1 = self._create_invoice_line(
            'service 1', 70.0, self.invoice_id)
        self.invoice_line_id2 = self._create_invoice_line(
            'service 2', 80.0, self.invoice_id)
        self.invoice_line_id11 = self._create_invoice_line(
            'service 11', 40.0, self.invoice_id1)
        self.invoice_line_id22 = self._create_invoice_line(
            'service 22', 50.0, self.invoice_id1)

    def test_open_invoice(self):
        """ Run test for invoice in 'open' state """
        self.assertTrue(self.invoice_id)
        invoice_obj = self.env['account.invoice']
        invoice = invoice_obj.browse(self.invoice_id)
        self.assertTrue(invoice)
        invoice.action_invoice_open()
        wizard_obj = self.env['account.invoice.split.wizard'].with_context(
            {'active_id': self.invoice_id})
        wizard = wizard_obj.create({})

        original_amount = invoice.amount_total
        original_name = invoice.name
        original_partner_id = invoice.partner_id.id
        original_invoice_date = invoice.date_invoice
        original_account = invoice.account_id.id
        original_journal = invoice.journal_id.id
        wizard.write(
            {'invoice_line_ids': [(4, self.invoice_line_id1)]})
        invoice_new = wizard.split_invoice()
        invoice = invoice_obj.browse(self.invoice_id)
        # Test if the lines have been exactly copied
        self.assertEqual(invoice.name, original_name)
        self.assertEqual(
            invoice.partner_id.id, original_partner_id)
        self.assertEqual(
            wizard.invoice_line_ids[0].invoice_id.date_invoice,
            original_invoice_date)
        self.assertEqual(
            invoice.date_invoice, original_invoice_date)
        self.assertEqual(
            wizard.invoice_line_ids[0].invoice_id.account_id.id,
            original_account)
        self.assertEqual(
            invoice.account_id.id, original_account)
        self.assertEqual(
            wizard.invoice_line_ids[0].invoice_id.journal_id.id,
            original_journal)
        self.assertEqual(
            invoice.journal_id.id, original_journal)
        self.assertEqual(
            original_amount,
            invoice_new.amount_total + invoice.amount_total
        )

    def test_draft_invoice(self):
        """ Run test for invoice in 'draft' state """
        self.assertTrue(self.invoice_id1)
        invoice_obj = self.env['account.invoice']
        invoice = invoice_obj.browse(self.invoice_id1)
        self.assertTrue(invoice)

        wizard_obj = self.env['account.invoice.split.wizard'].with_context(
            {'active_id': self.invoice_id1})
        wizard = wizard_obj.create({})

        original_amount = invoice.amount_total
        original_name = invoice.name
        orginal_partner_id = invoice.partner_id.id
        original_invoice_date = invoice.date_invoice
        original_account = invoice.account_id.id
        original_journal = invoice.journal_id.id
        wizard.write({
            'invoice_line_ids': [(4, self.invoice_line_id11)]})
        invoice_new = wizard.split_invoice()
        invoice = invoice_obj.browse(self.invoice_id1)
        # Test if the lines have been exactly copied
        self.assertEqual(invoice.name, original_name)
        self.assertEqual(
            wizard.invoice_line_ids[0].invoice_id.partner_id.id,
            orginal_partner_id)
        self.assertEqual(
            invoice.partner_id.id, orginal_partner_id)
        self.assertEqual(
            wizard.invoice_line_ids[0].invoice_id.date_invoice,
            original_invoice_date)
        self.assertEqual(
            invoice.date_invoice, original_invoice_date)
        self.assertEqual(
            wizard.invoice_line_ids[0].invoice_id.account_id.id,
            original_account)
        self.assertEqual(
            invoice.account_id.id, original_account)
        self.assertEqual(
            wizard.invoice_line_ids[0].invoice_id.journal_id.id,
            original_journal)
        self.assertEqual(
            invoice.journal_id.id, original_journal)
        self.assertEqual(
            original_amount,
            invoice_new.amount_total + invoice.amount_total
        )

    def _create_invoice(self, invoice_name):
        """ Set the update_posted to True to make invoice cancelable """
        journal_obj = self.env['account.journal']
        journal = journal_obj.search([('id', '=', 1)])
        journal.write({
            'update_posted': True,
        })
        partner_obj = self.env['res.partner']
        partner_id = partner_obj.create({
            'name': 'Kevin',
        }).id
        account_id = self.env['account.account'].search(
            [('internal_type', '=', 'receivable')])[0].id
        invoice_obj = self.env['account.invoice']
        invoice_id = invoice_obj.create({
            'name': invoice_name,
            'account_id': account_id,
            'currency_id': 1,
            'journal_id': 1,
            'partner_id': partner_id,
            'date_invoice': datetime.today()
        }).id
        return invoice_id

    def _create_invoice_line(self, description, amount, invoice_id):
        """ Create invoice's lines """
        invl_obj = self.env['account.invoice.line'].with_context(
            journal_id=1)
        invoice_line_id = invl_obj.create({
            'name': description,
            'price_unit': amount,
            'invoice_id': invoice_id,
            'account_id': invl_obj._default_account()
        }).id
        return invoice_line_id
<commit_msg>fix flak<commit_after>##############################################################################
#
#    Copyright (C) 2015 Compassion CH (http://www.compassion.ch)
#    Releasing children from poverty in Jesus' name
#    @author: Albert SHENOUDA <albert.shenouda@efrei.net>
#
#    The licence is in the file __manifest__.py
#
##############################################################################

from odoo.tests import common
from datetime import datetime
import logging
logger = logging.getLogger(__name__)

class TestSplitInvoice(common.TransactionCase):
    """ Test Project split invoice. 2 cases are tested :
           - open invoices
           - draft invoices
        This test check if the original invoice is well splitted in 2 invoices
        with the same informations and if the amount are matched """
    def setUp(self):
        super().setUp()
        self.invoice_id = self._create_invoice('SAJ/2015/0002')
        self.invoice_id1 = self._create_invoice('SAJ/2015/0003')
        self.invoice_line_id1 = self._create_invoice_line(
            'service 1', 70.0, self.invoice_id)
        self.invoice_line_id2 = self._create_invoice_line(
            'service 2', 80.0, self.invoice_id)
        self.invoice_line_id11 = self._create_invoice_line(
            'service 11', 40.0, self.invoice_id1)
        self.invoice_line_id22 = self._create_invoice_line(
            'service 22', 50.0, self.invoice_id1)

    def test_open_invoice(self):
        """ Run test for invoice in 'open' state """
        self.assertTrue(self.invoice_id)
        invoice_obj = self.env['account.invoice']
        invoice = invoice_obj.browse(self.invoice_id)
        self.assertTrue(invoice)
        invoice.action_invoice_open()
        wizard_obj = self.env['account.invoice.split.wizard'].with_context(
            {'active_id': self.invoice_id})
        wizard = wizard_obj.create({})

        original_amount = invoice.amount_total
        original_name = invoice.name
        original_partner_id = invoice.partner_id.id
        original_invoice_date = invoice.date_invoice
        original_account = invoice.account_id.id
        original_journal = invoice.journal_id.id
        wizard.write(
            {'invoice_line_ids': [(4, self.invoice_line_id1)]})
        invoice_new = wizard.split_invoice()
        invoice = invoice_obj.browse(self.invoice_id)
        # Test if the lines have been exactly copied
        self.assertEqual(invoice.name, original_name)
        self.assertEqual(
            invoice.partner_id.id, original_partner_id)
        self.assertEqual(
            wizard.invoice_line_ids[0].invoice_id.date_invoice,
            original_invoice_date)
        self.assertEqual(
            invoice.date_invoice, original_invoice_date)
        self.assertEqual(
            wizard.invoice_line_ids[0].invoice_id.account_id.id,
            original_account)
        self.assertEqual(
            invoice.account_id.id, original_account)
        self.assertEqual(
            wizard.invoice_line_ids[0].invoice_id.journal_id.id,
            original_journal)
        self.assertEqual(
            invoice.journal_id.id, original_journal)
        self.assertEqual(
            original_amount,
            invoice_new.amount_total + invoice.amount_total
        )

    def test_draft_invoice(self):
        """ Run test for invoice in 'draft' state """
        self.assertTrue(self.invoice_id1)
        invoice_obj = self.env['account.invoice']
        invoice = invoice_obj.browse(self.invoice_id1)
        self.assertTrue(invoice)

        wizard_obj = self.env['account.invoice.split.wizard'].with_context(
            {'active_id': self.invoice_id1})
        wizard = wizard_obj.create({})

        original_amount = invoice.amount_total
        original_name = invoice.name
        orginal_partner_id = invoice.partner_id.id
        original_invoice_date = invoice.date_invoice
        original_account = invoice.account_id.id
        original_journal = invoice.journal_id.id
        wizard.write({
            'invoice_line_ids': [(4, self.invoice_line_id11)]})
        invoice_new = wizard.split_invoice()
        invoice = invoice_obj.browse(self.invoice_id1)
        # Test if the lines have been exactly copied
        self.assertEqual(invoice.name, original_name)
        self.assertEqual(
            wizard.invoice_line_ids[0].invoice_id.partner_id.id,
            orginal_partner_id)
        self.assertEqual(
            invoice.partner_id.id, orginal_partner_id)
        self.assertEqual(
            wizard.invoice_line_ids[0].invoice_id.date_invoice,
            original_invoice_date)
        self.assertEqual(
            invoice.date_invoice, original_invoice_date)
        self.assertEqual(
            wizard.invoice_line_ids[0].invoice_id.account_id.id,
            original_account)
        self.assertEqual(
            invoice.account_id.id, original_account)
        self.assertEqual(
            wizard.invoice_line_ids[0].invoice_id.journal_id.id,
            original_journal)
        self.assertEqual(
            invoice.journal_id.id, original_journal)
        self.assertEqual(
            original_amount,
            invoice_new.amount_total + invoice.amount_total
        )

    def _create_invoice(self, invoice_name):
        """ Set the update_posted to True to make invoice cancelable """
        journal_obj = self.env['account.journal']
        journal = journal_obj.search([('id', '=', 1)])
        journal.write({
            'update_posted': True,
        })
        partner_obj = self.env['res.partner']
        partner_id = partner_obj.create({
            'name': 'Kevin',
        }).id
        account_id = self.env['account.account'].search(
            [('internal_type', '=', 'receivable')])[0].id
        invoice_obj = self.env['account.invoice']
        invoice_id = invoice_obj.create({
            'name': invoice_name,
            'account_id': account_id,
            'currency_id': 1,
            'journal_id': 1,
            'partner_id': partner_id,
            'date_invoice': datetime.today()
        }).id
        return invoice_id

    def _create_invoice_line(self, description, amount, invoice_id):
        """ Create invoice's lines """
        invl_obj = self.env['account.invoice.line'].with_context(
            journal_id=1)
        invoice_line_id = invl_obj.create({
            'name': description,
            'price_unit': amount,
            'invoice_id': invoice_id,
            'account_id': invl_obj._default_account()
        }).id
        return invoice_line_id
<|endoftext|>


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

import json
from itertools import groupby, chain
from operator import itemgetter

_MAX = 999999999

def sort_non_iterative(data_list, question_indexes=[], withdrawals=[], ties_sorting=[]):
    '''
    Sort non iterative questions of the first tally  by total_count

    - question_indexes are the questions this is applied to
    - withdrawals is a list of answer items that have been withdrawed.
    - ties_sorting is a list of answer items in order

    An answer items follows this format:
    {"question_index": 0, "answer_text": "Foo", "answer_id": 0}
    '''
    data = data_list[0]

    # append already listed withdrawals
    if 'withdrawals' in data:
        withdrawals = withdrawals + data['withdrawals']

    for q_num, question in enumerate(data['results']['questions']):
        # filter first
        if question['tally_type'] not in ["plurality-at-large", "borda", "borda-nauru", "pairwise-beta"] or\
            q_num not in question_indexes:
            continue

        # apply removals
        if "removed-candidates" in data:
            q_removed = [
                removed['answer_id']
                for removed in data["removed-candidates"]
                if removed['question_index'] == q_num]
            question['answers'][:] = [
                answer
                for answer in question['answers']
                if answer['id'] not in q_removed]

        q_withdrawals = list(filter(lambda w: w['question_index'] == q_num, withdrawals))
        q_withdrawals_ids = list(map(lambda w: w['answer_id'], q_withdrawals))
        q_ties_sorting = list(filter(lambda w: w['question_index'] == q_num, ties_sorting))

        # add default tie sort
        for answer in question['answers']:
            answer['tie_sort']  = 0

        # add tie sort index to each answer
        for i, item in enumerate(q_ties_sorting):
            item2 = question['answers'][item['answer_id']]

            # first do some checks
            assert item2['id'] == item['answer_id']
            assert item2['text'] == item['answer_text']
            # reverse numbering, to be compatible with total_count sorting
            item2['tie_sort'] = len(q_ties_sorting) - i

        # sanity check withdrawals
        for item in q_withdrawals:
            item2 = question['answers'][item['answer_id']]

            # first do some checks
            assert item2['id'] == item['answer_id']
            assert item2['text'] == item['answer_text']

        # first sort by id, to have a stable sort
        question['answers'] = sorted(question['answers'], key=itemgetter('id'))

        # then sort by total_count, resolving ties too
        question['answers'] = sorted(question['answers'], reverse=True,
            key=itemgetter('total_count', 'tie_sort'))

        # mark winners
        i = 0
        for answer in question['answers']:
            if answer['id'] in q_withdrawals_ids or i >= question['num_winners']:
                answer['winner_position'] = _MAX
            else:
                answer['winner_position'] = i
                i += 1

        # final sort based on winners
        question['answers'] = sorted(
            question['answers'],
            key=itemgetter('winner_position'))

        # remove temp data
        for answer in question['answers']:
            del answer['tie_sort']
            if answer['winner_position'] is _MAX:
                answer['winner_position'] = None<commit_msg>fixing support for removed candidates in sort<commit_after># -*- coding:utf-8 -*-

import json
from itertools import groupby, chain
from operator import itemgetter

_MAX = 999999999

def sort_non_iterative(data_list, question_indexes=[], withdrawals=[], ties_sorting=[]):
    '''
    Sort non iterative questions of the first tally  by total_count

    - question_indexes are the questions this is applied to
    - withdrawals is a list of answer items that have been withdrawed.
    - ties_sorting is a list of answer items in order

    An answer items follows this format:
    {"question_index": 0, "answer_text": "Foo", "answer_id": 0}
    '''
    data = data_list[0]

    # append already listed withdrawals
    if 'withdrawals' in data:
        withdrawals = withdrawals + data['withdrawals']

    for q_num, question in enumerate(data['results']['questions']):
        # filter first
        if question['tally_type'] not in ["plurality-at-large", "borda", "borda-nauru", "pairwise-beta"] or\
            q_num not in question_indexes:
            continue

        # apply removals
        q_removed = []
        if "removed-candidates" in data:
            q_removed = [
                removed['answer_id']
                for removed in data["removed-candidates"]
                if removed['question_index'] == q_num]
            question['answers'][:] = [
                answer
                for answer in question['answers']
                if answer['id'] not in q_removed]

        q_withdrawals = list(filter(lambda w: w['question_index'] == q_num, withdrawals))
        q_withdrawals_ids = list(map(lambda w: w['answer_id'], q_withdrawals))
        q_ties_sorting = list(filter(lambda w: w['question_index'] == q_num, ties_sorting))

        # add default tie sort
        for answer in question['answers']:
            answer['tie_sort']  = 0

        # add tie sort index to each answer
        for i, item in enumerate(q_ties_sorting):
            item2 = question['answers'][item['answer_id']]

            # first do some checks
            assert item2['id'] == item['answer_id']
            assert item2['text'] == item['answer_text']
            # reverse numbering, to be compatible with total_count sorting
            item2['tie_sort'] = len(q_ties_sorting) - i

        # sanity check withdrawals
        for item in q_withdrawals:
            if item['answer_id'] in q_removed:
                continue

            item2 = None
            for item_find in question['answers']:
                if item_find['id'] == item['answer_id']:
                    item2 = item_find
                    break

            # first do some checks
            assert item2 is not None
            assert item2['text'] == item['answer_text']

        # first sort by id, to have a stable sort
        question['answers'] = sorted(question['answers'], key=itemgetter('id'))

        # then sort by total_count, resolving ties too
        question['answers'] = sorted(question['answers'], reverse=True,
            key=itemgetter('total_count', 'tie_sort'))

        # mark winners
        i = 0
        for answer in question['answers']:
            if answer['id'] in q_withdrawals_ids or i >= question['num_winners']:
                answer['winner_position'] = _MAX
            else:
                answer['winner_position'] = i
                i += 1

        # final sort based on winners
        question['answers'] = sorted(
            question['answers'],
            key=itemgetter('winner_position'))

        # remove temp data
        for answer in question['answers']:
            del answer['tie_sort']
            if answer['winner_position'] is _MAX:
                answer['winner_position'] = None<|endoftext|>


<commit_before>#!/usr/bin/env python

import json

class PManager(object):
    def __init__(self, data):
        if isinstance(data, (str, unicode)):
            self.data = json.loads(data)
        else:
            self.data = data

        self.factor = 1
        self.unit = "MiB"
        self._pre = []
        self._kick = []
        self._post = []

        self._pcount = {}
        self._pend = {}
        self._rcount = 0
        self._pvcount = 0

    def pcount(self, disk_id, increment=0):
        self._pcount[disk_id] = self._pcount.get(disk_id, 0) + increment
        return self._pcount.get(disk_id, 0)

    def psize(self, disk_id, increment=0):
        self._pend[disk_id] = self._pend.get(disk_id, 0) + increment
        return self._pend.get(disk_id, 0)

    def rcount(self, increment=0):
        self._rcount += increment
        return self._rcount

    def pvcount(self, increment=0):
        self._pvcount += increment
        return self._pvcount

    def pre(self, command=None):
        if command:
            return self._pre.append(command)
        return self._pre

    def kick(self, command=None):
        if command:
            return self._kick.append(command)
        return self._kick

    def post(self, command=None):
        if command:
            return self._post.append(command)
        return self._post

    def _gettabfstype(self, vol):
        if vol["mount"] == "/":
            return "ext4"
        elif vol["mount"] == "/boot":
            return "ext2"
        elif vol["mount"] == "swap":
            return "swap"
        return "xfs"

    def _getfstype(self, vol):
        fstype = self._gettabfstype(vol)
        if fstype == "swap":
            return ""
        return "--fstype=%s" % fstype

    def _parttype(self, n):
        if n <= 3:
            return "primary"
        elif n == 4:
            return "extended"
        return "logical"

    def _getsize(self, vol):
        """Anaconda has hard coded limitation in 16TB
        for ext3/4 and xfs filesystems (the only filesystems
        we are supposed to use). Besides there is no stable
        64-bit ext4 implementation at the moment, so the
        limitation in 16TB for ext4 is not only
        anaconda limitation."""

        """Root partition can not be located on xfs file system
        therefore we check if root filesystem is larger
        than 16TB and set it size into 16TB if it is larger.
        It is necessary to note that to format 16TB
        volume on ext4 it is needed about 1G memory."""
        if vol["size"] > 16777216 and vol["mount"] == "/":
            return 16777216
        return vol["size"]

    def clean(self, disk):
        self.pre("test -e /dev/{0} && dd if=/dev/zero "
                        "of=/dev/{0} bs=1M count=10".format(disk["id"]))

    def gpt(self, disk):
        self.pre("parted -s /dev/{0} mklabel gpt".format(disk["id"]))

    def bootable(self, disk):
        """Create and mark Bios Boot partition to which grub will
        embed its code later, useable for legacy boot.
        May be way smaller, but be aware that the parted may
        shrink 1M partition to zero at some disks and versions."""
        self.pre("parted -a none -s /dev/{0} "
                 "unit {3} mkpart primary {1} {2}".format(
                     disk["id"],
                     self.psize(disk["id"]),
                     self.psize(disk["id"], 24 * self.factor),
                     self.unit
            )
        )
        self.pre("parted -s /dev/{0} set {1} bios_grub on".format(
                     disk["id"],
                     self.pcount(disk["id"], 1)
            )
        )

        """Create partition for the EFI boot, minimum
        size is 100M, recommended is 200M, with fat32 and
        future mountpoint in the /boot/efi. There is also
        '/usr/sbin/parted -s /dev/sda set 2 boot on'
        which is strictly needed for EFI boot."""
        self.pre("parted -a none -s /dev/{0} "
                 "unit {3} mkpart primary fat32 {1} {2}".format(
                     disk["id"],
                     self.psize(disk["id"]),
                     self.psize(disk["id"], 200 * self.factor),
                     self.unit
            )
        )
        self.pre("parted -s /dev/{0} set {1} boot on".format(
                     disk["id"],
                     self.pcount(disk["id"], 1)
            )
        )

    def boot(self):
        self.plains(volume_filter=lambda x: x["mount"] == "/boot")
        self.raids(volume_filter=lambda x: x["mount"] == "/boot")

    def notboot(self):
        self.plains(volume_filter=lambda x: x["mount"] != "/boot")
        self.raids(volume_filter=lambda x: x["mount"] != "/boot")

    def plains(self, volume_filter=None):
        if not volume_filter:
            volume_filter = lambda x: True
        for disk in [d for d in self.data if d["type"] == "disk"]:
            for part in filter(lambda p: p["type"] == "partition" and
                               volume_filter(p), disk["volumes"]):
                if part["size"] <= 0:
                    continue
                pcount = self.pcount(disk["id"], 1)
                self.pre("parted -a none -s /dev/{0} "
                         "unit {4} mkpart {1} {2} {3}".format(
                             disk["id"],
                             self._parttype(pcount),
                             self.psize(disk["id"]),
                             self.psize(disk["id"], part["size"] * self.factor),
                             self.unit))

                fstype = self._getfstype(part)
                size = self._getsize(part)
                tabmount = part["mount"] if part["mount"] != "swap" else "none"
                tabfstype = self._gettabfstype(part)
                if size > 0 and size <= 16777216:
                    self.kick("partition {0} "
                              "--onpart=$(readlink -f /dev/{2})"
                              "{3}".format(part["mount"], size,
                                           disk["id"], pcount))
                else:
                    if part["mount"] != "swap":
                        self.post("mkfs.{0} $(basename `readlink -f /dev/{1}`)"
                                  "{2}".format(tabfstype, disk["id"], pcount))
                        self.post("mkdir -p /mnt/sysimage{0}".format(
                            part["mount"]))
                    self.post("echo 'UUID=$(blkid -s UUID -o value "
                              "$(basename `readlink -f /dev/{0}`){1}) "
                              "{2} {3} defaults 0 0'"
                              " >> /mnt/sysimage/etc/fstab".format(
                                  disk["id"], pcount, tabmount, tabfstype))

    def raids(self, volume_filter=None):
        if not volume_filter:
            volume_filter = lambda x: True
        raids = {}
        for disk in [d for d in self.data if d["type"] == "disk"]:
            for raid in filter(lambda p: p["type"] == "raid" and
                               volume_filter(p), disk["volumes"]):
                if raid["size"] <= 0:
                    continue
                pcount = self.pcount(disk["id"], 1)
                rname = "raid.{0:03d}".format(self.rcount(1))
                begin_size = self.psize(disk["id"])
                end_size = self.psize(disk["id"], raid["size"] * self.factor)
                self.pre("parted -a none -s /dev/{0} "
                         "unit {4} mkpart {1} {2} {3}".format(
                             disk["id"], self._parttype(pcount),
                             begin_size, end_size, self.unit))
                self.kick("partition {0} "
                          "--onpart=$(readlink -f /dev/{2}){3}"
                          "".format(rname, raid["size"], disk["id"], pcount))

                if not raids.get(raid["mount"]):
                    raids[raid["mount"]] = []
                raids[raid["mount"]].append(rname)

        for (num, (mount, rnames)) in enumerate(raids.iteritems()):
            fstype = self._gettabfstype({"mount": mount})
            self.kick("raid {0} --device md{1} --fstype ext2 "
                      "--level=RAID1 {2}".format(mount, num, " ".join(rnames)))

    def pvs(self):
        pvs = {}
        for disk in [d for d in self.data if d["type"] == "disk"]:
            for pv in [p for p in disk["volumes"] if p["type"] == "pv"]:
                if pv["size"] <= 0:
                    continue
                pcount = self.pcount(disk["id"], 1)
                pvname = "pv.{0:03d}".format(self.pvcount(1))
                begin_size = self.psize(disk["id"])
                end_size = self.psize(disk["id"], pv["size"] * self.factor)
                self.pre("parted -a none -s /dev/{0} "
                         "unit {4} mkpart {1} {2} {3}".format(
                             disk["id"], self._parttype(pcount),
                             begin_size, end_size, self.unit))
                self.kick("partition {0} "
                          "--onpart=$(readlink -f /dev/{2}){3}"
                          "".format(pvname, pv["size"], disk["id"], pcount))

                if not pvs.get(pv["vg"]):
                    pvs[pv["vg"]] = []
                pvs[pv["vg"]].append(pvname)

        for vg, pvnames in pvs.iteritems():
            self.kick("volgroup {0} {1}".format(vg, " ".join(pvnames)))

    def lvs(self):
        for vg in [g for g in self.data if g["type"] == "vg"]:
            for lv in vg["volumes"]:
                if lv["size"] <= 0:
                    continue
                fstype = self._getfstype(lv)
                size = self._getsize(lv)
                tabmount = lv["mount"] if lv["mount"] != "swap" else "none"
                tabfstype = self._gettabfstype(lv)

                if size > 0 and size <= 16777216:
                    self.kick("logvol {0} --vgname={1} --size={2} "
                              "--name={3} {4}".format(
                                  lv["mount"], vg["id"], size,
                                  lv["name"], fstype))
                else:
                    self.post("lvcreate --size {0} --name {1} {2}".format(
                        size, lv["name"], vg["id"]))
                    if lv["mount"] != swap:
                        self.post("mkfs.{0} /dev/mapper/{1}-{2}".format(
                            tabfstype, vg["id"], lv["name"]))
                        self.post("mkdir -p /mnt/sysimage{0}"
                                  "".format(lv["mount"]))
                    self.post("echo '/dev/mapper/{0}-{1} {2} {3} defaults 0 0'"
                              " >> /mnt/sysimage/etc/fstab".format(
                                  vg["id"], lv["name"], tabmount, tabfstype))

    def bootloader(self):
        devs = []
        for disk in [d for d in self.data if d["type"] == "disk"]:
            devs.append("$(basename `readlink -f /dev/{0}`)"
                        "".format(disk["id"]))
        if devs:
            self.kick("bootloader --location=mbr --driveorder={0} "
                      "--append=' biosdevname=0 "
                      "crashkernel=none'".format(",".join(devs)))

    def expose(self,
               kickfile="/tmp/partition.ks",
               postfile="/tmp/post_partition.ks"
        ):
        result = ""
        for pre in self.pre():
            result += "{0}\n".format(pre)

        result += "echo > {0}\n".format(kickfile)
        for kick in self.kick():
            result += "echo \"{0}\" >> {1}\n".format(kick, kickfile)

        result += "echo > {0}\n".format(postfile)
        for post in self.post():
            result += "echo \"{0}\" >> {1}\n".format(post, postfile)
        return result

    def eval(self):
        for disk in [d for d in self.data if d["type"] == "disk"]:
            self.clean(disk)
            self.gpt(disk)
            self.bootable(disk)
        self.boot()
        self.notboot()
        self.pvs()
        self.lvs()
        self.bootloader()
        self.pre("sleep 10")
        for disk in [d for d in self.data if d["type"] == "disk"]:
            self.pre("hdparm -z /dev/{0}".format(disk["id"]))

def pm(data):
    pmanager = PManager(data)
    pmanager.eval()
    return pmanager.expose()

<commit_msg>Added replacement of '-' with '--' for vg and lv names<commit_after>#!/usr/bin/env python

import json

class PManager(object):
    def __init__(self, data):
        if isinstance(data, (str, unicode)):
            self.data = json.loads(data)
        else:
            self.data = data

        self.factor = 1
        self.unit = "MiB"
        self._pre = []
        self._kick = []
        self._post = []

        self._pcount = {}
        self._pend = {}
        self._rcount = 0
        self._pvcount = 0

    def pcount(self, disk_id, increment=0):
        self._pcount[disk_id] = self._pcount.get(disk_id, 0) + increment
        return self._pcount.get(disk_id, 0)

    def psize(self, disk_id, increment=0):
        self._pend[disk_id] = self._pend.get(disk_id, 0) + increment
        return self._pend.get(disk_id, 0)

    def rcount(self, increment=0):
        self._rcount += increment
        return self._rcount

    def pvcount(self, increment=0):
        self._pvcount += increment
        return self._pvcount

    def pre(self, command=None):
        if command:
            return self._pre.append(command)
        return self._pre

    def kick(self, command=None):
        if command:
            return self._kick.append(command)
        return self._kick

    def post(self, command=None):
        if command:
            return self._post.append(command)
        return self._post

    def _gettabfstype(self, vol):
        if vol["mount"] == "/":
            return "ext4"
        elif vol["mount"] == "/boot":
            return "ext2"
        elif vol["mount"] == "swap":
            return "swap"
        return "xfs"

    def _getfstype(self, vol):
        fstype = self._gettabfstype(vol)
        if fstype == "swap":
            return ""
        return "--fstype=%s" % fstype

    def _parttype(self, n):
        if n <= 3:
            return "primary"
        elif n == 4:
            return "extended"
        return "logical"

    def _getsize(self, vol):
        """Anaconda has hard coded limitation in 16TB
        for ext3/4 and xfs filesystems (the only filesystems
        we are supposed to use). Besides there is no stable
        64-bit ext4 implementation at the moment, so the
        limitation in 16TB for ext4 is not only
        anaconda limitation."""

        """Root partition can not be located on xfs file system
        therefore we check if root filesystem is larger
        than 16TB and set it size into 16TB if it is larger.
        It is necessary to note that to format 16TB
        volume on ext4 it is needed about 1G memory."""
        if vol["size"] > 16777216 and vol["mount"] == "/":
            return 16777216
        return vol["size"]

    def clean(self, disk):
        self.pre("test -e /dev/{0} && dd if=/dev/zero "
                        "of=/dev/{0} bs=1M count=10".format(disk["id"]))

    def gpt(self, disk):
        self.pre("parted -s /dev/{0} mklabel gpt".format(disk["id"]))

    def bootable(self, disk):
        """Create and mark Bios Boot partition to which grub will
        embed its code later, useable for legacy boot.
        May be way smaller, but be aware that the parted may
        shrink 1M partition to zero at some disks and versions."""
        self.pre("parted -a none -s /dev/{0} "
                 "unit {3} mkpart primary {1} {2}".format(
                     disk["id"],
                     self.psize(disk["id"]),
                     self.psize(disk["id"], 24 * self.factor),
                     self.unit
            )
        )
        self.pre("parted -s /dev/{0} set {1} bios_grub on".format(
                     disk["id"],
                     self.pcount(disk["id"], 1)
            )
        )

        """Create partition for the EFI boot, minimum
        size is 100M, recommended is 200M, with fat32 and
        future mountpoint in the /boot/efi. There is also
        '/usr/sbin/parted -s /dev/sda set 2 boot on'
        which is strictly needed for EFI boot."""
        self.pre("parted -a none -s /dev/{0} "
                 "unit {3} mkpart primary fat32 {1} {2}".format(
                     disk["id"],
                     self.psize(disk["id"]),
                     self.psize(disk["id"], 200 * self.factor),
                     self.unit
            )
        )
        self.pre("parted -s /dev/{0} set {1} boot on".format(
                     disk["id"],
                     self.pcount(disk["id"], 1)
            )
        )

    def boot(self):
        self.plains(volume_filter=lambda x: x["mount"] == "/boot")
        self.raids(volume_filter=lambda x: x["mount"] == "/boot")

    def notboot(self):
        self.plains(volume_filter=lambda x: x["mount"] != "/boot")
        self.raids(volume_filter=lambda x: x["mount"] != "/boot")

    def plains(self, volume_filter=None):
        if not volume_filter:
            volume_filter = lambda x: True
        for disk in [d for d in self.data if d["type"] == "disk"]:
            for part in filter(lambda p: p["type"] == "partition" and
                               volume_filter(p), disk["volumes"]):
                if part["size"] <= 0:
                    continue
                pcount = self.pcount(disk["id"], 1)
                self.pre("parted -a none -s /dev/{0} "
                         "unit {4} mkpart {1} {2} {3}".format(
                             disk["id"],
                             self._parttype(pcount),
                             self.psize(disk["id"]),
                             self.psize(disk["id"], part["size"] * self.factor),
                             self.unit))

                fstype = self._getfstype(part)
                size = self._getsize(part)
                tabmount = part["mount"] if part["mount"] != "swap" else "none"
                tabfstype = self._gettabfstype(part)
                if size > 0 and size <= 16777216:
                    self.kick("partition {0} "
                              "--onpart=$(readlink -f /dev/{2})"
                              "{3}".format(part["mount"], size,
                                           disk["id"], pcount))
                else:
                    if part["mount"] != "swap":
                        self.post("mkfs.{0} $(basename `readlink -f /dev/{1}`)"
                                  "{2}".format(tabfstype, disk["id"], pcount))
                        self.post("mkdir -p /mnt/sysimage{0}".format(
                            part["mount"]))
                    self.post("echo 'UUID=$(blkid -s UUID -o value "
                              "$(basename `readlink -f /dev/{0}`){1}) "
                              "{2} {3} defaults 0 0'"
                              " >> /mnt/sysimage/etc/fstab".format(
                                  disk["id"], pcount, tabmount, tabfstype))

    def raids(self, volume_filter=None):
        if not volume_filter:
            volume_filter = lambda x: True
        raids = {}
        for disk in [d for d in self.data if d["type"] == "disk"]:
            for raid in filter(lambda p: p["type"] == "raid" and
                               volume_filter(p), disk["volumes"]):
                if raid["size"] <= 0:
                    continue
                pcount = self.pcount(disk["id"], 1)
                rname = "raid.{0:03d}".format(self.rcount(1))
                begin_size = self.psize(disk["id"])
                end_size = self.psize(disk["id"], raid["size"] * self.factor)
                self.pre("parted -a none -s /dev/{0} "
                         "unit {4} mkpart {1} {2} {3}".format(
                             disk["id"], self._parttype(pcount),
                             begin_size, end_size, self.unit))
                self.kick("partition {0} "
                          "--onpart=$(readlink -f /dev/{2}){3}"
                          "".format(rname, raid["size"], disk["id"], pcount))

                if not raids.get(raid["mount"]):
                    raids[raid["mount"]] = []
                raids[raid["mount"]].append(rname)

        for (num, (mount, rnames)) in enumerate(raids.iteritems()):
            fstype = self._gettabfstype({"mount": mount})
            self.kick("raid {0} --device md{1} --fstype ext2 "
                      "--level=RAID1 {2}".format(mount, num, " ".join(rnames)))

    def pvs(self):
        pvs = {}
        for disk in [d for d in self.data if d["type"] == "disk"]:
            for pv in [p for p in disk["volumes"] if p["type"] == "pv"]:
                if pv["size"] <= 0:
                    continue
                pcount = self.pcount(disk["id"], 1)
                pvname = "pv.{0:03d}".format(self.pvcount(1))
                begin_size = self.psize(disk["id"])
                end_size = self.psize(disk["id"], pv["size"] * self.factor)
                self.pre("parted -a none -s /dev/{0} "
                         "unit {4} mkpart {1} {2} {3}".format(
                             disk["id"], self._parttype(pcount),
                             begin_size, end_size, self.unit))
                self.kick("partition {0} "
                          "--onpart=$(readlink -f /dev/{2}){3}"
                          "".format(pvname, pv["size"], disk["id"], pcount))

                if not pvs.get(pv["vg"]):
                    pvs[pv["vg"]] = []
                pvs[pv["vg"]].append(pvname)

        for vg, pvnames in pvs.iteritems():
            self.kick("volgroup {0} {1}".format(vg, " ".join(pvnames)))

    def lvs(self):
        for vg in [g for g in self.data if g["type"] == "vg"]:
            for lv in vg["volumes"]:
                if lv["size"] <= 0:
                    continue
                fstype = self._getfstype(lv)
                size = self._getsize(lv)
                tabmount = lv["mount"] if lv["mount"] != "swap" else "none"
                tabfstype = self._gettabfstype(lv)

                if size > 0 and size <= 16777216:
                    self.kick("logvol {0} --vgname={1} --size={2} "
                              "--name={3} {4}".format(
                                  lv["mount"], vg["id"], size,
                                  lv["name"], fstype))
                else:
                    self.post("lvcreate --size {0} --name {1} {2}".format(
                        size, lv["name"], vg["id"]))
                    if lv["mount"] != swap:
                        self.post("mkfs.{0} /dev/mapper/{1}-{2}".format(
                            tabfstype, vg["id"], lv["name"]))
                        self.post("mkdir -p /mnt/sysimage{0}"
                                  "".format(lv["mount"]))
                    """
                    The name of the device. An LVM device is
                    expressed as the volume group name and the logical
                    volume name separated by a hyphen. A hyphen in
                    the original name is translated to two hyphens.
                    """
                    self.post("echo '/dev/mapper/{0}-{1} {2} {3} defaults 0 0'"
                              " >> /mnt/sysimage/etc/fstab".format(
                                  vg["id"].replace("-", "--"),
                                  lv["name"].replace("-", "--"),
                                  tabmount, tabfstype))

    def bootloader(self):
        devs = []
        for disk in [d for d in self.data if d["type"] == "disk"]:
            devs.append("$(basename `readlink -f /dev/{0}`)"
                        "".format(disk["id"]))
        if devs:
            self.kick("bootloader --location=mbr --driveorder={0} "
                      "--append=' biosdevname=0 "
                      "crashkernel=none'".format(",".join(devs)))

    def expose(self,
               kickfile="/tmp/partition.ks",
               postfile="/tmp/post_partition.ks"
        ):
        result = ""
        for pre in self.pre():
            result += "{0}\n".format(pre)

        result += "echo > {0}\n".format(kickfile)
        for kick in self.kick():
            result += "echo \"{0}\" >> {1}\n".format(kick, kickfile)

        result += "echo > {0}\n".format(postfile)
        for post in self.post():
            result += "echo \"{0}\" >> {1}\n".format(post, postfile)
        return result

    def eval(self):
        for disk in [d for d in self.data if d["type"] == "disk"]:
            self.clean(disk)
            self.gpt(disk)
            self.bootable(disk)
        self.boot()
        self.notboot()
        self.pvs()
        self.lvs()
        self.bootloader()
        self.pre("sleep 10")
        for disk in [d for d in self.data if d["type"] == "disk"]:
            self.pre("hdparm -z /dev/{0}".format(disk["id"]))

def pm(data):
    pmanager = PManager(data)
    pmanager.eval()
    return pmanager.expose()

<|endoftext|>


<commit_before>from __future__ import absolute_import

from celery import shared_task
from .models import Task

@shared_task
def task_run(id):
    task = Task.objects.get(id=id)
    task.run()

@shared_task
def add(x, y):
    return x + y

@shared_task
def mul(x, y):
    return x * y

@shared_task
def xsum(numbers):
    return sum(numbers)<commit_msg>begin atomic experience task<commit_after>from __future__ import absolute_import

from celery import shared_task
from .models import Item, Selection, Preset, Task

@shared_task
def task_run(id):
    task = Task.objects.get(id=id)
    task.run()

@shared_task
def experience_run(experience_id, item_id):
    item = Item.objects.get(id=item_id)
    experience = Experience.objects.get(id=experience_id)
    experience.run(item)

@shared_task
def add(x, y):
    return x + y

@shared_task
def mul(x, y):
    return x * y

@shared_task
def xsum(numbers):
    return sum(numbers)<|endoftext|>


<commit_before>
from django.contrib.auth.models import User, Group
from django.core.exceptions import PermissionDenied
from django import template
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.utils.html import escape
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext, ugettext_lazy as _
from django.contrib.auth.forms import UserCreationForm, AdminPasswordChangeForm
from django.contrib import admin

class GroupAdmin(admin.ModelAdmin):
    search_fields = ('name',)
    ordering = ('name',)
    filter_horizontal = ('permissions',)

class UserAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
        (_('Permissions'), {'fields': ('is_staff', 'is_active', 'is_superuser', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
        (_('Groups'), {'fields': ('groups',)}),
    )
    add_form = UserCreationForm
    change_password_form = AdminPasswordChangeForm
    list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff')
    list_filter = ('is_staff', 'is_superuser')
    search_fields = ('username', 'first_name', 'last_name', 'email')
    ordering = ('username',)
    filter_horizontal = ('user_permissions',)

    def __call__(self, request, url):
        # this should not be here, but must be due to the way __call__ routes
        # in ModelAdmin.
        if url is None:
            return self.changelist_view(request)
        if url.endswith('password'):
            return self.user_change_password(request, url.split('/')[0])
        return super(UserAdmin, self).__call__(request, url)

    def add_view(self, request):
        if not self.has_change_permission(request):
            raise PermissionDenied
        if request.method == 'POST':
            form = self.add_form(request.POST)
            if form.is_valid():
                new_user = form.save()
                msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': 'user', 'obj': new_user}
                self.log_addition(request, new_user)
                if "_addanother" in request.POST:
                    request.user.message_set.create(message=msg)
                    return HttpResponseRedirect(request.path)
                else:
                    request.user.message_set.create(message=msg + ' ' + ugettext("You may edit it again below."))
                    return HttpResponseRedirect('../%s/' % new_user.id)
        else:
            form = self.add_form()
        return render_to_response('admin/auth/user/add_form.html', {
            'title': _('Add user'),
            'form': form,
            'is_popup': '_popup' in request.REQUEST,
            'add': True,
            'change': False,
            'has_add_permission': True,
            'has_delete_permission': False,
            'has_change_permission': True,
            'has_file_field': False,
            'has_absolute_url': False,
            'auto_populated_fields': (),
            'opts': self.model._meta,
            'save_as': False,
            'username_help_text': self.model._meta.get_field('username').help_text,
            'root_path': self.admin_site.root_path,
        }, context_instance=template.RequestContext(request))

    def user_change_password(self, request, id):
        if not request.user.has_perm('auth.change_user'):
            raise PermissionDenied
        user = get_object_or_404(self.model, pk=id)
        if request.method == 'POST':
            form = self.change_password_form(user, request.POST)
            if form.is_valid():
                new_user = form.save()
                msg = ugettext('Password changed successfully.')
                request.user.message_set.create(message=msg)
                return HttpResponseRedirect('..')
        else:
            form = self.change_password_form(user)
        return render_to_response('admin/auth/user/change_password.html', {
            'title': _('Change password: %s') % escape(user.username),
            'form': form,
            'is_popup': '_popup' in request.REQUEST,
            'add': True,
            'change': False,
            'has_delete_permission': False,
            'has_change_permission': True,
            'has_absolute_url': False,
            'opts': self.model._meta,
            'original': user,
            'save_as': False,
            'show_save': True,
            'root_path': self.admin_site.root_path,
        }, context_instance=RequestContext(request))

admin.site.register(Group, GroupAdmin)
admin.site.register(User, UserAdmin)

<commit_msg>Added app_label to the context for the add user admin view.<commit_after>
from django.contrib.auth.models import User, Group
from django.core.exceptions import PermissionDenied
from django import template
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.utils.html import escape
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext, ugettext_lazy as _
from django.contrib.auth.forms import UserCreationForm, AdminPasswordChangeForm
from django.contrib import admin

class GroupAdmin(admin.ModelAdmin):
    search_fields = ('name',)
    ordering = ('name',)
    filter_horizontal = ('permissions',)

class UserAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
        (_('Permissions'), {'fields': ('is_staff', 'is_active', 'is_superuser', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
        (_('Groups'), {'fields': ('groups',)}),
    )
    add_form = UserCreationForm
    change_password_form = AdminPasswordChangeForm
    list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff')
    list_filter = ('is_staff', 'is_superuser')
    search_fields = ('username', 'first_name', 'last_name', 'email')
    ordering = ('username',)
    filter_horizontal = ('user_permissions',)

    def __call__(self, request, url):
        # this should not be here, but must be due to the way __call__ routes
        # in ModelAdmin.
        if url is None:
            return self.changelist_view(request)
        if url.endswith('password'):
            return self.user_change_password(request, url.split('/')[0])
        return super(UserAdmin, self).__call__(request, url)

    def add_view(self, request):
        if not self.has_change_permission(request):
            raise PermissionDenied
        if request.method == 'POST':
            form = self.add_form(request.POST)
            if form.is_valid():
                new_user = form.save()
                msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': 'user', 'obj': new_user}
                self.log_addition(request, new_user)
                if "_addanother" in request.POST:
                    request.user.message_set.create(message=msg)
                    return HttpResponseRedirect(request.path)
                else:
                    request.user.message_set.create(message=msg + ' ' + ugettext("You may edit it again below."))
                    return HttpResponseRedirect('../%s/' % new_user.id)
        else:
            form = self.add_form()
        return render_to_response('admin/auth/user/add_form.html', {
            'title': _('Add user'),
            'form': form,
            'is_popup': '_popup' in request.REQUEST,
            'add': True,
            'change': False,
            'has_add_permission': True,
            'has_delete_permission': False,
            'has_change_permission': True,
            'has_file_field': False,
            'has_absolute_url': False,
            'auto_populated_fields': (),
            'opts': self.model._meta,
            'save_as': False,
            'username_help_text': self.model._meta.get_field('username').help_text,
            'root_path': self.admin_site.root_path,
            'app_label': self.model._meta.app_label,
        }, context_instance=template.RequestContext(request))

    def user_change_password(self, request, id):
        if not request.user.has_perm('auth.change_user'):
            raise PermissionDenied
        user = get_object_or_404(self.model, pk=id)
        if request.method == 'POST':
            form = self.change_password_form(user, request.POST)
            if form.is_valid():
                new_user = form.save()
                msg = ugettext('Password changed successfully.')
                request.user.message_set.create(message=msg)
                return HttpResponseRedirect('..')
        else:
            form = self.change_password_form(user)
        return render_to_response('admin/auth/user/change_password.html', {
            'title': _('Change password: %s') % escape(user.username),
            'form': form,
            'is_popup': '_popup' in request.REQUEST,
            'add': True,
            'change': False,
            'has_delete_permission': False,
            'has_change_permission': True,
            'has_absolute_url': False,
            'opts': self.model._meta,
            'original': user,
            'save_as': False,
            'show_save': True,
            'root_path': self.admin_site.root_path,
        }, context_instance=RequestContext(request))

admin.site.register(Group, GroupAdmin)
admin.site.register(User, UserAdmin)

<|endoftext|>


<commit_before>import asyncio
import logging
import sys
import tempfile
import traceback

from asgiref.sync import sync_to_async

from django.conf import settings
from django.core import signals
from django.core.exceptions import RequestAborted, RequestDataTooBig
from django.core.handlers import base
from django.http import (
    FileResponse, HttpRequest, HttpResponse, HttpResponseBadRequest,
    HttpResponseServerError, QueryDict, parse_cookie,
)
from django.urls import set_script_prefix
from django.utils.functional import cached_property

logger = logging.getLogger('django.request')

class ASGIRequest(HttpRequest):
    """
    Custom request subclass that decodes from an ASGI-standard request dict
    and wraps request body handling.
    """
    # Number of seconds until a Request gives up on trying to read a request
    # body and aborts.
    body_receive_timeout = 60

    def __init__(self, scope, body_file):
        self.scope = scope
        self._post_parse_error = False
        self._read_started = False
        self.resolver_match = None
        self.script_name = self.scope.get('root_path', '')
        if self.script_name and scope['path'].startswith(self.script_name):
            # TODO: Better is-prefix checking, slash handling?
            self.path_info = scope['path'][len(self.script_name):]
        else:
            self.path_info = scope['path']
        # The Django path is different from ASGI scope path args, it should
        # combine with script name.
        if self.script_name:
            self.path = '%s/%s' % (
                self.script_name.rstrip('/'),
                self.path_info.replace('/', '', 1),
            )
        else:
            self.path = scope['path']
        # HTTP basics.
        self.method = self.scope['method'].upper()
        # Ensure query string is encoded correctly.
        query_string = self.scope.get('query_string', '')
        if isinstance(query_string, bytes):
            query_string = query_string.decode()
        self.META = {
            'REQUEST_METHOD': self.method,
            'QUERY_STRING': query_string,
            'SCRIPT_NAME': self.script_name,
            'PATH_INFO': self.path_info,
            # WSGI-expecting code will need these for a while
            'wsgi.multithread': True,
            'wsgi.multiprocess': True,
        }
        if self.scope.get('client'):
            self.META['REMOTE_ADDR'] = self.scope['client'][0]
            self.META['REMOTE_HOST'] = self.META['REMOTE_ADDR']
            self.META['REMOTE_PORT'] = self.scope['client'][1]
        if self.scope.get('server'):
            self.META['SERVER_NAME'] = self.scope['server'][0]
            self.META['SERVER_PORT'] = str(self.scope['server'][1])
        else:
            self.META['SERVER_NAME'] = 'unknown'
            self.META['SERVER_PORT'] = '0'
        # Headers go into META.
        for name, value in self.scope.get('headers', []):
            name = name.decode('latin1')
            if name == 'content-length':
                corrected_name = 'CONTENT_LENGTH'
            elif name == 'content-type':
                corrected_name = 'CONTENT_TYPE'
            else:
                corrected_name = 'HTTP_%s' % name.upper().replace('-', '_')
            # HTTP/2 say only ASCII chars are allowed in headers, but decode
            # latin1 just in case.
            value = value.decode('latin1')
            if corrected_name in self.META:
                value = self.META[corrected_name] + ',' + value
            self.META[corrected_name] = value
        # Pull out request encoding, if provided.
        self._set_content_type_params(self.META)
        # Directly assign the body file to be our stream.
        self._stream = body_file
        # Other bits.
        self.resolver_match = None

    @cached_property
    def GET(self):
        return QueryDict(self.META['QUERY_STRING'])

    def _get_scheme(self):
        return self.scope.get('scheme') or super()._get_scheme()

    def _get_post(self):
        if not hasattr(self, '_post'):
            self._load_post_and_files()
        return self._post

    def _set_post(self, post):
        self._post = post

    def _get_files(self):
        if not hasattr(self, '_files'):
            self._load_post_and_files()
        return self._files

    POST = property(_get_post, _set_post)
    FILES = property(_get_files)

    @cached_property
    def COOKIES(self):
        return parse_cookie(self.META.get('HTTP_COOKIE', ''))

class ASGIHandler(base.BaseHandler):
    """Handler for ASGI requests."""
    request_class = ASGIRequest
    # Size to chunk response bodies into for multiple response messages.
    chunk_size = 2 ** 16

    def __init__(self):
        super(ASGIHandler, self).__init__()
        self.load_middleware()

    async def __call__(self, scope, receive, send):
        """
        Async entrypoint - parses the request and hands off to get_response.
        """
        # Serve only HTTP connections.
        # FIXME: Allow to override this.
        if scope['type'] != 'http':
            raise ValueError(
                'Django can only handle ASGI/HTTP connections, not %s.'
                % scope['type']
            )
        # Receive the HTTP request body as a stream object.
        try:
            body_file = await self.read_body(receive)
        except RequestAborted:
            return
        # Request is complete and can be served.
        set_script_prefix(self.get_script_prefix(scope))
        await sync_to_async(signals.request_started.send)(sender=self.__class__, scope=scope)
        # Get the request and check for basic issues.
        request, error_response = self.create_request(scope, body_file)
        if request is None:
            await self.send_response(error_response, send)
            return
        # Get the response, using a threadpool via sync_to_async, if needed.
        if asyncio.iscoroutinefunction(self.get_response):
            response = await self.get_response(request)
        else:
            # If get_response is synchronous, run it non-blocking.
            response = await sync_to_async(self.get_response)(request)
        response._handler_class = self.__class__
        # Increase chunk size on file responses (ASGI servers handles low-level
        # chunking).
        if isinstance(response, FileResponse):
            response.block_size = self.chunk_size
        # Send the response.
        await self.send_response(response, send)

    async def read_body(self, receive):
        """Reads a HTTP body from an ASGI connection."""
        # Use the tempfile that auto rolls-over to a disk file as it fills up.
        body_file = tempfile.SpooledTemporaryFile(max_size=settings.FILE_UPLOAD_MAX_MEMORY_SIZE, mode='w+b')
        while True:
            message = await receive()
            if message['type'] == 'http.disconnect':
                # Early client disconnect.
                raise RequestAborted()
            # Add a body chunk from the message, if provided.
            if 'body' in message:
                body_file.write(message['body'])
            # Quit out if that's the end.
            if not message.get('more_body', False):
                break
        body_file.seek(0)
        return body_file

    def create_request(self, scope, body_file):
        """
        Create the Request object and returns either (request, None) or
        (None, response) if there is an error response.
        """
        try:
            return self.request_class(scope, body_file), None
        except UnicodeDecodeError:
            logger.warning(
                'Bad Request (UnicodeDecodeError)',
                exc_info=sys.exc_info(),
                extra={'status_code': 400},
            )
            return None, HttpResponseBadRequest()
        except RequestDataTooBig:
            return None, HttpResponse('413 Payload too large', status=413)

    def handle_uncaught_exception(self, request, resolver, exc_info):
        """Last-chance handler for exceptions."""
        # There's no WSGI server to catch the exception further up
        # if this fails, so translate it into a plain text response.
        try:
            return super().handle_uncaught_exception(request, resolver, exc_info)
        except Exception:
            return HttpResponseServerError(
                traceback.format_exc() if settings.DEBUG else 'Internal Server Error',
                content_type='text/plain',
            )

    async def send_response(self, response, send):
        """Encode and send a response out over ASGI."""
        # Collect cookies into headers. Have to preserve header case as there
        # are some non-RFC compliant clients that require e.g. Content-Type.
        response_headers = []
        for header, value in response.items():
            if isinstance(header, str):
                header = header.encode('ascii')
            if isinstance(value, str):
                value = value.encode('latin1')
            response_headers.append((bytes(header), bytes(value)))
        for c in response.cookies.values():
            response_headers.append(
                (b'Set-Cookie', c.output(header='').encode('ascii').strip())
            )
        # Initial response message.
        await send({
            'type': 'http.response.start',
            'status': response.status_code,
            'headers': response_headers,
        })
        # Streaming responses need to be pinned to their iterator.
        if response.streaming:
            # Access `__iter__` and not `streaming_content` directly in case
            # it has been overridden in a subclass.
            for part in response:
                for chunk, _ in self.chunk_bytes(part):
                    await send({
                        'type': 'http.response.body',
                        'body': chunk,
                        # Ignore "more" as there may be more parts; instead,
                        # use an empty final closing message with False.
                        'more_body': True,
                    })
            # Final closing message.
            await send({'type': 'http.response.body'})
        # Other responses just need chunking.
        else:
            # Yield chunks of response.
            for chunk, last in self.chunk_bytes(response.content):
                await send({
                    'type': 'http.response.body',
                    'body': chunk,
                    'more_body': not last,
                })
        response.close()

    @classmethod
    def chunk_bytes(cls, data):
        """
        Chunks some data up so it can be sent in reasonable size messages.
        Yields (chunk, last_chunk) tuples.
        """
        position = 0
        if not data:
            yield data, True
            return
        while position < len(data):
            yield (
                data[position:position + cls.chunk_size],
                (position + cls.chunk_size) >= len(data),
            )
            position += cls.chunk_size

    def get_script_prefix(self, scope):
        """
        Return the script prefix to use from either the scope or a setting.
        """
        if settings.FORCE_SCRIPT_NAME:
            return settings.FORCE_SCRIPT_NAME
        return scope.get('root_path', '') or ''
<commit_msg>Refs #23919 -- Replaced super(ASGIHandler, self) with super().<commit_after>import asyncio
import logging
import sys
import tempfile
import traceback

from asgiref.sync import sync_to_async

from django.conf import settings
from django.core import signals
from django.core.exceptions import RequestAborted, RequestDataTooBig
from django.core.handlers import base
from django.http import (
    FileResponse, HttpRequest, HttpResponse, HttpResponseBadRequest,
    HttpResponseServerError, QueryDict, parse_cookie,
)
from django.urls import set_script_prefix
from django.utils.functional import cached_property

logger = logging.getLogger('django.request')

class ASGIRequest(HttpRequest):
    """
    Custom request subclass that decodes from an ASGI-standard request dict
    and wraps request body handling.
    """
    # Number of seconds until a Request gives up on trying to read a request
    # body and aborts.
    body_receive_timeout = 60

    def __init__(self, scope, body_file):
        self.scope = scope
        self._post_parse_error = False
        self._read_started = False
        self.resolver_match = None
        self.script_name = self.scope.get('root_path', '')
        if self.script_name and scope['path'].startswith(self.script_name):
            # TODO: Better is-prefix checking, slash handling?
            self.path_info = scope['path'][len(self.script_name):]
        else:
            self.path_info = scope['path']
        # The Django path is different from ASGI scope path args, it should
        # combine with script name.
        if self.script_name:
            self.path = '%s/%s' % (
                self.script_name.rstrip('/'),
                self.path_info.replace('/', '', 1),
            )
        else:
            self.path = scope['path']
        # HTTP basics.
        self.method = self.scope['method'].upper()
        # Ensure query string is encoded correctly.
        query_string = self.scope.get('query_string', '')
        if isinstance(query_string, bytes):
            query_string = query_string.decode()
        self.META = {
            'REQUEST_METHOD': self.method,
            'QUERY_STRING': query_string,
            'SCRIPT_NAME': self.script_name,
            'PATH_INFO': self.path_info,
            # WSGI-expecting code will need these for a while
            'wsgi.multithread': True,
            'wsgi.multiprocess': True,
        }
        if self.scope.get('client'):
            self.META['REMOTE_ADDR'] = self.scope['client'][0]
            self.META['REMOTE_HOST'] = self.META['REMOTE_ADDR']
            self.META['REMOTE_PORT'] = self.scope['client'][1]
        if self.scope.get('server'):
            self.META['SERVER_NAME'] = self.scope['server'][0]
            self.META['SERVER_PORT'] = str(self.scope['server'][1])
        else:
            self.META['SERVER_NAME'] = 'unknown'
            self.META['SERVER_PORT'] = '0'
        # Headers go into META.
        for name, value in self.scope.get('headers', []):
            name = name.decode('latin1')
            if name == 'content-length':
                corrected_name = 'CONTENT_LENGTH'
            elif name == 'content-type':
                corrected_name = 'CONTENT_TYPE'
            else:
                corrected_name = 'HTTP_%s' % name.upper().replace('-', '_')
            # HTTP/2 say only ASCII chars are allowed in headers, but decode
            # latin1 just in case.
            value = value.decode('latin1')
            if corrected_name in self.META:
                value = self.META[corrected_name] + ',' + value
            self.META[corrected_name] = value
        # Pull out request encoding, if provided.
        self._set_content_type_params(self.META)
        # Directly assign the body file to be our stream.
        self._stream = body_file
        # Other bits.
        self.resolver_match = None

    @cached_property
    def GET(self):
        return QueryDict(self.META['QUERY_STRING'])

    def _get_scheme(self):
        return self.scope.get('scheme') or super()._get_scheme()

    def _get_post(self):
        if not hasattr(self, '_post'):
            self._load_post_and_files()
        return self._post

    def _set_post(self, post):
        self._post = post

    def _get_files(self):
        if not hasattr(self, '_files'):
            self._load_post_and_files()
        return self._files

    POST = property(_get_post, _set_post)
    FILES = property(_get_files)

    @cached_property
    def COOKIES(self):
        return parse_cookie(self.META.get('HTTP_COOKIE', ''))

class ASGIHandler(base.BaseHandler):
    """Handler for ASGI requests."""
    request_class = ASGIRequest
    # Size to chunk response bodies into for multiple response messages.
    chunk_size = 2 ** 16

    def __init__(self):
        super().__init__()
        self.load_middleware()

    async def __call__(self, scope, receive, send):
        """
        Async entrypoint - parses the request and hands off to get_response.
        """
        # Serve only HTTP connections.
        # FIXME: Allow to override this.
        if scope['type'] != 'http':
            raise ValueError(
                'Django can only handle ASGI/HTTP connections, not %s.'
                % scope['type']
            )
        # Receive the HTTP request body as a stream object.
        try:
            body_file = await self.read_body(receive)
        except RequestAborted:
            return
        # Request is complete and can be served.
        set_script_prefix(self.get_script_prefix(scope))
        await sync_to_async(signals.request_started.send)(sender=self.__class__, scope=scope)
        # Get the request and check for basic issues.
        request, error_response = self.create_request(scope, body_file)
        if request is None:
            await self.send_response(error_response, send)
            return
        # Get the response, using a threadpool via sync_to_async, if needed.
        if asyncio.iscoroutinefunction(self.get_response):
            response = await self.get_response(request)
        else:
            # If get_response is synchronous, run it non-blocking.
            response = await sync_to_async(self.get_response)(request)
        response._handler_class = self.__class__
        # Increase chunk size on file responses (ASGI servers handles low-level
        # chunking).
        if isinstance(response, FileResponse):
            response.block_size = self.chunk_size
        # Send the response.
        await self.send_response(response, send)

    async def read_body(self, receive):
        """Reads a HTTP body from an ASGI connection."""
        # Use the tempfile that auto rolls-over to a disk file as it fills up.
        body_file = tempfile.SpooledTemporaryFile(max_size=settings.FILE_UPLOAD_MAX_MEMORY_SIZE, mode='w+b')
        while True:
            message = await receive()
            if message['type'] == 'http.disconnect':
                # Early client disconnect.
                raise RequestAborted()
            # Add a body chunk from the message, if provided.
            if 'body' in message:
                body_file.write(message['body'])
            # Quit out if that's the end.
            if not message.get('more_body', False):
                break
        body_file.seek(0)
        return body_file

    def create_request(self, scope, body_file):
        """
        Create the Request object and returns either (request, None) or
        (None, response) if there is an error response.
        """
        try:
            return self.request_class(scope, body_file), None
        except UnicodeDecodeError:
            logger.warning(
                'Bad Request (UnicodeDecodeError)',
                exc_info=sys.exc_info(),
                extra={'status_code': 400},
            )
            return None, HttpResponseBadRequest()
        except RequestDataTooBig:
            return None, HttpResponse('413 Payload too large', status=413)

    def handle_uncaught_exception(self, request, resolver, exc_info):
        """Last-chance handler for exceptions."""
        # There's no WSGI server to catch the exception further up
        # if this fails, so translate it into a plain text response.
        try:
            return super().handle_uncaught_exception(request, resolver, exc_info)
        except Exception:
            return HttpResponseServerError(
                traceback.format_exc() if settings.DEBUG else 'Internal Server Error',
                content_type='text/plain',
            )

    async def send_response(self, response, send):
        """Encode and send a response out over ASGI."""
        # Collect cookies into headers. Have to preserve header case as there
        # are some non-RFC compliant clients that require e.g. Content-Type.
        response_headers = []
        for header, value in response.items():
            if isinstance(header, str):
                header = header.encode('ascii')
            if isinstance(value, str):
                value = value.encode('latin1')
            response_headers.append((bytes(header), bytes(value)))
        for c in response.cookies.values():
            response_headers.append(
                (b'Set-Cookie', c.output(header='').encode('ascii').strip())
            )
        # Initial response message.
        await send({
            'type': 'http.response.start',
            'status': response.status_code,
            'headers': response_headers,
        })
        # Streaming responses need to be pinned to their iterator.
        if response.streaming:
            # Access `__iter__` and not `streaming_content` directly in case
            # it has been overridden in a subclass.
            for part in response:
                for chunk, _ in self.chunk_bytes(part):
                    await send({
                        'type': 'http.response.body',
                        'body': chunk,
                        # Ignore "more" as there may be more parts; instead,
                        # use an empty final closing message with False.
                        'more_body': True,
                    })
            # Final closing message.
            await send({'type': 'http.response.body'})
        # Other responses just need chunking.
        else:
            # Yield chunks of response.
            for chunk, last in self.chunk_bytes(response.content):
                await send({
                    'type': 'http.response.body',
                    'body': chunk,
                    'more_body': not last,
                })
        response.close()

    @classmethod
    def chunk_bytes(cls, data):
        """
        Chunks some data up so it can be sent in reasonable size messages.
        Yields (chunk, last_chunk) tuples.
        """
        position = 0
        if not data:
            yield data, True
            return
        while position < len(data):
            yield (
                data[position:position + cls.chunk_size],
                (position + cls.chunk_size) >= len(data),
            )
            position += cls.chunk_size

    def get_script_prefix(self, scope):
        """
        Return the script prefix to use from either the scope or a setting.
        """
        if settings.FORCE_SCRIPT_NAME:
            return settings.FORCE_SCRIPT_NAME
        return scope.get('root_path', '') or ''
<|endoftext|>


<commit_before>IOC_TO_ISO = {
    'AFG': 'AF',
    'ALB': 'AL',
    'ALG': 'DZ',
    'AND': 'AD',
    'ANG': 'AO',
    'ANT': 'AG',
    'ARG': 'AR',
    'ARM': 'AM',
    'ARU': 'AW',
    'ASA': 'AS',
    'AUS': 'AU',
    'AUT': 'AT',
    'AZE': 'AZ',
    'BAH': 'BS',
    'BAN': 'BD',
    'BAR': 'BB',
    'BDI': 'BI',
    'BEL': 'BE',
    'BEN': 'BJ',
    'BER': 'BM',
    'BHU': 'BT',
    'BIH': 'BA',
    'BIZ': 'BZ',
    'BLR': 'BY',
    'BOL': 'BO',
    'BOT': 'BW',
    'BRA': 'BR',
    'BRN': 'BH',
    'BRU': 'BN',
    'BUL': 'BG',
    'BUR': 'BF',
    'CAF': 'CF',
    'CAM': 'KH',
    'CAN': 'CA',
    'CAY': 'KY',
    'CGO': 'CG',
    'CHA': 'TD',
    'CHI': 'CL',
    'CHN': 'CN',
    'CIV': 'CI',
    'CMR': 'CM',
    'COD': 'CD',
    'COK': 'CK',
    'COL': 'CO',
    'COM': 'KM',
    'CPV': 'CV',
    'CRC': 'CR',
    'CRO': 'HR',
    'CUB': 'CU',
    'CYP': 'CY',
    'CZE': 'CZ',
    'DEN': 'DK',
    'DJI': 'DJ',
    'DMA': 'DM',
    'DOM': 'DO',
    'ECU': 'EC',
    'EGY': 'EG',
    'ERI': 'ER',
    'ESA': 'SV',
    'ESP': 'ES',
    'EST': 'EE',
    'ETH': 'ET',
    'FIJ': 'FJ',
    'FIN': 'FI',
    'FRA': 'FR',
    'FSM': 'FM',
    'GAB': 'GA',
    'GAM': 'GM',
    'GBR': 'GB',
    'GBS': 'GW',
    'GEO': 'GE',
    'GEQ': 'GQ',
    'GER': 'DE',
    'GHA': 'GH',
    'GRE': 'GR',
    'GRN': 'GD',
    'GUA': 'GT',
    'GUI': 'GN',
    'GUM': 'GU',
    'GUY': 'GY',
    'HAI': 'HT',
    'HKG': 'HK',
    'HON': 'HN',
    'HUN': 'HU',
    'INA': 'ID',
    'IND': 'IN',
    'IRI': 'IR',
    'IRL': 'IE',
    'IRQ': 'IQ',
    'ISL': 'IS',
    'ISR': 'IL',
    'ISV': 'VI',
    'ITA': 'IT',
    'IVB': 'VG',
    'JAM': 'JM',
    'JOR': 'JO',
    'JPN': 'JP',
    'KAZ': 'KZ',
    'KEN': 'KE',
    'KGZ': 'KG',
    'KIR': 'KI',
    'KOR': 'KP',
    'KSA': 'SA',
    'KUW': 'KW',
    'LAO': 'LA',
    'LAT': 'LV',
    'LBA': 'LY',
    'LBR': 'LR',
    'LCA': 'LC',
    'LES': 'LS',
    'LIB': 'LB',
    'LIE': 'LI',
    'LTU': 'LT',
    'LUX': 'LU',
    'MAD': 'MG',
    'MAR': 'MA',
    'MAS': 'MY',
    'MAW': 'MW',
    'MDA': 'MD',
    'MDV': 'MV',
    'MEX': 'MX',
    'MGL': 'MN',
    'MHL': 'MH',
    'MKD': 'MK',
    'MLI': 'ML',
    'MLT': 'MT',
    'MNE': 'ME',
    'MON': 'MC',
    'MOZ': 'MZ',
    'MRI': 'MU',
    'MTN': 'MR',
    'MYA': 'MM',
    'NAM': 'NA',
    'NCA': 'NI',
    'NED': 'NL',
    'NEP': 'NP',
    'NGR': 'NG',
    'NIG': 'NE',
    'NOR': 'NO',
    'NRU': 'NR',
    'NZL': 'NZ',
    'OMA': 'OM',
    'PAK': 'PK',
    'PAN': 'PA',
    'PAR': 'PY',
    'PER': 'PE',
    'PHI': 'PH',
    'PLE': 'PS',
    'PLW': 'PW',
    'PNG': 'PG',
    'POL': 'PL',
    'POR': 'PT',
    'PRK': 'KR',
    'PUR': 'PR',
    'QAT': 'QA',
    'ROU': 'RO',
    'RSA': 'ZA',
    'RUS': 'RU',
    'RWA': 'RW',
    'SAM': 'WS',
    'SEN': 'SN',
    'SEY': 'SC',
    'SIN': 'SG',
    'SKN': 'KN',
    'SLE': 'SL',
    'SLO': 'SI',
    'SMR': 'SM',
    'SOL': 'SB',
    'SOM': 'SO',
    'SRB': 'RS',
    'SRI': 'LK',
    'STP': 'ST',
    'SUD': 'SD',
    'SUI': 'CH',
    'SUR': 'SR',
    'SVK': 'SK',
    'SWE': 'SE',
    'SWZ': 'SZ',
    'SYR': 'SY',
    'TAN': 'TZ',
    'TGA': 'TO',
    'THA': 'TH',
    'TJK': 'TJ',
    'TKM': 'TM',
    'TLS': 'TL',
    'TOG': 'TG',
    'TPE': 'TW',
    'TTO': 'TT',
    'TUN': 'TN',
    'TUR': 'TR',
    'TUV': 'TV',
    'UAE': 'AE',
    'UGA': 'UG',
    'UKR': 'UA',
    'URU': 'UY',
    'USA': 'US',
    'UZB': 'UZ',
    'VAN': 'VU',
    'VEN': 'VE',
    'VIE': 'VN',
    'VIN': 'VC',
    'YEM': 'YE',
    'ZAM': 'ZM',
    'ZIM': 'ZW',
}

ISO_TO_IOC = dict((iso, ioc) for ioc, iso in IOC_TO_ISO.items())

def check_ioc_countries(verbosity=1):
    """
    Check if all IOC codes map to ISO codes correctly
    """
    from django_countries.data import COUNTRIES

    if verbosity:  # pragma: no cover
        print("Checking if all IOC codes map correctly")
    for key in ISO_TO_IOC:
        assert COUNTRIES.get(key), 'No ISO code for %s' % key
    if verbosity:  # pragma: no cover
        print("Finished checking IOC codes")
<commit_msg>Update ioc_data.py<commit_after>IOC_TO_ISO = {
    'AFG': 'AF',
    'ALB': 'AL',
    'ALG': 'DZ',
    'AND': 'AD',
    'ANG': 'AO',
    'ANT': 'AG',
    'ARG': 'AR',
    'ARM': 'AM',
    'ARU': 'AW',
    'ASA': 'AS',
    'AUS': 'AU',
    'AUT': 'AT',
    'AZE': 'AZ',
    'BAH': 'BS',
    'BAN': 'BD',
    'BAR': 'BB',
    'BDI': 'BI',
    'BEL': 'BE',
    'BEN': 'BJ',
    'BER': 'BM',
    'BHU': 'BT',
    'BIH': 'BA',
    'BIZ': 'BZ',
    'BLR': 'BY',
    'BOL': 'BO',
    'BOT': 'BW',
    'BRA': 'BR',
    'BRN': 'BH',
    'BRU': 'BN',
    'BUL': 'BG',
    'BUR': 'BF',
    'CAF': 'CF',
    'CAM': 'KH',
    'CAN': 'CA',
    'CAY': 'KY',
    'CGO': 'CG',
    'CHA': 'TD',
    'CHI': 'CL',
    'CHN': 'CN',
    'CIV': 'CI',
    'CMR': 'CM',
    'COD': 'CD',
    'COK': 'CK',
    'COL': 'CO',
    'COM': 'KM',
    'CPV': 'CV',
    'CRC': 'CR',
    'CRO': 'HR',
    'CUB': 'CU',
    'CYP': 'CY',
    'CZE': 'CZ',
    'DEN': 'DK',
    'DJI': 'DJ',
    'DMA': 'DM',
    'DOM': 'DO',
    'ECU': 'EC',
    'EGY': 'EG',
    'ERI': 'ER',
    'ESA': 'SV',
    'ESP': 'ES',
    'EST': 'EE',
    'ETH': 'ET',
    'FIJ': 'FJ',
    'FIN': 'FI',
    'FRA': 'FR',
    'FSM': 'FM',
    'GAB': 'GA',
    'GAM': 'GM',
    'GBR': 'GB',
    'GBS': 'GW',
    'GEO': 'GE',
    'GEQ': 'GQ',
    'GER': 'DE',
    'GHA': 'GH',
    'GRE': 'GR',
    'GRN': 'GD',
    'GUA': 'GT',
    'GUI': 'GN',
    'GUM': 'GU',
    'GUY': 'GY',
    'HAI': 'HT',
    'HKG': 'HK',
    'HON': 'HN',
    'HUN': 'HU',
    'INA': 'ID',
    'IND': 'IN',
    'IRI': 'IR',
    'IRL': 'IE',
    'IRQ': 'IQ',
    'ISL': 'IS',
    'ISR': 'IL',
    'ISV': 'VI',
    'ITA': 'IT',
    'IVB': 'VG',
    'JAM': 'JM',
    'JOR': 'JO',
    'JPN': 'JP',
    'KAZ': 'KZ',
    'KEN': 'KE',
    'KGZ': 'KG',
    'KIR': 'KI',
    'KOR': 'KR',
    'KSA': 'SA',
    'KUW': 'KW',
    'LAO': 'LA',
    'LAT': 'LV',
    'LBA': 'LY',
    'LBR': 'LR',
    'LCA': 'LC',
    'LES': 'LS',
    'LIB': 'LB',
    'LIE': 'LI',
    'LTU': 'LT',
    'LUX': 'LU',
    'MAD': 'MG',
    'MAR': 'MA',
    'MAS': 'MY',
    'MAW': 'MW',
    'MDA': 'MD',
    'MDV': 'MV',
    'MEX': 'MX',
    'MGL': 'MN',
    'MHL': 'MH',
    'MKD': 'MK',
    'MLI': 'ML',
    'MLT': 'MT',
    'MNE': 'ME',
    'MON': 'MC',
    'MOZ': 'MZ',
    'MRI': 'MU',
    'MTN': 'MR',
    'MYA': 'MM',
    'NAM': 'NA',
    'NCA': 'NI',
    'NED': 'NL',
    'NEP': 'NP',
    'NGR': 'NG',
    'NIG': 'NE',
    'NOR': 'NO',
    'NRU': 'NR',
    'NZL': 'NZ',
    'OMA': 'OM',
    'PAK': 'PK',
    'PAN': 'PA',
    'PAR': 'PY',
    'PER': 'PE',
    'PHI': 'PH',
    'PLE': 'PS',
    'PLW': 'PW',
    'PNG': 'PG',
    'POL': 'PL',
    'POR': 'PT',
    'PRK': 'KP',
    'PUR': 'PR',
    'QAT': 'QA',
    'ROU': 'RO',
    'RSA': 'ZA',
    'RUS': 'RU',
    'RWA': 'RW',
    'SAM': 'WS',
    'SEN': 'SN',
    'SEY': 'SC',
    'SIN': 'SG',
    'SKN': 'KN',
    'SLE': 'SL',
    'SLO': 'SI',
    'SMR': 'SM',
    'SOL': 'SB',
    'SOM': 'SO',
    'SRB': 'RS',
    'SRI': 'LK',
    'STP': 'ST',
    'SUD': 'SD',
    'SUI': 'CH',
    'SUR': 'SR',
    'SVK': 'SK',
    'SWE': 'SE',
    'SWZ': 'SZ',
    'SYR': 'SY',
    'TAN': 'TZ',
    'TGA': 'TO',
    'THA': 'TH',
    'TJK': 'TJ',
    'TKM': 'TM',
    'TLS': 'TL',
    'TOG': 'TG',
    'TPE': 'TW',
    'TTO': 'TT',
    'TUN': 'TN',
    'TUR': 'TR',
    'TUV': 'TV',
    'UAE': 'AE',
    'UGA': 'UG',
    'UKR': 'UA',
    'URU': 'UY',
    'USA': 'US',
    'UZB': 'UZ',
    'VAN': 'VU',
    'VEN': 'VE',
    'VIE': 'VN',
    'VIN': 'VC',
    'YEM': 'YE',
    'ZAM': 'ZM',
    'ZIM': 'ZW',
}

ISO_TO_IOC = dict((iso, ioc) for ioc, iso in IOC_TO_ISO.items())

def check_ioc_countries(verbosity=1):
    """
    Check if all IOC codes map to ISO codes correctly
    """
    from django_countries.data import COUNTRIES

    if verbosity:  # pragma: no cover
        print("Checking if all IOC codes map correctly")
    for key in ISO_TO_IOC:
        assert COUNTRIES.get(key), 'No ISO code for %s' % key
    if verbosity:  # pragma: no cover
        print("Finished checking IOC codes")
<|endoftext|>