Ejemplo n.º 1
0
def membership_type_choices():
    # set choices for membership type according to current user's access rights
    junior = current_user.is_access(UserRole.jd_admin)
    choices = MembershipType.choices(extra=[(99, 'adult (!=junior)')],
                                     blank=True)
    if junior:
        choices = [c for c in choices if c[1] == 'junior']
    return choices
Ejemplo n.º 2
0
class QueryForm(FlaskForm):
    months = [(m, calendar.month_name[m]) for m in range(1, 13)]
    number = MyStringField(label='number', db_map='Member.number')
    last_updated = MyStringField(label='last updated',
                                 db_map='Member.last_updated',
                                 validators=[validate_date_format])
    status = MySelectField(label='status',
                           choices=[],
                           coerce=MemberStatus.coerce,
                           db_map='Member.status')
    member_type = MySelectField(label='member type',
                                choices=MembershipType.choices(extra=[
                                    (99, 'adult (!=junior)')
                                ],
                                                               blank=True),
                                coerce=MembershipType.coerce,
                                db_map='Member.member_type')
    start_date = MyStringField(label='start date',
                               db_map='Member.start_date',
                               validators=[validate_date_format])
    end_date = MyStringField('end date',
                             db_map='Member.end_date',
                             validators=[validate_date_format])
    birth_date = MyStringField('birth date',
                               db_map='Member.birth_date',
                               validators=[validate_date_format])
    post_code = MyStringField(label='post code', db_map='Address.post_code')
    country = MySelectField(label='country',
                            choices=country_choices(
                                blank=True,
                                extra=[(9999, 'overseas (!=United Kingdom)')]),
                            coerce=int,
                            db_map='Address.Country.id')
    payment_type = MySelectField(label='payment type',
                                 choices=PaymentType.choices(blank=True),
                                 coerce=PaymentMethod.coerce,
                                 db_map='Member.Payment.type')
    payment_method = MySelectField(label='payment method',
                                   choices=PaymentMethod.choices(blank=True),
                                   coerce=PaymentMethod.coerce,
                                   db_map='Member.last_payment_method')
    payment_date = MyStringField(label='payment date',
                                 db_map='Member.Payment.date',
                                 validators=[validate_date_format])
    payment_comment = MyStringField(label='payment comment',
                                    db_map='Payment.comment')
    renewal_activated = MyStringField(label='renewal activated',
                                      db_map='Member.renewal_activated()')
    comms = MySelectField(label='comms',
                          choices=CommsType.choices(blank=True),
                          coerce=CommsType.coerce,
                          db_map='Member.comms')
    comms_status = MySelectField(label='comms status',
                                 choices=CommsStatus.choices(blank=True),
                                 coerce=CommsType.coerce,
                                 db_map='Member.comms_status')
    birth_month = MySelectField(label='birth month',
                                choices=[(0, '')] + months,
                                coerce=int,
                                db_map='Member.birth_date.birth_month()')
    age = MyStringField(label='age', db_map='Member.birth_date.age()')
    current_action = MySelectField(label='current action',
                                   choices=MemberAction.choices(blank=True),
                                   coerce=MemberAction.coerce,
                                   db_map='Action.action')
    action_status = MySelectField(label='action status',
                                  choices=ActionStatus.choices(blank=True),
                                  coerce=ActionStatus.coerce,
                                  db_map='Action.status')
    action_date = MyStringField(label='action date',
                                db_map='Action.date',
                                validators=[validate_date_format])
    action_comment = MyStringField(label='action comment',
                                   db_map='Action.comment')
    comment_date = MyStringField(label='comment date',
                                 db_map='Comment.date',
                                 validators=[validate_date_format])
    comment = MyStringField(label='comment', db_map='Comment.comment')

    first_name = MyStringField(label='first name', db_map='Member.first_name')
    last_name = MyStringField(label='last name', db_map='Member.last_name')
    email = MyStringField(label='email', db_map='Member.email')
    fan_id = MyStringField(label='fan id', db_map='Member.season_ticket_id')

    query_clauses = HiddenField(label='query')
    display_fields = MultiCheckboxField(label='fields to extract ...',
                                        choices=list(
                                            enumerate(extract_fields_map)))
    submit = SubmitField(label='Submit')

    def query_fields(self):
        return [
            self.number, self.last_updated, self.status, self.member_type,
            self.start_date, self.end_date, self.birth_date, self.comms,
            self.comms_status, self.birth_month, self.age, self.current_action,
            self.action_date, self.action_status, self.action_comment,
            self.comment_date, self.comment, self.payment_type,
            self.payment_method, self.payment_date, self.payment_comment,
            self.first_name, self.last_name, self.email, self.post_code,
            self.country, self.renewal_activated, self.fan_id
        ]

    def set_status_choices(self):
        self.status.choices = status_choices()

    def find_members(self):
        query_clauses = select_fields_to_query(self.query_fields(), 'Member')
        return query_clauses

    def get_updates(self):
        updates = select_fields_to_update(self.query_fields(), 'Member')
Ejemplo n.º 3
0
class MemberDetailsForm(FlaskForm):
    full_name = StringField(label='Full Name')
    return_url = HiddenField(label='Return URL')
    member_number = HiddenField(label='Member Number')
    dt_number = StringField(label='Id')
    status = MySelectField(label='Status',
                           choices=MemberStatus.choices(),
                           coerce=MemberStatus.coerce)
    type = MySelectField(label='Type',
                         choices=MembershipType.choices(),
                         coerce=MembershipType.coerce)
    start_date = DateField(label='Start')
    end_date = DateField(label='End')
    birth_date = DateField(label='DoB', validators=[Optional()])
    age = HiddenField(label='Age')
    last_updated = StringField(label='Updated')
    access = MySelectField(label='Access',
                           choices=UserRole.choices(blank=False, ),
                           coerce=UserRole.coerce)
    fan_id = StringField(label='AFCW Fan ID')
    external_access = MySelectField(label='External access',
                                    choices=ExternalAccess.choices(),
                                    coerce=ExternalAccess.coerce)
    payment_method = MySelectField(label='Pay method',
                                   choices=PaymentMethod.choices(blank=True),
                                   coerce=PaymentMethod.coerce)
    title = MySelectField(label='Title',
                          choices=Title.choices(blank=True),
                          coerce=Title.coerce)
    first_name = StringField(label='First', validators=[InputRequired()])
    last_name = StringField(label='Last', validators=[InputRequired()])
    sex = MySelectField(label='Sex',
                        choices=Sex.choices(blank=True),
                        coerce=int)

    line1 = StringField(label='Address line 1')
    line2 = StringField(label='Address line 2')
    line3 = StringField(label='Address line 3')
    city = StringField(label='City')
    state = MySelectField(label='State',
                          choices=state_choices(blank=True),
                          coerce=int)
    post_code = StringField(label='Post Code')
    county = MySelectField(label='County',
                           choices=county_choices(blank=True),
                           coerce=int)
    country = MySelectField(label='Country',
                            choices=country_choices(),
                            coerce=int)

    home_phone = StringField(label='Home Phone')
    mobile_phone = StringField(label='Mobile')
    email = StringField(
        label='Email ',
        validators=[Optional(), Email("Invalid email address")])
    comms = MySelectField(label='Comms ',
                          choices=CommsType.choices(),
                          coerce=CommsType.coerce)
    comms_status = MySelectField(label='Status ',
                                 choices=CommsStatus.choices(),
                                 coerce=CommsStatus.coerce)

    submit = SubmitField(label='Save')

    payment_list = FieldList(FormField(PaymentItemForm))
    action_list = FieldList(FormField(ActionItemForm))
    comment_list = FieldList(FormField(CommentItemForm))

    jd_email = StringField(
        label='JD Email ',
        validators=[Optional(), Email("Invalid email address")])
    jd_gift = MySelectField(label='JD Gift',
                            choices=JuniorGift.choices(blank=True),
                            coerce=JuniorGift.coerce)

    def populate_member(self, member_number, return_url, copy=False):
        self.return_url.data = return_url
        new_member = member_number == 0
        if new_member or copy:
            member = get_new_member()
        else:
            member = get_member(member_number)
        if copy:
            base_member = get_member(member_number)
            new_member = True
            member.last_name = base_member.last_name
            member.address = base_member.address
            member.home_phone = base_member.home_phone
            member.mobile_phone = base_member.mobile_phone
            member.email = base_member.email
            member.comms = base_member.comms
        address = member.address
        self.member_number.data = str(member.number)
        self.dt_number.data = member.dt_number()
        self.status.data = member.status.value
        self.type.data = member.member_type.value
        self.start_date.data = member.start_date
        self.end_date.data = member.end_date
        self.birth_date.data = member.birth_date
        self.age.data = str(member.age()) if member.age() is not None else None
        self.last_updated.data = fmt_date(member.last_updated)
        self.access.data = member.user.role.value if member.user else 0

        self.fan_id.data = member.season_ticket_id if member.season_ticket_id else ''
        self.external_access.data = (member.external_access
                                     or ExternalAccess.none).value
        self.payment_method.data = member.last_payment_method.value if member.last_payment_method else ''

        self.full_name.data = member.full_name()
        self.title.data = member.title.value if member.title else ''
        self.first_name.data = member.first_name
        self.last_name.data = member.last_name
        self.sex.data = member.sex.value if member.sex else ''

        self.line1.data = address.line_1
        self.line2.data = address.line_2
        self.line3.data = address.line_3
        self.city.data = address.city
        self.state.data = address.state.id if address.state else 0
        self.post_code.data = address.post_code
        self.county.data = address.county.id if address.county else 0
        self.country.data = address.country.id

        self.home_phone.data = member.home_phone
        self.mobile_phone.data = member.mobile_phone
        self.email.data = member.email
        self.comms.data = member.comms.value
        self.comms_status.data = member.comms_status.value if member.comms_status else CommsStatus.all_ok

        for payment in [get_new_payment()] + member.payments:
            item_form = PaymentItemForm()
            item_form.date = payment.date
            item_form.pay_type = payment.type.value  ## nb: don't set the data attribute for select fields in a fieldlist!
            item_form.amount = payment.amount
            item_form.method = payment.method.value if payment.method else None
            item_form.comment = payment.comment or ''
            self.payment_list.append_entry(item_form)

        for action in [get_new_action(new_member)] + member.actions:
            item_form = ActionItemForm()
            if action.action:
                item_form.action = action.action.value
            if action.status:
                item_form.status = action.status.value
            item_form.date = action.date
            item_form.comment = action.comment or ''
            self.action_list.append_entry(item_form)

        for comment in [get_new_comment()] + member.comments:
            item_form = CommentItemForm()
            item_form.date = comment.date
            item_form.comment = comment.comment or ''
            self.comment_list.append_entry(item_form)

        if new_member or member.member_type == MembershipType.junior:
            if not member.junior:
                member.junior = get_junior()
            self.jd_email.data = member.junior.email or ''
            self.jd_gift.data = member.junior.gift.value if member.junior.gift else ''
        else:
            self.jd_email = self.jd_gift = None

    def validate(self):
        result = True
        new_member = int(self.member_number.data) == 0
        if not super(MemberDetailsForm, self).validate():
            return False
        if new_member:
            name = self.first_name.data + ' ' + self.last_name.data
            existing = get_members_by_name(name)
            if len(existing) > 0:
                self.first_name.errors.append('{} is already member {}'.format(
                    name, existing[0].dt_number()))
                result = False
        return result

    def save_member(self, member_number):
        member_details = {
            'title': self.title.data,
            'first_name': self.first_name.data.strip(),
            'last_name': self.last_name.data.strip(),
            'sex': self.sex.data,
            'member_type': self.type.data,
            'status': self.status.data,
            'start_date': self.start_date.data,
            'end_date': self.end_date.data,
            'birth_date': self.birth_date.data,
            'access': self.access.data,
            'external_access': self.external_access.data,
            'fan_id': self.fan_id.data,
            'payment_method': self.payment_method.data,
            'home_phone': self.home_phone.data.strip(),
            'mobile_phone': self.mobile_phone.data.strip(),
            'email': self.email.data.strip(),
            'comms': self.comms.data,
            'comms_status': self.comms_status.data,
            'line_1': self.line1.data.strip(),
            'line_2': self.line2.data.strip(),
            'line_3': self.line3.data.strip(),
            'city': self.city.data.strip(),
            'state': get_state(self.state.data),
            'post_code': self.post_code.data.strip(),
            'county': get_county(self.county.data),
            'country': get_country(self.country.data),
            'payments': [],
            'actions': [],
            'comments': []
        }
        if self.type.data == MembershipType.junior.value:
            member_details['jd_mail'] = self.jd_email.data.strip()
            member_details['jd_gift'] = self.jd_gift.data

        for payment in self.payment_list.data:
            if payment['amount']:
                member_details['payments'].append(payment)

        for action in self.action_list.data:
            if action['action'] > 0:
                member_details['actions'].append(action)

        for comment in self.comment_list.data:
            if comment['comment']:
                member_details['comments'].append(comment)

        return save_member_details(member_number, member_details)