<commit_before>import traceback
from pycqed.measurement.calibration.two_qubit_gates import CalibBuilder
from pycqed.analysis_v2 import tomography_qudev as tomo_analysis
from pycqed.measurement.calibration.calibration_points import CalibrationPoints

class Tomography(CalibBuilder):
    """
    Class to do state or process tomography.

    For state tomography, pass the `pulses` to generate the state from ground
    state. Pass `final_rots_basis` to choose the tomography basis.

    For process tomography, the `pulses` represent the process under
    investigation. Use `final_rots_basis` and `init_rots_basis` to specify the
    tomography bases for the measurement and for the state initialization.

    Args:
        task_list: A list of dictionaries, describing tomography experiments
            to be executed in parallel. Keys for each task:
                qubits: Qubits for the tomography
                pulses: The process or state that is to be measured in
                    tomography. See the docstring of
                    `Tomography.block_from_anything` for allowed formats.
                final_rots_basis, final_all_rots: The basis rotations done
                    before the readout. For the exact format of the arguments,
                    see the docsstring of `CircuitBuilder.tomography_pulses`.
                    Defaults: ('I', 'X180', 'Y90', 'mY90', 'X90', 'mX90'), True
                init_rots_basis, init_all_rots: The basis rotations done
                    before the pulses for initialization. For the exact format
                    of the arguments, see the docsstring of
                    `CircuitBuilder.tomography_pulses`. Main intended use is
                    for process tomography. Defaults: Defaults: ('I',), True
                prepended_pulses: A sequence of pulses that will be prepended
                    to the initialization rotations, but will come after the
                    preparation pulses (e.g. preselection). See the docstring
                    of `Tomography.block_from_anything` for allowed formats.
                    Default: None
            The default value for any item not specified in the task, except
            for `prepended_pulses` or `qubits`, will be taken from the keyword
            arguments of the init of the class, if provided.
        cal_all_combinations: Generate all combinations of multi-qubit
            calibration states, e.g. 00, 01, 10, 11 for two qubits, instead of
            just the necessary subset assuming no crosstalk, e.g. 00, 11.
            Default: True
        optimize_identity: Implement the identity operation by zero-duration
            virtual pulses if True. Idle for the duration of a pi-pulse if
            False. Default: False
    """

    kw_for_task_keys = ('pulses',
                        'init_rots_basis',
                        'final_rots_basis',
                        'init_all_rots',
                        'final_all_rots')

    def __init__(self, task_list, pulses=None, init_rots_basis=('I',),
                 final_rots_basis=tomo_analysis.DEFAULT_BASIS_ROTS,
                 init_all_rots=True, final_all_rots=True,
                 cal_all_combinations=True, optimize_identity=False, **kw):
        try:
            for task in task_list:
                # convert qubit objects to qubit names
                task['qubits'] = [q if isinstance(q, str) else q.name
                                  for q in task['qubits']]
                # generate an informative task prefix
                if 'prefix' not in task:
                    task['prefix'] = ''.join(task['qubits']) + "_"

            state_tomo = True
            if init_rots_basis != ('I',):
                state_tomo = False
            for task in task_list:
                if task.get('init_rots_basis', ('I',)) != ('I',):
                    state_tomo = False
            if state_tomo:
                self.experiment_name = \
                    f'{kw.get("state_name", "")}StateTomography'
            else:
                self.experiment_name = \
                    f'{kw.get("process_name", "")}ProcessTomography'

            super().__init__(task_list,
                             pulses=pulses,
                             init_rots_basis=init_rots_basis,
                             final_rots_basis=final_rots_basis,
                             init_all_rots=init_all_rots,
                             final_all_rots=final_all_rots,
                             cal_all_combinations=cal_all_combinations,
                             optimize_identity=optimize_identity,
                             **kw)

            # update measurement object to only read out tomography qubits
            self.meas_obj_names = []
            for task in self.task_list:
                for qbn in task['qubits']:
                    if qbn not in self.meas_obj_names:
                        self.meas_obj_names.append(qbn)
            self.meas_objs, _ = self.get_qubits(self.meas_obj_names)

            self.optimize_identity = optimize_identity
            self.global_prepend_block = None
            self.global_initializations = None
            self.global_finalizations = None

            # Preprocess sweep points and tasks before creating the sequences
            self.preprocessed_task_list = self.preprocess_task_list(
                pulses=pulses,
                init_rots_basis=init_rots_basis,
                final_rots_basis=final_rots_basis,
                init_all_rots=init_all_rots,
                final_all_rots=final_all_rots,
                cal_all_combinations=cal_all_combinations,
                optimize_identity=optimize_identity,
                **kw)

            init_kwargs = kw.get('init_kwargs', {})
            init_kwargs['prepend_block'] = self.global_prepend_block

            self.sequences, self.mc_points = self.parallel_sweep(
                self.preprocessed_task_list, self.sweep_block,
                block_align=['end'], init_kwargs=init_kwargs, **kw)

            self.autorun(**kw)  # run measurement & analysis if requested in kw
        except Exception as x:
            self.exception = x
            traceback.print_exc()

    def preprocess_task_list(self, **kw):
        """
        Called once per task. See MultiTaskingExperiment.preprocess_task_list.

        In this case, combines the initialization and finalization pulses from
        all tasks.
        """

        prepend_block_list = []
        initializations_map = {}
        nr_inits = 0
        finalizations_map = {}
        nr_finals = 0
        preprocessed_task_list = super().preprocess_task_list(**kw)

        def process_tomo(qubits, rots_basis, all_rots):
            seg_map = {}
            nr_segs = 0
            if rots_basis is None:
                rots_basis = ('I',)
            tomo = self.tomography_pulses(qubits, rots_basis, all_rots)
            for tomo_seg in tomo:
                for qb in qubits:
                    tomo_seg_qb = tomo_seg[self.qb_names.index(qb)]
                    seg_map[qb] = seg_map.get(qb, [])
                    seg_map[qb].append(tomo_seg_qb)
                    nr_segs = max(nr_segs, len(seg_map[qb]))
            return seg_map, nr_segs, rots_basis

        for task in preprocessed_task_list:
            initializations_map, nr_inits, task['init_rots_basis'] = \
                process_tomo(task['qubits'], task['init_rots_basis'],
                             task['init_all_rots'])
            finalizations_map, nr_finals, task['final_rots_basis'] = \
                process_tomo(task['qubits'], task['final_rots_basis'],
                             task['final_all_rots'])
            if task.get('prepend_pulses', None) is not None:
                prepend_block_list.append(self.block_from_anything(
                    task['prepend_pulses'], 'PrependPulses'))
        for qb in self.meas_obj_names:
            if qb not in initializations_map:
                initializations_map[qb] = []
            if qb not in finalizations_map:
                finalizations_map[qb] = []
        for qb, tomo_init_qb in initializations_map.items():
            while len(tomo_init_qb) < nr_inits:
                tomo_init_qb.append('I')
        for qb, tomo_final_qb in finalizations_map.items():
            while len(tomo_final_qb) < nr_finals:
                tomo_final_qb.append('I')
        self.global_initializations = []
        for i in range(nr_inits):
            self.global_initializations += [[]]
            for qb in self.meas_obj_names:
                self.global_initializations[-1] += [initializations_map[qb][i]]
        self.global_finalizations = []
        for i in range(nr_finals):
            self.global_finalizations += [[]]
            for qb in self.meas_obj_names:
                self.global_finalizations[-1] += [finalizations_map[qb][i]]
        if len(prepend_block_list) > 0:
            self.global_prepend_block = self.simultaneous_blocks(
                'prepend_block', prepend_block_list, block_align='end')
        self.do_optimize_identity()
        self.sweep_points.add_sweep_parameter(
            'finalize', self.global_finalizations, '', 'Final', dimension=0)
        self.sweep_points.add_sweep_parameter(
            'initialize', self.global_initializations, '', 'Init', dimension=1)

        return preprocessed_task_list

    def sweep_block(self, pulses, **kw):
        """
        Called by MultiTaskingExperiment.parallel_sweep per task.

        No further processing needs to be done on the pulses passed in the task.
        """
        return self.block_from_anything(pulses, 'Pulses')

    def create_cal_points(self, n_cal_points_per_state=1, cal_states='auto',
                          for_ef=False, cal_all_combinations=False, **kw):
        """Adds calibration points to the experiment.

        Supports using either the minimal calibration points set 00...0, 11...1
        or the full set 00...0, 00...1, ..., 11...0, 11...1.

        MultiTaskingExperiment.create_cal_points supports only the first case.

        Args:
            n_cal_points_per_state, cal_states, for_ef, **kw:
                See MultiTaskingExperiment.create_cal_points
            cal_all_combinations: bool, optional
                Creates a full set of all computational states as calibration
                points if true. Default False.
        """

        if not cal_all_combinations:
            return super().create_cal_points(
                n_cal_points_per_state=n_cal_points_per_state,
                cal_states=cal_states,
                for_ef=for_ef, **kw)
        self.cal_states = CalibrationPoints.guess_cal_states(
            cal_states, for_ef=for_ef)
        self.cal_points = CalibrationPoints.multi_qubit(
            self.task_list[0]['qubits'], self.cal_states,
            n_per_state=n_cal_points_per_state, all_combinations=True)
        for task in self.task_list[1:]:
            self.cal_points = CalibrationPoints.combine_parallel(
                self.cal_points,
                CalibrationPoints.multi_qubit(
                    task['qubits'], self.cal_states,
                    n_per_state=n_cal_points_per_state, all_combinations=True)
            )
        self.exp_metadata.update({'cal_points': repr(self.cal_points)})

    def do_optimize_identity(self):
        """Replace 'I' operations with 'Z0' or with 'X0' in init. and final.

        The zero-duration 'Z0' operation is used if self.optimize_identity,
        otherwise single-qubit-gate-duration 'X0' operation is used.
        """

        def recursive_replace_op(in_this, this, with_that):
            if isinstance(in_this, str):
                if in_this == this:
                    return with_that
                elif in_this.startswith(this + ' '):
                    return with_that + ' ' + in_this[len(this) + 1:]
                else:
                    return in_this
            else:
                return [recursive_replace_op(el, this, with_that)
                        for el in in_this]

        replacement_op = 'Z0' if self.optimize_identity else 'X0'
        self.global_finalizations = recursive_replace_op(
            self.global_finalizations, 'I', replacement_op)
        self.global_initializations = recursive_replace_op(
            self.global_initializations, 'I', replacement_op)
<commit_msg>Bugfix Tomography measurement<commit_after>import traceback
from pycqed.measurement.calibration.two_qubit_gates import CalibBuilder
from pycqed.analysis_v2 import tomography_qudev as tomo_analysis
from pycqed.measurement.calibration.calibration_points import CalibrationPoints

class Tomography(CalibBuilder):
    """
    Class to do state or process tomography.

    For state tomography, pass the `pulses` to generate the state from ground
    state. Pass `final_rots_basis` to choose the tomography basis.

    For process tomography, the `pulses` represent the process under
    investigation. Use `final_rots_basis` and `init_rots_basis` to specify the
    tomography bases for the measurement and for the state initialization.

    Args:
        task_list: A list of dictionaries, describing tomography experiments
            to be executed in parallel. Keys for each task:
                qubits: Qubits for the tomography
                pulses: The process or state that is to be measured in
                    tomography. See the docstring of
                    `Tomography.block_from_anything` for allowed formats.
                final_rots_basis, final_all_rots: The basis rotations done
                    before the readout. For the exact format of the arguments,
                    see the docsstring of `CircuitBuilder.tomography_pulses`.
                    Defaults: ('I', 'X180', 'Y90', 'mY90', 'X90', 'mX90'), True
                init_rots_basis, init_all_rots: The basis rotations done
                    before the pulses for initialization. For the exact format
                    of the arguments, see the docsstring of
                    `CircuitBuilder.tomography_pulses`. Main intended use is
                    for process tomography. Defaults: Defaults: ('I',), True
                prepended_pulses: A sequence of pulses that will be prepended
                    to the initialization rotations, but will come after the
                    preparation pulses (e.g. preselection). See the docstring
                    of `Tomography.block_from_anything` for allowed formats.
                    Default: None
            The default value for any item not specified in the task, except
            for `prepended_pulses` or `qubits`, will be taken from the keyword
            arguments of the init of the class, if provided.
        cal_all_combinations: Generate all combinations of multi-qubit
            calibration states, e.g. 00, 01, 10, 11 for two qubits, instead of
            just the necessary subset assuming no crosstalk, e.g. 00, 11.
            Default: True
        optimize_identity: Implement the identity operation by zero-duration
            virtual pulses if True. Idle for the duration of a pi-pulse if
            False. Default: False
    """

    kw_for_task_keys = ('pulses',
                        'init_rots_basis',
                        'final_rots_basis',
                        'init_all_rots',
                        'final_all_rots')

    def __init__(self, task_list, pulses=None, init_rots_basis=('I',),
                 final_rots_basis=tomo_analysis.DEFAULT_BASIS_ROTS,
                 init_all_rots=True, final_all_rots=True,
                 cal_all_combinations=True, optimize_identity=False, **kw):
        try:
            for task in task_list:
                # convert qubit objects to qubit names
                task['qubits'] = [q if isinstance(q, str) else q.name
                                  for q in task['qubits']]
                # generate an informative task prefix
                if 'prefix' not in task:
                    task['prefix'] = ''.join(task['qubits']) + "_"

            state_tomo = True
            if init_rots_basis != ('I',):
                state_tomo = False
            for task in task_list:
                if task.get('init_rots_basis', ('I',)) != ('I',):
                    state_tomo = False
            if state_tomo:
                self.experiment_name = \
                    f'{kw.get("state_name", "")}StateTomography'
            else:
                self.experiment_name = \
                    f'{kw.get("process_name", "")}ProcessTomography'

            super().__init__(task_list,
                             pulses=pulses,
                             init_rots_basis=init_rots_basis,
                             final_rots_basis=final_rots_basis,
                             init_all_rots=init_all_rots,
                             final_all_rots=final_all_rots,
                             cal_all_combinations=cal_all_combinations,
                             optimize_identity=optimize_identity,
                             **kw)

            # update measurement object to only read out tomography qubits
            self.meas_obj_names = []
            for task in self.task_list:
                for qbn in task['qubits']:
                    if qbn not in self.meas_obj_names:
                        self.meas_obj_names.append(qbn)
            self.meas_objs, _ = self.get_qubits(self.meas_obj_names)

            self.optimize_identity = optimize_identity
            self.global_prepend_block = None
            self.global_initializations = None
            self.global_finalizations = None

            # Preprocess sweep points and tasks before creating the sequences
            self.preprocessed_task_list = self.preprocess_task_list(
                pulses=pulses,
                init_rots_basis=init_rots_basis,
                final_rots_basis=final_rots_basis,
                init_all_rots=init_all_rots,
                final_all_rots=final_all_rots,
                cal_all_combinations=cal_all_combinations,
                optimize_identity=optimize_identity,
                **kw)

            init_kwargs = kw.get('init_kwargs', {})
            init_kwargs['prepend_block'] = self.global_prepend_block

            self.sequences, self.mc_points = self.parallel_sweep(
                self.preprocessed_task_list, self.sweep_block,
                block_align=['end'], init_kwargs=init_kwargs, **kw)

            self.autorun(**kw)  # run measurement & analysis if requested in kw
        except Exception as x:
            self.exception = x
            traceback.print_exc()

    def preprocess_task_list(self, **kw):
        """
        Called once per task. See MultiTaskingExperiment.preprocess_task_list.

        In this case, combines the initialization and finalization pulses from
        all tasks.
        """

        prepend_block_list = []
        initializations_map = {}
        nr_inits = 0
        finalizations_map = {}
        nr_finals = 0
        preprocessed_task_list = super().preprocess_task_list(**kw)

        def process_tomo(qubits, rots_basis, all_rots):
            seg_map = {}
            nr_segs = 0
            if rots_basis is None:
                rots_basis = ('I',)
            tomo = self.tomography_pulses(qubits, rots_basis, all_rots)
            for tomo_seg in tomo:
                for qb in qubits:
                    tomo_seg_qb = tomo_seg[self.qb_names.index(qb)]
                    seg_map[qb] = seg_map.get(qb, [])
                    seg_map[qb].append(tomo_seg_qb)
                    nr_segs = max(nr_segs, len(seg_map[qb]))
            return seg_map, nr_segs, rots_basis

        finalizations_map_all = dict()
        for task in preprocessed_task_list:
            initializations_map, nr_inits, task['init_rots_basis'] = \
                process_tomo(task['qubits'], task['init_rots_basis'],
                             task['init_all_rots'])
            finalizations_map, nr_finals, task['final_rots_basis'] = \
                process_tomo(task['qubits'], task['final_rots_basis'],
                             task['final_all_rots'])
            if task.get('prepend_pulses', None) is not None:
                prepend_block_list.append(self.block_from_anything(
                    task['prepend_pulses'], 'PrependPulses'))
            finalizations_map_all.update(finalizations_map)
        for qb in self.meas_obj_names:
            if qb not in initializations_map:
                initializations_map[qb] = []
            if qb not in finalizations_map_all:
                finalizations_map[qb] = []
        for qb, tomo_init_qb in initializations_map.items():
            while len(tomo_init_qb) < nr_inits:
                tomo_init_qb.append('I')
        for qb, tomo_final_qb in finalizations_map_all.items():
            while len(tomo_final_qb) < nr_finals:
                tomo_final_qb.append('I')
        self.global_initializations = []
        for i in range(nr_inits):
            self.global_initializations += [[]]
            for qb in self.meas_obj_names:
                self.global_initializations[-1] += [initializations_map[qb][i]]
        self.global_finalizations = []
        for i in range(nr_finals):
            self.global_finalizations += [[]]
            for qb in self.meas_obj_names:
                self.global_finalizations[-1] += [finalizations_map_all[qb][i]]
        if len(prepend_block_list) > 0:
            self.global_prepend_block = self.simultaneous_blocks(
                'prepend_block', prepend_block_list, block_align='end')
        self.do_optimize_identity()
        self.sweep_points.add_sweep_parameter(
            'finalize', self.global_finalizations, '', 'Final', dimension=0)
        self.sweep_points.add_sweep_parameter(
            'initialize', self.global_initializations, '', 'Init', dimension=1)

        return preprocessed_task_list

    def sweep_block(self, pulses, **kw):
        """
        Called by MultiTaskingExperiment.parallel_sweep per task.

        No further processing needs to be done on the pulses passed in the task.
        """
        return self.block_from_anything(pulses, 'Pulses')

    def create_cal_points(self, n_cal_points_per_state=1, cal_states='auto',
                          for_ef=False, cal_all_combinations=False, **kw):
        """Adds calibration points to the experiment.

        Supports using either the minimal calibration points set 00...0, 11...1
        or the full set 00...0, 00...1, ..., 11...0, 11...1.

        MultiTaskingExperiment.create_cal_points supports only the first case.

        Args:
            n_cal_points_per_state, cal_states, for_ef, **kw:
                See MultiTaskingExperiment.create_cal_points
            cal_all_combinations: bool, optional
                Creates a full set of all computational states as calibration
                points if true. Default False.
        """

        if not cal_all_combinations:
            return super().create_cal_points(
                n_cal_points_per_state=n_cal_points_per_state,
                cal_states=cal_states,
                for_ef=for_ef, **kw)
        self.cal_states = CalibrationPoints.guess_cal_states(
            cal_states, for_ef=for_ef)
        self.cal_points = CalibrationPoints.multi_qubit(
            self.task_list[0]['qubits'], self.cal_states,
            n_per_state=n_cal_points_per_state, all_combinations=True)
        for task in self.task_list[1:]:
            self.cal_points = CalibrationPoints.combine_parallel(
                self.cal_points,
                CalibrationPoints.multi_qubit(
                    task['qubits'], self.cal_states,
                    n_per_state=n_cal_points_per_state, all_combinations=True)
            )
        self.exp_metadata.update({'cal_points': repr(self.cal_points)})

    def do_optimize_identity(self):
        """Replace 'I' operations with 'Z0' or with 'X0' in init. and final.

        The zero-duration 'Z0' operation is used if self.optimize_identity,
        otherwise single-qubit-gate-duration 'X0' operation is used.
        """

        def recursive_replace_op(in_this, this, with_that):
            if isinstance(in_this, str):
                if in_this == this:
                    return with_that
                elif in_this.startswith(this + ' '):
                    return with_that + ' ' + in_this[len(this) + 1:]
                else:
                    return in_this
            else:
                return [recursive_replace_op(el, this, with_that)
                        for el in in_this]

        replacement_op = 'Z0' if self.optimize_identity else 'X0'
        self.global_finalizations = recursive_replace_op(
            self.global_finalizations, 'I', replacement_op)
        self.global_initializations = recursive_replace_op(
            self.global_initializations, 'I', replacement_op)
<|endoftext|>


<commit_before>
a = b = c = 2

print(a+b+c)

# 也可以为多个对象指定多个变量,注意等号前后个数不一致会报错
a, b, c = 1, 2, "runoob"

print(a)
print(b)
print(c)

# 内置的 type() 函数可以用来查询变量所指的对象类型
print(type(a))
print(type(c))

# 数值运算

# 加法
print(1 + 2)

# 减法
print(11.4 - 3.3)

# 乘法
print(3 * 5)

# 除法，得到一个浮点数
print(3 / 2)

# 除法，得到一个整数
print(3 // 2)

# 取余
print(8 % 3)

# 乘方
print(2 ** 3)

#
<commit_msg>commitByZsx<commit_after>
a = b = c = 2

print(a+b+c)

# 也可以为多个对象指定多个变量,注意等号前后个数不一致会报错
a, b, c = 1, 2, "runoob"

print(a)
print(b)
print(c)

# 内置的 type() 函数可以用来查询变量所指的对象类型
print(type(a))
print(type(c))

# 数值运算

# 加法
print(1 + 2)

# 减法
print(11.4 - 3.3)

# 乘法
print(3 * 5)

# 除法，得到一个浮点数
print(3 / 2)

# 除法，得到一个整数
print(3 // 2)

# 取余
print(8 % 3)

# 乘方
print(2 ** 3)

# 字符串截取

str = "zhaoshuxue"

print (str)          # 输出字符串
print (str[0:-1])    # 输出第一个到倒数第二个的所有字符
print (str[0])       # 输出字符串第一个字符
print (str[2:5])     # 输出从第三个开始到第五个的字符
print (str[2:])      # 输出从第三个开始的后的所有字符
print (str * 2)      # 输出字符串两次
print (str + "TEST") # 连接字符串

#
<|endoftext|>


<commit_before># coding: utf-8

from django.forms import (
	Form, IntegerField, CharField, EmailField, ChoiceField, NullBooleanField )
from django.contrib.formtools.wizard.views import SessionWizardView
from django.shortcuts import render, render_to_response
from django.template.loader import render_to_string
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext_lazy as _
from django.core.mail import send_mail
from datetime import date, datetime
from pytz import utc
from copy import copy
from pprint import pformat
from core.models import User
from ticketing.models import Order, Ticket, Performance, PriceCategory, StandardMarketingPollAnswer

def landing(request):
	return render(request, 'space/landing.html')

performances = (
	('do', _('Donderdag 7 mei - om 20u30 in Aula Pieter De Somer, Leuven')),
	('vr', _('Vrijdag 8 mei - om 20u30 in Aula Pieter De Somer, Leuven')),
	('za', _('Zaterdag 9 mei - om 20u00 in St-Martinuskerk, Zaventem')),
)

class SpaceTicketingForm_1(Form):
	performance = ChoiceField(required=True, choices=performances)
	num_culture_card_tickets = IntegerField(required=False, min_value=0)
	num_student_tickets = IntegerField(required=False, min_value=0)
	num_non_student_tickets = IntegerField(required=False, min_value=0)
	num_zaventem_tickets = IntegerField(required=False, min_value=0)
	first_name = CharField(required=True)
	last_name = CharField(required=True)
	email = EmailField(required=True)

def get_user_list():
	choices = []
	for user in User.objects.all().order_by('first_name'):
		choices.append((str(user.id), user.get_full_name()))
	return choices

class SpaceTicketingForm_2(Form):
	payment_method = ChoiceField(required=True, choices=Order.payment_method_choices)
	marketing_feedback = CharField(required=True)
	referred_member = ChoiceField(required=False, choices=get_user_list())
	first_concert = NullBooleanField(required=False)
	remarks = CharField(required=False)

def parse_form_data(form):
	data = {}
	data['performance']              = form.get('performance', '')
	data['performance_full']		 = dict(performances).get(data['performance'], '')
	data['num_culture_card_tickets'] = int(form.get('num_culture_card_tickets', 0))
	data['num_student_tickets']      = int(form.get('num_student_tickets', 0))
	data['num_non_student_tickets']  = int(form.get('num_non_student_tickets', 0))
	data['num_zaventem_tickets']     = int(form.get('num_zaventem_tickets', 0))
	data['first_name']               = form.get('first_name', '')
	data['last_name']                = form.get('last_name', '')
	data['email']                    = form.get('email', '')
	data['payment_method']           = form.get('payment_method', '')
	data['payment_method_full']		 = dict(Order.payment_method_choices).get(data['payment_method'], '')
	data['marketing_feedback']       = form.get('marketing_feedback', '')
	data['referred_member']          = form.get('referred_member', '')
	data['first_concert'] 			 = form.get('first_concert', None)
	data['remarks']                  = form.get('remarks', '')

	if data['performance'] == 'za':
		data['total_tickets'] = data['num_zaventem_tickets']
		data['total_price'] = 10*data['num_zaventem_tickets']
	else:
		data['total_tickets'] = data['num_student_tickets'] + data['num_non_student_tickets'] + data['num_culture_card_tickets']
		data['total_price'] = 5*data['num_student_tickets'] + 9*data['num_non_student_tickets'] + 4*data['num_culture_card_tickets']

	return data

def persist_data(data):
	if data['performance'] == 'do':
		performance = Performance.objects.get(date__contains=date(2015,5,7))
	elif data['performance'] == 'vr':
		performance = Performance.objects.get(date__contains=date(2015,5,8))
	elif data['performance'] == 'za':
		performance = Performance.objects.get(date__contains=date(2015,5,9))

	order = Order.objects.create(
		performance = performance,
		first_name = data['first_name'],
		last_name = data['last_name'],
		email = data['email'],
		date = datetime.now(utc),
		payment_method = data['payment_method'],
		user_remarks = data['remarks'],
	)
	if data['referred_member']:
		rm = User.objects.get(id=int(data['referred_member']))
	else:
		rm = None
	marketing_poll_answers = StandardMarketingPollAnswer.objects.create(
		associated_order = order,
		marketing_feedback = data['marketing_feedback'],
		referred_member = rm,
		first_concert = data['first_concert'],
	)

	if data['performance'] == 'za':
		for i in range(data['num_zaventem_tickets']):
			Ticket.objects.create(
				order = order,
				price_category = PriceCategory.objects.get(name="Zaventem VVK SPACE", price=10),
			)
	else:
		for i in range(data['num_student_tickets']):
			Ticket.objects.create(
				order = order,
				price_category = PriceCategory.objects.get(name="Student in VVK (vanaf winter 2014)", price=5),
			)
		for i in range(data['num_non_student_tickets']):
			Ticket.objects.create(
				order = order,
				price_category = PriceCategory.objects.get(name="Niet-student in VVK (vanaf winter 2014)", price=9),
			)
		for i in range(data['num_culture_card_tickets']):
			Ticket.objects.create(
				order = order,
				price_category = PriceCategory.objects.get(name="Cultuurkaart in VVK (vanaf winter 2014)", price=4),
			)

def email_user(data):
	subject = "Bestelling tickets 'SPACE' concert Arenbergorkest"
	msg = render_to_string('space/email.html', data)
	send_mail(subject, msg, "webapp@arenbergorkest.be", [data['email'], 'tomas.fiers@gmail.com'], html_message=msg)

def email_admin(data):
	subject = "Bestelling tickets 'SPACE' concert Arenbergorkest"
	msg = pformat(data)
	send_mail(subject, msg, "webapp@arenbergorkest.be", ['tomas.fiers@gmail.com'])

FORMS = [("start_order", SpaceTicketingForm_1),
		 ("complete_order", SpaceTicketingForm_2)]

TEMPLATES = {"start_order": "space/start_order.html",
			 "complete_order": "space/complete_order.html"}

class MultipageTicketingForm(SessionWizardView):
	def get_template_names(self):
		return [TEMPLATES[self.steps.current]]

	def get_context_data(self, form, **kwargs):
		context = super(SessionWizardView, self).get_context_data(form=form, **kwargs)
		if self.steps.current == 'complete_order':
			data = parse_form_data(self.get_cleaned_data_for_step('start_order'))
			context.update({'first_form': data})
		return context

	def done(self, form_list, form_dict, **kwargs):
		form_data = copy(form_dict['start_order'].cleaned_data)
		form_data.update(form_dict['complete_order'].cleaned_data)
		data = parse_form_data(form_data)
		persist_data(data)
		email_user(data)
		# email_admin(data)
		return render_to_response('space/thanks.html', data)
<commit_msg>Remove send_mail temporarily<commit_after># coding: utf-8

from django.forms import (
	Form, IntegerField, CharField, EmailField, ChoiceField, NullBooleanField )
from django.contrib.formtools.wizard.views import SessionWizardView
from django.shortcuts import render, render_to_response
from django.template.loader import render_to_string
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext_lazy as _
from django.core.mail import send_mail
from datetime import date, datetime
from pytz import utc
from copy import copy
from pprint import pformat
from core.models import User
from ticketing.models import Order, Ticket, Performance, PriceCategory, StandardMarketingPollAnswer

def landing(request):
	return render(request, 'space/landing.html')

performances = (
	('do', _('Donderdag 7 mei - om 20u30 in Aula Pieter De Somer, Leuven')),
	('vr', _('Vrijdag 8 mei - om 20u30 in Aula Pieter De Somer, Leuven')),
	('za', _('Zaterdag 9 mei - om 20u00 in St-Martinuskerk, Zaventem')),
)

class SpaceTicketingForm_1(Form):
	performance = ChoiceField(required=True, choices=performances)
	num_culture_card_tickets = IntegerField(required=False, min_value=0)
	num_student_tickets = IntegerField(required=False, min_value=0)
	num_non_student_tickets = IntegerField(required=False, min_value=0)
	num_zaventem_tickets = IntegerField(required=False, min_value=0)
	first_name = CharField(required=True)
	last_name = CharField(required=True)
	email = EmailField(required=True)

def get_user_list():
	choices = []
	for user in User.objects.all().order_by('first_name'):
		choices.append((str(user.id), user.get_full_name()))
	return choices

class SpaceTicketingForm_2(Form):
	payment_method = ChoiceField(required=True, choices=Order.payment_method_choices)
	marketing_feedback = CharField(required=True)
	referred_member = ChoiceField(required=False, choices=get_user_list())
	first_concert = NullBooleanField(required=False)
	remarks = CharField(required=False)

def parse_form_data(form):
	data = {}
	data['performance']              = form.get('performance', '')
	data['performance_full']		 = dict(performances).get(data['performance'], '')
	data['num_culture_card_tickets'] = int(form.get('num_culture_card_tickets', 0))
	data['num_student_tickets']      = int(form.get('num_student_tickets', 0))
	data['num_non_student_tickets']  = int(form.get('num_non_student_tickets', 0))
	data['num_zaventem_tickets']     = int(form.get('num_zaventem_tickets', 0))
	data['first_name']               = form.get('first_name', '')
	data['last_name']                = form.get('last_name', '')
	data['email']                    = form.get('email', '')
	data['payment_method']           = form.get('payment_method', '')
	data['payment_method_full']		 = dict(Order.payment_method_choices).get(data['payment_method'], '')
	data['marketing_feedback']       = form.get('marketing_feedback', '')
	data['referred_member']          = form.get('referred_member', '')
	data['first_concert'] 			 = form.get('first_concert', None)
	data['remarks']                  = form.get('remarks', '')

	if data['performance'] == 'za':
		data['total_tickets'] = data['num_zaventem_tickets']
		data['total_price'] = 10*data['num_zaventem_tickets']
	else:
		data['total_tickets'] = data['num_student_tickets'] + data['num_non_student_tickets'] + data['num_culture_card_tickets']
		data['total_price'] = 5*data['num_student_tickets'] + 9*data['num_non_student_tickets'] + 4*data['num_culture_card_tickets']

	return data

def persist_data(data):
	if data['performance'] == 'do':
		performance = Performance.objects.get(date__contains=date(2015,5,7))
	elif data['performance'] == 'vr':
		performance = Performance.objects.get(date__contains=date(2015,5,8))
	elif data['performance'] == 'za':
		performance = Performance.objects.get(date__contains=date(2015,5,9))

	order = Order.objects.create(
		performance = performance,
		first_name = data['first_name'],
		last_name = data['last_name'],
		email = data['email'],
		date = datetime.now(utc),
		payment_method = data['payment_method'],
		user_remarks = data['remarks'],
	)
	if data['referred_member']:
		rm = User.objects.get(id=int(data['referred_member']))
	else:
		rm = None
	marketing_poll_answers = StandardMarketingPollAnswer.objects.create(
		associated_order = order,
		marketing_feedback = data['marketing_feedback'],
		referred_member = rm,
		first_concert = data['first_concert'],
	)

	if data['performance'] == 'za':
		for i in range(data['num_zaventem_tickets']):
			Ticket.objects.create(
				order = order,
				price_category = PriceCategory.objects.get(name="Zaventem VVK SPACE", price=10),
			)
	else:
		for i in range(data['num_student_tickets']):
			Ticket.objects.create(
				order = order,
				price_category = PriceCategory.objects.get(name="Student in VVK (vanaf winter 2014)", price=5),
			)
		for i in range(data['num_non_student_tickets']):
			Ticket.objects.create(
				order = order,
				price_category = PriceCategory.objects.get(name="Niet-student in VVK (vanaf winter 2014)", price=9),
			)
		for i in range(data['num_culture_card_tickets']):
			Ticket.objects.create(
				order = order,
				price_category = PriceCategory.objects.get(name="Cultuurkaart in VVK (vanaf winter 2014)", price=4),
			)

def email_user(data):
	subject = "Bestelling tickets 'SPACE' concert Arenbergorkest"
	msg = render_to_string('space/email.html', data)
	send_mail(subject, msg, "webapp@arenbergorkest.be", [data['email'], 'tomas.fiers@gmail.com'], html_message=msg)

def email_admin(data):
	subject = "Bestelling tickets 'SPACE' concert Arenbergorkest"
	msg = pformat(data)
	send_mail(subject, msg, "webapp@arenbergorkest.be", ['tomas.fiers@gmail.com'])

FORMS = [("start_order", SpaceTicketingForm_1),
		 ("complete_order", SpaceTicketingForm_2)]

TEMPLATES = {"start_order": "space/start_order.html",
			 "complete_order": "space/complete_order.html"}

class MultipageTicketingForm(SessionWizardView):
	def get_template_names(self):
		return [TEMPLATES[self.steps.current]]

	def get_context_data(self, form, **kwargs):
		context = super(SessionWizardView, self).get_context_data(form=form, **kwargs)
		if self.steps.current == 'complete_order':
			data = parse_form_data(self.get_cleaned_data_for_step('start_order'))
			context.update({'first_form': data})
		return context

	def done(self, form_list, form_dict, **kwargs):
		form_data = copy(form_dict['start_order'].cleaned_data)
		form_data.update(form_dict['complete_order'].cleaned_data)
		data = parse_form_data(form_data)
		persist_data(data)
		# email_user(data)
		# email_admin(data)
		return render_to_response('space/thanks.html', data)
<|endoftext|>


<commit_before>import re
import itertools
from django.contrib.gis.db import models
from mapit.managers import Manager, GeoManager
from postcodes.models import Postcode

class GenerationManager(models.Manager):
    def current(self):
        latest_on = self.get_query_set().filter(active=True).order_by('-id')
        if latest_on: return latest_on[0]
        return 0

    def new(self):
        latest = self.get_query_set().order_by('-id')
        if not latest or latest[0].active:
            return None
        return latest[0]

class Generation(models.Model):
    active = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)

    objects = GenerationManager()

    def __unicode__(self):
        return "Generation %d (%sactive)" % (self.id, "" if self.active else "in")

class AreaManager(models.GeoManager):
    def by_location(self, location, generation=None):
        if generation is None: generation = Generation.objects.current()
        return Area.objects.filter(
            polygons__polygon__contains=location,
            generation_low__lte=generation, generation_high__gte=generation
        )

    def by_postcode(self, postcode, generation=None):
        if generation is None: generation = Generation.objects.current()
        postcode = re.sub('\s+', '', postcode.upper())
        postcode = get_object_or_404(Postcode, postcode=postcode)
        return itertools.chain(
            self.by_location(postcode.location, generation),
            postcode.areas.filter(
                generation_low__lte=generation, generation_high__gte=generation
            )
        )

    def get_or_create_with_name(self, country='', type='', name_type='', name=''):
        current_generation = Generation.objects.current()
        new_generation = Generation.objects.new()
        area, created = Area.objects.get_or_create(country=country, type=type,
            generation_low__lte=current_generation, generation_high__gte=current_generation,
            names__type=name_type, names__name=name,
            defaults = { 'generation_low': new_generation, 'generation_high': new_generation }
        )
        if created:
            area.names.get_or_create(type=name_type, name=name)
        else:
            area.generation_high = new_generation
            area.save()
        return area

    def get_or_create_with_code(self, country='', type='', code_type='', code=''):
        current_generation = Generation.objects.current()
        new_generation = Generation.objects.new()
        area, created = Area.objects.get_or_create(country=country, type=type,
            generation_low__lte=current_generation, generation_high__gte=current_generation,
            codes__type=code_type, codes__code=code,
            defaults = { 'generation_low': new_generation, 'generation_high': new_generation }
        )
        if created:
            area.codes.get_or_create(type=code_type, code=code)
        else:
            area.generation_high = new_generation
            area.save()
        return area

class Area(models.Model):
    name = models.CharField(max_length=100, editable=False, blank=True) # Automatically set from name children
    parent_area = models.ForeignKey('self', related_name='children', null=True, blank=True)
    type = models.CharField(max_length=3, db_index=True, choices=(
        ('EUR', 'Euro region'),
        ('HOL', 'House of Lords'),
        ('HOC', 'House of Lords constituency'),
        ('WMP', 'UK Parliament'),
        ('WMC', 'UK Parliament constituency'),
        ('Northern Ireland', (
            ('NIA', 'Northern Ireland Assembly'),
            ('NIE', 'Northern Ireland Assembly constituency'),
            ('LGD', 'Northern Irish Council'),
            ('LGE', 'Northern Irish Council electoral area'),
            ('LGW', 'Northern Irish Council ward'),
        )),
        ('England', (
            ('CTY', 'County council'),
            ('CED', 'County council ward'),
            ('DIS', 'District council'),
            ('DIW', 'District council ward'),
            ('GLA', 'Greater London Authority'),
            ('LAC', 'London Assembly constituency'),
            ('LAE', 'London Assembly area (shared)'),
            ('LAS', 'London Assembly area'),
            ('LBO', 'London borough'),
            ('LBW', 'London borough ward'),
            ('MTD', 'Metropolitan district'),
            ('MTW', 'Metropolitan district ward'),
            ('COI', 'Scilly Isles'),
            ('COP', 'Scilly Isles "ward"'),
        )),
        ('SPA', 'Scottish Parliament'),
        ('SPC', 'Scottish Parliament constituency'),
        ('SPE', 'Scottish Parliament region'),
        ('WAS', 'Welsh Assembly'),
        ('WAC', 'Welsh Assembly constituency'),
        ('WAE', 'Welsh Assembly region'),
        ('UTA', 'Unitary Authority'),
        ('UTE', 'Unitary Authority ward (UTE)'),
        ('UTW', 'Unitary Authority ward (UTW)'),
        ('CPC', 'Civil Parish'),
    ))
    country = models.CharField(max_length=1, choices=(
        ('E', 'England'),
        ('W', 'Wales'),
        ('S', 'Scotland'),
        ('N', 'Northern Ireland'),
        ('', 'None'),
    ))
    generation_low = models.ForeignKey(Generation, related_name='new_areas', null=True)
    generation_high = models.ForeignKey(Generation, related_name='final_areas', null=True)

    objects = AreaManager()

    @property
    def all_codes(self):
        codes = {}
        for code in self.codes.all():
            codes[code.type] = code.code
        return codes

    def __unicode__(self):
        name = self.name or '(unknown)'
        return '%s %s' % (self.type, name)

class Geometry(models.Model):
    area = models.ForeignKey(Area, related_name='polygons')
    polygon = models.PolygonField(srid=27700)
    objects = GeoManager()

    class Meta:
        verbose_name_plural = 'geometries'

    def __unicode__(self):
        return '%s, polygon %d' % (self.area, self.id)

class Name(models.Model):
    area = models.ForeignKey(Area, related_name='names')
    type = models.CharField(max_length=1, choices=(
        ('O', 'Ordnance Survey'),
        ('S', 'ONS (SNAC/GSS)'),
        ('M', 'Override name'),
    ))
    name = models.CharField(max_length=100)
    objects = Manager()

    class Meta:
        unique_together = ('area', 'type')

    def __unicode__(self):
        return '%s (%s) [%s]' % (self.name, self.type, self.area.id)

    def make_friendly_name(self, name):
        n = re.sub('\s+', ' ', name.name.strip())
        n = n.replace('St. ', 'St ')
        if name.type == 'M': return n
        if name.type == 'S': return n
        # Type must be 'O' here
        n = re.sub(' Euro Region$', '', n) # EUR
        n = re.sub(' (Burgh|Co|Boro) Const$', '', n) # WMC
        n = re.sub(' (Islands )?P Const$', '', n) # SPC
        n = re.sub(' PER$', '', n) # SPE
        n = re.sub(' GL Assembly Const$', '', n) # LAC
        n = re.sub(' Assembly Const$', '', n) # WAC
        n = re.sub(' Assembly ER$', '', n) # WAE
        n = re.sub(' London Boro$', ' Borough', n) # LBO
        if self.area.country == 'W': n = re.sub('^.*? - ', '', n) # UTA
        n = re.sub('(?:The )?City of (.*?) (District )?\(B\)$', r'\1 City', n) # UTA
        n = re.sub(' District \(B\)$', ' Borough', n) # DIS
        n = re.sub(' \(B\)$', ' Borough', n) # DIS
        if self.area.type in ('CTY', 'DIS', 'LBO', 'UTA', 'MTD'): n += ' Council'
        n = re.sub(' (ED|CP)$', '', n) # CPC, CED, UTE
        n = re.sub(' Ward$', '', n) # DIW, LBW, MTW, UTW
        return n

    def save(self, *args, **kwargs):
        super(Name, self).save(*args, **kwargs)
        try:
            name = self.area.names.filter(type__in=('M', 'O', 'S')).order_by('type')[0]
            self.area.name = self.make_friendly_name(name)
            self.area.save()
        except:
            pass

class Code(models.Model):
    area = models.ForeignKey(Area, related_name='codes')
    type = models.CharField(max_length=10, choices=(
        ('ons', 'SNAC'),
        ('gss', 'GSS (SNAC replacement)'),
        ('unit_id', 'Boundary-Line (OS Admin Area ID)')
    ))
    code = models.CharField(max_length=10)
    objects = Manager()

    class Meta:
        unique_together = ('area', 'type')

    def __unicode__(self):
        return '%s (%s) [%s]' % (self.code, self.type, self.area.id)
<commit_msg>Typo.<commit_after>import re
import itertools
from django.contrib.gis.db import models
from mapit.managers import Manager, GeoManager
from mapit.postcodes.models import Postcode

class GenerationManager(models.Manager):
    def current(self):
        latest_on = self.get_query_set().filter(active=True).order_by('-id')
        if latest_on: return latest_on[0]
        return 0

    def new(self):
        latest = self.get_query_set().order_by('-id')
        if not latest or latest[0].active:
            return None
        return latest[0]

class Generation(models.Model):
    active = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)

    objects = GenerationManager()

    def __unicode__(self):
        return "Generation %d (%sactive)" % (self.id, "" if self.active else "in")

class AreaManager(models.GeoManager):
    def by_location(self, location, generation=None):
        if generation is None: generation = Generation.objects.current()
        return Area.objects.filter(
            polygons__polygon__contains=location,
            generation_low__lte=generation, generation_high__gte=generation
        )

    def by_postcode(self, postcode, generation=None):
        if generation is None: generation = Generation.objects.current()
        postcode = re.sub('\s+', '', postcode.upper())
        postcode = get_object_or_404(Postcode, postcode=postcode)
        return itertools.chain(
            self.by_location(postcode.location, generation),
            postcode.areas.filter(
                generation_low__lte=generation, generation_high__gte=generation
            )
        )

    def get_or_create_with_name(self, country='', type='', name_type='', name=''):
        current_generation = Generation.objects.current()
        new_generation = Generation.objects.new()
        area, created = Area.objects.get_or_create(country=country, type=type,
            generation_low__lte=current_generation, generation_high__gte=current_generation,
            names__type=name_type, names__name=name,
            defaults = { 'generation_low': new_generation, 'generation_high': new_generation }
        )
        if created:
            area.names.get_or_create(type=name_type, name=name)
        else:
            area.generation_high = new_generation
            area.save()
        return area

    def get_or_create_with_code(self, country='', type='', code_type='', code=''):
        current_generation = Generation.objects.current()
        new_generation = Generation.objects.new()
        area, created = Area.objects.get_or_create(country=country, type=type,
            generation_low__lte=current_generation, generation_high__gte=current_generation,
            codes__type=code_type, codes__code=code,
            defaults = { 'generation_low': new_generation, 'generation_high': new_generation }
        )
        if created:
            area.codes.get_or_create(type=code_type, code=code)
        else:
            area.generation_high = new_generation
            area.save()
        return area

class Area(models.Model):
    name = models.CharField(max_length=100, editable=False, blank=True) # Automatically set from name children
    parent_area = models.ForeignKey('self', related_name='children', null=True, blank=True)
    type = models.CharField(max_length=3, db_index=True, choices=(
        ('EUR', 'Euro region'),
        ('HOL', 'House of Lords'),
        ('HOC', 'House of Lords constituency'),
        ('WMP', 'UK Parliament'),
        ('WMC', 'UK Parliament constituency'),
        ('Northern Ireland', (
            ('NIA', 'Northern Ireland Assembly'),
            ('NIE', 'Northern Ireland Assembly constituency'),
            ('LGD', 'Northern Irish Council'),
            ('LGE', 'Northern Irish Council electoral area'),
            ('LGW', 'Northern Irish Council ward'),
        )),
        ('England', (
            ('CTY', 'County council'),
            ('CED', 'County council ward'),
            ('DIS', 'District council'),
            ('DIW', 'District council ward'),
            ('GLA', 'Greater London Authority'),
            ('LAC', 'London Assembly constituency'),
            ('LAE', 'London Assembly area (shared)'),
            ('LAS', 'London Assembly area'),
            ('LBO', 'London borough'),
            ('LBW', 'London borough ward'),
            ('MTD', 'Metropolitan district'),
            ('MTW', 'Metropolitan district ward'),
            ('COI', 'Scilly Isles'),
            ('COP', 'Scilly Isles "ward"'),
        )),
        ('SPA', 'Scottish Parliament'),
        ('SPC', 'Scottish Parliament constituency'),
        ('SPE', 'Scottish Parliament region'),
        ('WAS', 'Welsh Assembly'),
        ('WAC', 'Welsh Assembly constituency'),
        ('WAE', 'Welsh Assembly region'),
        ('UTA', 'Unitary Authority'),
        ('UTE', 'Unitary Authority ward (UTE)'),
        ('UTW', 'Unitary Authority ward (UTW)'),
        ('CPC', 'Civil Parish'),
    ))
    country = models.CharField(max_length=1, choices=(
        ('E', 'England'),
        ('W', 'Wales'),
        ('S', 'Scotland'),
        ('N', 'Northern Ireland'),
        ('', 'None'),
    ))
    generation_low = models.ForeignKey(Generation, related_name='new_areas', null=True)
    generation_high = models.ForeignKey(Generation, related_name='final_areas', null=True)

    objects = AreaManager()

    @property
    def all_codes(self):
        codes = {}
        for code in self.codes.all():
            codes[code.type] = code.code
        return codes

    def __unicode__(self):
        name = self.name or '(unknown)'
        return '%s %s' % (self.type, name)

class Geometry(models.Model):
    area = models.ForeignKey(Area, related_name='polygons')
    polygon = models.PolygonField(srid=27700)
    objects = GeoManager()

    class Meta:
        verbose_name_plural = 'geometries'

    def __unicode__(self):
        return '%s, polygon %d' % (self.area, self.id)

class Name(models.Model):
    area = models.ForeignKey(Area, related_name='names')
    type = models.CharField(max_length=1, choices=(
        ('O', 'Ordnance Survey'),
        ('S', 'ONS (SNAC/GSS)'),
        ('M', 'Override name'),
    ))
    name = models.CharField(max_length=100)
    objects = Manager()

    class Meta:
        unique_together = ('area', 'type')

    def __unicode__(self):
        return '%s (%s) [%s]' % (self.name, self.type, self.area.id)

    def make_friendly_name(self, name):
        n = re.sub('\s+', ' ', name.name.strip())
        n = n.replace('St. ', 'St ')
        if name.type == 'M': return n
        if name.type == 'S': return n
        # Type must be 'O' here
        n = re.sub(' Euro Region$', '', n) # EUR
        n = re.sub(' (Burgh|Co|Boro) Const$', '', n) # WMC
        n = re.sub(' (Islands )?P Const$', '', n) # SPC
        n = re.sub(' PER$', '', n) # SPE
        n = re.sub(' GL Assembly Const$', '', n) # LAC
        n = re.sub(' Assembly Const$', '', n) # WAC
        n = re.sub(' Assembly ER$', '', n) # WAE
        n = re.sub(' London Boro$', ' Borough', n) # LBO
        if self.area.country == 'W': n = re.sub('^.*? - ', '', n) # UTA
        n = re.sub('(?:The )?City of (.*?) (District )?\(B\)$', r'\1 City', n) # UTA
        n = re.sub(' District \(B\)$', ' Borough', n) # DIS
        n = re.sub(' \(B\)$', ' Borough', n) # DIS
        if self.area.type in ('CTY', 'DIS', 'LBO', 'UTA', 'MTD'): n += ' Council'
        n = re.sub(' (ED|CP)$', '', n) # CPC, CED, UTE
        n = re.sub(' Ward$', '', n) # DIW, LBW, MTW, UTW
        return n

    def save(self, *args, **kwargs):
        super(Name, self).save(*args, **kwargs)
        try:
            name = self.area.names.filter(type__in=('M', 'O', 'S')).order_by('type')[0]
            self.area.name = self.make_friendly_name(name)
            self.area.save()
        except:
            pass

class Code(models.Model):
    area = models.ForeignKey(Area, related_name='codes')
    type = models.CharField(max_length=10, choices=(
        ('ons', 'SNAC'),
        ('gss', 'GSS (SNAC replacement)'),
        ('unit_id', 'Boundary-Line (OS Admin Area ID)')
    ))
    code = models.CharField(max_length=10)
    objects = Manager()

    class Meta:
        unique_together = ('area', 'type')

    def __unicode__(self):
        return '%s (%s) [%s]' % (self.code, self.type, self.area.id)
<|endoftext|>


<commit_before>"""

matlab_magic
============

Magic command interface for interactive work with Matlab(R) via the pymatbridge

Note
====
Thanks to Max Jaderberg for his work on pymatbridge.

"""

import sys, os
import tempfile
from glob import glob
from shutil import rmtree
from getopt import getopt
from urllib2 import URLError

import numpy as np
try:
    import scipy.io as sio
    has_io = True
except ImportError:
    has_io = False
    no_io_str = "Must have scipy.io to perform i/o"
    no_io_str += "operations with the Matlab session"

from IPython.core.displaypub import publish_display_data
from IPython.core.magic import (Magics, magics_class, cell_magic, line_magic,
                                line_cell_magic, needs_local_scope)
from IPython.testing.skipdoctest import skip_doctest
from IPython.core.magic_arguments import (argument, magic_arguments,
                                          parse_argstring)
from IPython.utils.py3compat import str_to_unicode, unicode_to_str, PY3

import pymatbridge as pymat

class MatlabInterperterError(RuntimeError):
    """
    Some error occurs while matlab is running
    """
    def __init__(self, line, err):
        self.line = line
        self.err = err

    def __unicode__(self):
        s = "Failed to parse and evaluate line %r.\n Matlab error message: %r"%\
                (self.line, self.err)
        return s

    if PY3:
        __str__ = __unicode__
    else:
        def __str__(self):
            return unicode_to_str(unicode(self), 'utf-8')

@magics_class
class MatlabMagics(Magics):
    """
    A set of magics for interactive work with Matlab(R).
    """
    def __init__(self, shell,
                 matlab='matlab',
                 maxtime=60,
                 pyconverter=np.asarray,
                 cache_display_data=False):
        """
        Parameters
        ----------

        shell : IPython shell

        matlab : str
            The system call to start a matlab session. Allows you to choose a
            particular version of matlab if you want

        maxtime : float
           The maximal time to wait for responses for matlab (in seconds).
           Default: 10 seconds.

        pyconverter : callable
            To be called on matlab variables returning into the ipython
            namespace

        cache_display_data : bool
            If True, the published results of the final call to R are
            cached in the variable 'display_cache'.

        """
        super(MatlabMagics, self).__init__(shell)
        self.cache_display_data = cache_display_data

        self.Matlab = pymat.Matlab(matlab, maxtime=maxtime)
        self.Matlab.start()
        self.pyconverter = pyconverter

    def __del__(self):
        """shut down the Matlab server when the object dies.

        2FIX: this seems to not be called when ipython terminates. bleah.
        """
        try:
            self.Matlab.stop()
        except:
            raise

    def eval(self, line):
        """
        Parse and evaluate a single line of matlab
        """
        run_dict = self.Matlab.run_code(line, maxtime=self.Matlab.maxtime)

        if run_dict['success'] == 'false':
            raise MatlabInterperterError(line, run_dict['content']['stdout'])

        # This is the matlab stdout:
        return run_dict

    def set_matlab_var(self, name, value):
        """
        Set up a variable in Matlab workspace
        """
        run_dict = self.Matlab.run_func("pymat_set_variable.m",
                                        {'name':name, 'value':value},
                                        maxtime=self.Matlab.maxtime)

        if run_dict['success'] == 'false':
            raise MatlabInterperterError(line, run_dict['content']['stdout'])

    @magic_arguments()
    @argument(
        '-i', '--input', action='append',
        help='Names of input variable from shell.user_ns to be assigned to Matlab variables of the same names after calling self.pyconverter. Multiple names can be passed separated only by commas with no whitespace.'
        )

    @argument(
        '-o', '--output', action='append',
        help='Names of variables to be pushed from matlab to shell.user_ns after executing cell body and applying self.Matlab.get_variable(). Multiple names can be passed separated only by commas with no whitespace.'
        )

    @argument(
        '-s', '--silent', action='store_true',
        help='Do not display text output of MATLAB command'
        )

    @argument(
        'code',
        nargs='*',
        )

    @needs_local_scope
    @line_cell_magic
    def matlab(self, line, cell=None, local_ns=None):
        """

        Execute code in matlab

        """
        args = parse_argstring(self.matlab, line)

        # arguments 'code' in line are prepended to
        # the cell lines

        if cell is None:
            code = ''
            return_output = True
            line_mode = True
        else:
            code = cell
            return_output = False
            line_mode = False

        code = ' '.join(args.code) + code

        if local_ns is None:
            local_ns = {}

        if args.input:
            if has_io:
                for input in ','.join(args.input).split(','):
                    try:
                        val = local_ns[input]
                    except KeyError:
                        val = self.shell.user_ns[input]

                    # To make an array JSON serializable
                    if (isinstance(val, np.ndarray)):
                        val = val.tolist()

                    self.set_matlab_var(input, val)

            else:
                raise RuntimeError(no_io_str)

        text_output = ''
        #imgfiles = []

        try:
            if line_mode:
                e_s = "There was an error running the code:\n %s"%line
                result_dict = self.eval(line)
            else:
                e_s = "There was an error running the code:\n %s"%code
                result_dict = self.eval(code)
        except URLError:
            e_s += "\n-----------------------"
            e_s += "\nAre you sure Matlab is started?"
            raise RuntimeError(e_s)

        text_output += result_dict['content']['stdout']
        # Figures get saved by matlab in reverse order...
        imgfiles = result_dict['content']['figures'][::-1]
        data_dir = result_dict['content']['datadir']

        display_data = []
        if text_output and not args.silent:
            display_data.append(('MatlabMagic.matlab',
                                 {'text/plain':text_output}))

        for imgf in imgfiles:
            if len(imgf):
                # Store the path to the directory so that you can delete it
                # later on:
                image = open(imgf, 'rb').read()
                display_data.append(('MatlabMagic.matlab',
                                     {'image/png': image}))

        for tag, disp_d in display_data:
            publish_display_data(tag, disp_d)

        # Delete the temporary data files created by matlab:
        if len(data_dir):
            rmtree(data_dir)

        if args.output:
            if has_io:
                for output in ','.join(args.output).split(','):
                    self.shell.push({output:self.Matlab.get_variable(output)})
            else:
                raise RuntimeError(no_io_str)

_loaded = False
def load_ipython_extension(ip, **kwargs):
    """Load the extension in IPython."""
    global _loaded
    if not _loaded:
        ip.register_magics(MatlabMagics(ip, **kwargs))
        _loaded = True

def unload_ipython_extension(ip):
    global _loaded
    if _loaded:
        magic = ip.magics_manager.registry.pop('MatlabMagics')
        magic.Matlab.stop()

<commit_msg>unload_ext behaves correctly<commit_after>"""

matlab_magic
============

Magic command interface for interactive work with Matlab(R) via the pymatbridge

Note
====
Thanks to Max Jaderberg for his work on pymatbridge.

"""

import sys, os
import tempfile
from glob import glob
from shutil import rmtree
from getopt import getopt
from urllib2 import URLError

import numpy as np
try:
    import scipy.io as sio
    has_io = True
except ImportError:
    has_io = False
    no_io_str = "Must have scipy.io to perform i/o"
    no_io_str += "operations with the Matlab session"

from IPython.core.displaypub import publish_display_data
from IPython.core.magic import (Magics, magics_class, cell_magic, line_magic,
                                line_cell_magic, needs_local_scope)
from IPython.testing.skipdoctest import skip_doctest
from IPython.core.magic_arguments import (argument, magic_arguments,
                                          parse_argstring)
from IPython.utils.py3compat import str_to_unicode, unicode_to_str, PY3

import pymatbridge as pymat

class MatlabInterperterError(RuntimeError):
    """
    Some error occurs while matlab is running
    """
    def __init__(self, line, err):
        self.line = line
        self.err = err

    def __unicode__(self):
        s = "Failed to parse and evaluate line %r.\n Matlab error message: %r"%\
                (self.line, self.err)
        return s

    if PY3:
        __str__ = __unicode__
    else:
        def __str__(self):
            return unicode_to_str(unicode(self), 'utf-8')

@magics_class
class MatlabMagics(Magics):
    """
    A set of magics for interactive work with Matlab(R).
    """
    def __init__(self, shell,
                 matlab='matlab',
                 maxtime=60,
                 pyconverter=np.asarray,
                 cache_display_data=False):
        """
        Parameters
        ----------

        shell : IPython shell

        matlab : str
            The system call to start a matlab session. Allows you to choose a
            particular version of matlab if you want

        maxtime : float
           The maximal time to wait for responses for matlab (in seconds).
           Default: 10 seconds.

        pyconverter : callable
            To be called on matlab variables returning into the ipython
            namespace

        cache_display_data : bool
            If True, the published results of the final call to R are
            cached in the variable 'display_cache'.

        """
        super(MatlabMagics, self).__init__(shell)
        self.cache_display_data = cache_display_data

        self.Matlab = pymat.Matlab(matlab, maxtime=maxtime)
        self.Matlab.start()
        self.pyconverter = pyconverter

    def __del__(self):
        """shut down the Matlab server when the object dies.

        2FIX: this seems to not be called when ipython terminates. bleah.
        """
        try:
            self.Matlab.stop()
        except:
            raise

    def eval(self, line):
        """
        Parse and evaluate a single line of matlab
        """
        run_dict = self.Matlab.run_code(line, maxtime=self.Matlab.maxtime)

        if run_dict['success'] == 'false':
            raise MatlabInterperterError(line, run_dict['content']['stdout'])

        # This is the matlab stdout:
        return run_dict

    def set_matlab_var(self, name, value):
        """
        Set up a variable in Matlab workspace
        """
        run_dict = self.Matlab.run_func("pymat_set_variable.m",
                                        {'name':name, 'value':value},
                                        maxtime=self.Matlab.maxtime)

        if run_dict['success'] == 'false':
            raise MatlabInterperterError(line, run_dict['content']['stdout'])

    @magic_arguments()
    @argument(
        '-i', '--input', action='append',
        help='Names of input variable from shell.user_ns to be assigned to Matlab variables of the same names after calling self.pyconverter. Multiple names can be passed separated only by commas with no whitespace.'
        )

    @argument(
        '-o', '--output', action='append',
        help='Names of variables to be pushed from matlab to shell.user_ns after executing cell body and applying self.Matlab.get_variable(). Multiple names can be passed separated only by commas with no whitespace.'
        )

    @argument(
        '-s', '--silent', action='store_true',
        help='Do not display text output of MATLAB command'
        )

    @argument(
        'code',
        nargs='*',
        )

    @needs_local_scope
    @line_cell_magic
    def matlab(self, line, cell=None, local_ns=None):
        """

        Execute code in matlab

        """
        args = parse_argstring(self.matlab, line)

        # arguments 'code' in line are prepended to
        # the cell lines

        if cell is None:
            code = ''
            return_output = True
            line_mode = True
        else:
            code = cell
            return_output = False
            line_mode = False

        code = ' '.join(args.code) + code

        if local_ns is None:
            local_ns = {}

        if args.input:
            if has_io:
                for input in ','.join(args.input).split(','):
                    try:
                        val = local_ns[input]
                    except KeyError:
                        val = self.shell.user_ns[input]

                    # To make an array JSON serializable
                    if (isinstance(val, np.ndarray)):
                        val = val.tolist()

                    self.set_matlab_var(input, val)

            else:
                raise RuntimeError(no_io_str)

        text_output = ''
        #imgfiles = []

        try:
            if line_mode:
                e_s = "There was an error running the code:\n %s"%line
                result_dict = self.eval(line)
            else:
                e_s = "There was an error running the code:\n %s"%code
                result_dict = self.eval(code)
        except URLError:
            e_s += "\n-----------------------"
            e_s += "\nAre you sure Matlab is started?"
            raise RuntimeError(e_s)

        text_output += result_dict['content']['stdout']
        # Figures get saved by matlab in reverse order...
        imgfiles = result_dict['content']['figures'][::-1]
        data_dir = result_dict['content']['datadir']

        display_data = []
        if text_output and not args.silent:
            display_data.append(('MatlabMagic.matlab',
                                 {'text/plain':text_output}))

        for imgf in imgfiles:
            if len(imgf):
                # Store the path to the directory so that you can delete it
                # later on:
                image = open(imgf, 'rb').read()
                display_data.append(('MatlabMagic.matlab',
                                     {'image/png': image}))

        for tag, disp_d in display_data:
            publish_display_data(tag, disp_d)

        # Delete the temporary data files created by matlab:
        if len(data_dir):
            rmtree(data_dir)

        if args.output:
            if has_io:
                for output in ','.join(args.output).split(','):
                    self.shell.push({output:self.Matlab.get_variable(output)})
            else:
                raise RuntimeError(no_io_str)

_loaded = False
def load_ipython_extension(ip, **kwargs):
    """Load the extension in IPython."""
    global _loaded
    if not _loaded:
        ip.register_magics(MatlabMagics(ip, **kwargs))
        _loaded = True

def unload_ipython_extension(ip):
    global _loaded
    if _loaded:
        magic = ip.magics_manager.registry.pop('MatlabMagics')
        magic.Matlab.stop()
        _loaded = False

<|endoftext|>


<commit_before>
from tastypie.validation import Validation

class LogValidation(Validation):
    def is_valid(self, bundle, request=None):
        """
        Performs a check on ``bundle.data``to ensure it is valid.

        If the data is valid, an empty list (all valid) will be returned. If
        not, a list of errors will be returned.
        """

        data = bundle.data
        if data is None:
            data = {}

        errors = {}

        # Validate datetime
        if 'datetime' not in data:
            errors['datetime'] = 'Datetime not included in request.'
        else:
            DATETIME_REGEX = re.compile('^(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})(T|\s+)(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2}).*?$')
            match = DATETIME_REGEX.search(data['datetime'])
            if match:
                dt_data = match.groupdict()
                try:
                    datetime_obj = datetime(int(dt_data['year']), int(dt_data['month']), int(dt_data['day']),
                                        int(dt_data['hour']), int(dt_data['minute']), int(dt_data['second']))
                except ValueError:
                    errors['datetime'] = "ValueError: Datetime is wrong."
            else:
                errors['datetime'] = 'Datetime badly formatted'

        # Validate severity
        if 'severity' not in data:
            errors['severity'] = 'Severity level not inluded in request.'
        else:
            if data['severity'] < 0 or data['severity'] > 7:
                errors['severity'] = "Not supported severity level."

        return errors

<commit_msg>Added function which is called on if data is malformed. It posts a 'monlog' log in monlog to notify user<commit_after>
from tastypie.validation import Validation
from log.models import LogMessage
from django.contrib.auth.models import User
import datetime
import simplejson as json

class LogValidation(Validation):
    def log_malformed_data(self, data, errors):
        """
        If data provided by client is corrupt or malformed, we handle it here
        by adding an extra log to the system by monlog, notifying the user about
        the problem.
        """
        current_date = datetime.datetime.now()
        desc = json.dumps(data)
        monlog_user = User.objects.get(pk=1)
        severity = 4;
        server_ip = "127.0.0.1"

        if 'datetime' in errors:
            # We found malformed datetime
            log = LogMessage(datetime = current_date, long_desc = desc, short_desc = "Monlog: Discovered malformed datetime in log message.", application = monlog_user, server_ip = server_ip, severity = severity)
            log.save()

        elif 'severity' in errors:
            # We found incorrect severity
            log = LogMessage(datetime = current_date, long_desc = desc, short_desc = "Monlog: Discovered incorrect severity in log message.", application = monlog_user, server_ip = server_ip, severity = severity)
            log.save()

    def is_valid(self, bundle, request=None):
        """
        Performs a check on ``bundle.data``to ensure it is valid.

        If the data is valid, an empty list (all valid) will be returned. If
        not, a list of errors will be returned.
        """

        data = bundle.data
        if data is None:
            data = {}

        errors = {}

        # Validate datetime
        if 'datetime' not in data:
            errors['datetime'] = 'Datetime not included in request.'
        else:
            DATETIME_REGEX = re.compile('^(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})(T|\s+)(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2}).*?$')
            match = DATETIME_REGEX.search(data['datetime'])
            if match:
                dt_data = match.groupdict()
                try:
                    datetime_obj = datetime(int(dt_data['year']), int(dt_data['month']), int(dt_data['day']),
                                        int(dt_data['hour']), int(dt_data['minute']), int(dt_data['second']))
                except ValueError:
                    errors['datetime'] = "ValueError: Datetime is wrong."
            else:
                errors['datetime'] = 'Datetime badly formatted'

        # Validate severity
        if 'severity' not in data:
            errors['severity'] = 'Severity level not inluded in request.'
        else:
            if data['severity'] < 0 or data['severity'] > 7:
                errors['severity'] = "Not supported severity level."
        if len(errors) > 0:
            self.log_malformed_data(data, errors)

        return errors

<|endoftext|>