Beispiel #1
0
def check_watchjob(watchjob_id, query_json, last_case_id):
    with database_session() as dbs:
        reader = SBKReader()

        try:
            query = json.loads(query_json)
        except ValueError:
            # JSON parsing error, just return nothing
            LOG.exception('Error parsing query JSON')
            return []

        if 'street' in query:
            address = query['street']
            newer_than_case = last_case_id

            LOG.debug('Getting all results for address {}, newer than case {}'.format(address, newer_than_case))
            new_cases = reader.get_cases(address, newer_than_case)

            LOG.debug('Found {} results'.format(len(new_cases)))
            # LOG.debug(json.dumps(new_cases, indent=2, ensure_ascii=False))

            if len(new_cases):
                new_last_case_id = new_cases[0]['id']

                watchjob = get_watchjob(dbs, watchjob_id)

                LOG.debug('The new last_case_id is {}, write it to the database'.format(new_last_case_id))

                watchjob.last_case_id = new_last_case_id
            else:
                LOG.debug('No new cases found.')

            return new_cases
        else:
            return []
Beispiel #2
0
def handle_confirm_request(token):
    """Handle confirm requests.

    Check whether the token corresponds to a user request, and if yes, create the relevant user and watchjob.
    Args:
        token (str): the given token
    """
    LOG.info(f'Received confirm request with token {log_safe(token)}')

    with database_session() as dbs:
        try:
            # TODO make sure token is valid, ie not empty

            user = confirm_request(dbs, token)
            if len(user.watchjobs) == 1:
                # user just created, send them an email
                LOG.debug(f'Created user {user.email}')

                delete_link = urllib.parse.urljoin(request.url_root, url_for('main.delete', t=user.delete_token))

                send_welcome_email.apply_async(args=[user.email, delete_link])
            else:
                LOG.debug('Added watchjob to existing user {user.email}')

            flash('Bevakningsförfrågan aktiverades!',
                  f'Framöver kommer du att få mejl på {user.email} när ärenden om din adress dyker upp.')
        except (NoResultFound, IntegrityError) as err:
            # most likely adding the user violated the unique constraint, or the token was invalid
            LOG.error(str(err))
            flash('Ogiltigt token eller något annat gick fel')
Beispiel #3
0
def handle_new_request(email, address):
    """Handle new watchjob requests submitted to the website.

    This checks the untrusted user input `email` and `address` and, if valid, creates a
    new database UserRequest record.
    Args:
        email (str): untrusted user input string for the email address
        address (str): untrusted user input string for the address
    """
    LOG.info(f'Received new request for address: {log_safe(address)}, email: {log_safe(email)}')
    with database_session() as dbs:
        try:
            assert valid_address(address), f'Got invalid address: {log_safe(address)}'
            assert valid_email(email), f'Got invalid email: {log_safe(email)}'

            # TODO:  do street/fastighet distinction
            request_token = add_request(dbs, email, watchjob_query={'street': address})
            LOG.debug(f'Request stored in database (token: {request_token}).')

            confirm_link = urllib.parse.urljoin(request.url_root, url_for('main.confirm', t=request_token))

            send_confirm_email.apply_async(args=[email, confirm_link, address])

            flash(f'Tagit emot bevakningsförfrågan. '
                  f'Aktivera den med länken som skickades till {email}.')
        except AssertionError as error:
            LOG.error(str(error))
            flash('Någonting har gått fel, tyvärr.')
Beispiel #4
0
def run_all_watchjobs():
    with database_session() as dbs:
        LOG.info('Running all watch jobs...')
        watchjobs = get_all_watchjobs(dbs)
        for watchjob in watchjobs:
            LOG.debug(watchjob)

            (check_watchjob.s(watchjob.id, watchjob.query, watchjob.last_case_id) | notify_users.s(watchjob.id)). \
                apply_async()
Beispiel #5
0
def notify_users(new_cases, watchjob_id):
    with database_session() as dbs:
        if new_cases:
            LOG.debug('There\'s new cases, get the users for watch job %s and notify them!', watchjob_id)

            watchjob = get_watchjob(dbs, watchjob_id)

            # TODO send actual emails
            LOG.debug('Sending notifications to %s', [user.email for user in watchjob.users])
        else:
            LOG.debug('Nothing to do.')

        return len(new_cases)
Beispiel #6
0
def handle_delete_request(token):
    """Handle delete requsts.

    Check whether the token corresponds to a user account, and if yes, delete the relevant user and all of the associated rows.
    Args:
        token (str): the delete token
    """
    LOG.info(f'Received delete request with token {log_safe(token)}')

    with database_session() as dbs:
        try:
            # TODO make sure token is valid, ie not empty

            user = delete_user(dbs, token)

            flash('Användarkontot raderades!',
                  f'Du kommer inte få mejl om dina bevakningar längre.')
        except (NoResultFound, IntegrityError) as err:
            # most likely adding the user violated the unique constraint, or the token was invalid
            LOG.error(str(err))
            flash('Ogiltigt token eller något annat gick fel')