예제 #1
0
파일: core.py 프로젝트: exhuma/lost-tracker
def store_registration(mailer, session, data):
    """
    Stores a registration to the database.

    The *data* dictionary contains the following items (all strings):

        * group_name
        * contact_name
        * email
        * tel
        * time: The time of day that the group begins the "run".
        * comments
        * num_vegetarians
        * num_participants
        * user_id: The ID of the user that made the registration
    """
    qry = Group.query.filter_by(name=data['group_name'])
    check = qry.first()
    if check:
        raise ValueError('Group {} already registered'.format(
            data['group_name']))
    else:
        key = os.urandom(50).encode('base64').replace('/', '')[0:20]
        # Regenerate keys if needed (just in case to avoid duplicates).
        qry = Group.query.filter_by(confirmation_key=key)
        check_key = qry.first()
        while check_key:
            key = os.urandom(50).encode('base64').replace('/', '')[0:20]
            qry = Group.query.filter_by(confirmation_key=key)
            check_key = qry.first()

        new_grp = Group(data['group_name'],
                        data['contact_name'],
                        data['tel'],
                        None,
                        data['time'],
                        data['comments'],
                        key,
                        data['user_id'])
        order = start_time_to_order(data['time'])
        new_grp.order = _get_unique_order(Group, order)
        new_grp.num_vegetarians = int(data['num_vegetarians'])
        new_grp.num_participants = int(data['num_participants'])
        new_grp.email = data['email']

        session.add(new_grp)
        try:
            session.flush()
        except IntegrityError:
            session.rollback()
            LOG.exception('Error while adding the new group {0}'.format(
                data['group_name']))
            raise ValueError('Error while adding the new group {0}'.format(
                data['group_name']))

        return key
예제 #2
0
 def start_time(self, value):
     self._start_time = value
     if value:
         self.order = _get_unique_order(Group, start_time_to_order(value))
     else:
         self.order = _get_unique_order(Group, 0)
예제 #3
0
def store_registration(session, data, url, needs_confirmation=True):
    """
    Stores a registration to the database.

    The *data* dictionary contains the following items (all strings):

        * group_name
        * contact_name
        * email
        * tel
        * time
        * comments

    If *needs_confirmation* is true (the default), this method will store the
    reservation as "not yet confirmed". An e-mail will be sent out to the
    address specified in the *email* field. The e-mail will contain a link to
    ``/confirm/<key>`` where ``<key`` is a randomly generated string.

    @franky: implement
    @franky: urllib.quote_plus(os.urandom(50).encode('base64')[0:30])
    @franky: See ``_external`` at
             http://flask.pocoo.org/docs/api/#flask.url_for
    @franky: The "key" should be unique in the DB. Generate new keys as long as
             duplicates are found in the DB.
    mailing with python: https://pypi.python.org/pypi/Envelopes/0.4
    """
    qry = Group.query.filter_by(name=data['group_name'])
    check = qry.first()
    if check:
        raise ValueError('Group {} already registered'.format(
            data['group_name']))
    else:
        key = os.urandom(50).encode('base64').replace('/', '')[0:20]
        qry = Group.query.filter_by(confirmation_key=key)
        check_key = qry.first()
        while check_key:
            key = os.urandom(50).encode('base64').replace('/', '')[0:20]
            qry = Group.query.filter_by(confirmation_key=key)
            check_key = qry.first()

        new_grp = Group(data['group_name'],
                        data['contact_name'],
                        data['tel'],
                        None,
                        data['time'],
                        data['email'],
                        data['comments'],
                        key
                        )
        new_grp.order = start_time_to_order(data['time'])

        session.add(new_grp)
        try:
            session.flush()
        except IntegrityError:
            session.rollback()
            LOG.exception('Error while adding the new group {0}'.format(
                data['group_name']))
            raise ValueError('Error while adding the new group {0}'.format(
                data['group_name']))

        if needs_confirmation:
            confirm_link = '{}/{}'.format(url, urllib.quote_plus(key))
            send('confirm',
                 to=(data['email'], data['contact_name']),
                 data={
                     'confirmation_link': confirm_link
                 })
        return True