Esempio n. 1
0
def transition_old_state(request):
    old_id = request.matchdict['old_id']
    try:
        old = m.get_old(old_id)
    except NoResultFound:
        request.response.status = 404
        return {'error': 'No OLD with supplied ID'}
    payload, error = get_json_payload(request)
    if error:
        return error
    try:
        new_state, error = validate_old_state(payload.get('state'))
        if error:
            return {'error': error}
        if new_state == old.state:
            updated_old = old
        else:
            new_state, error = validate_state_transition(old.state, new_state)
            if error:
                return {'error': error}
            updated_old = m.update_old(old, state=new_state)
    except m.DTValueError as e:
        request.response.status = 400
        return {'error': str(e)}
    except Exception:
        request.response.status = 500
        return {'error': 'Internal server error'}
    request.response.status = 200
    return m.serialize_old(updated_old)
Esempio n. 2
0
def read_old(request):
    old_id = request.matchdict['old_id']
    try:
        old = m.get_old(old_id)
    except NoResultFound:
        request.response.status = 404
        return {'error': 'No OLD with supplied ID'}
    return m.serialize_old(old)
Esempio n. 3
0
def enqueue_command(request):
    payload, error = get_json_payload(request)
    if error:
        return error
    old_id = payload.get('old_id')
    if not old_id:
        request.response.status = 400
        return {'error': 'OLD ID is required'}
    try:
        old = m.get_old(old_id)
    except NoResultFound:
        request.response.status = 404
        return {'error': 'No OLD with supplied ID'}
    cmd, status = m.enqueue_sync_old_command(old_id)
    request.response.status = 201
    if status == 'found':
        request.response.status = 200
    return m.serialize_sync_old_command(cmd)
Esempio n. 4
0
def update_old(request):
    old_id = request.matchdict['old_id']
    try:
        old = m.get_old(old_id)
    except NoResultFound:
        request.response.status = 404
        return {'error': 'No OLD with supplied ID'}
    payload, error = get_json_payload(request)
    if error:
        return error
    # Cannot update slug (for now, too complicated)
    try:
        payload = {
            'name':
            str_or_none(payload.get('name', old.name)),
            'leader':
            str_or_none(payload.get('leader', old.leader)),
            'username':
            str_or_none(payload.get('username', old.username)),
            'password':
            str_or_none(payload.get('password', old.password)),
            'is_auto_syncing':
            boolean(payload.get('is_auto_syncing', old.is_auto_syncing))
        }
        if payload == {attr: getattr(old, attr) for attr in payload}:
            updated_old = old
        else:
            updated_old = m.update_old(old, **payload)
    except m.DTValueError as e:
        request.response.status = 400
        return {'error': str(e)}
    except Exception:
        request.response.status = 500
        return {'error': 'Internal server error'}
    request.response.status = 200
    return m.serialize_old(updated_old)
Esempio n. 5
0
    def test_old_api(self):
        import dativetopserver.models as m
        from sqlalchemy.orm.exc import NoResultFound
        self.assertEqual([], self.session.query(m.OLD).all())

        # Create an initial OLD, supplying just the slug
        old1 = m.create_old('oka')
        old_service = m.get_old_service()
        self.assertEqual('oka', old1.slug)
        self.assertEqual('oka', old1.name)
        self.assertIsNone(old1.leader)
        self.assertIsNone(old1.username)
        self.assertIsNone(old1.password)
        self.assertFalse(old1.is_auto_syncing)
        self.assertEqual(m.old_state.not_synced, old1.state)

        # Create a second OLD, supplying everything
        old2 = m.create_old(
            'bla',
            name='Blackfoot',
            leader='https://do.onlinelinguisticdatabase.org/blaold',
            username='******',
            password='******',
            is_auto_syncing=True)
        self.assertEqual('bla', old2.slug)
        self.assertEqual('Blackfoot', old2.name)
        self.assertEqual('https://do.onlinelinguisticdatabase.org/blaold',
                         old2.leader)
        self.assertEqual('someuser', old2.username)
        self.assertEqual('somepassword', old2.password)
        self.assertTrue(old2.is_auto_syncing)
        old2_history_id = old2.history_id

        # Update the second OLD
        new_old2 = m.update_old(old2, leader='abc')
        self.assertEqual('abc', new_old2.leader)
        for attr in [
                'slug', 'name', 'username', 'password', 'is_auto_syncing'
        ]:
            self.assertEqual(getattr(old2, attr), getattr(new_old2, attr))
        olds = self.session.query(m.OLD).all()
        self.assertEqual(3, len(olds))
        self.assertEqual(2, len([o for o in olds if o.end > m.get_now()]))
        self.assertEqual(2, len(m.get_olds()))
        self.assertEqual(new_old2, m.get_old(old2_history_id))

        # Attempting to create an OLD with the slug of an existing OLD will throw
        try:
            m.create_old(new_old2.slug)
        except Exception as e:
            self.assertIsInstance(e, m.DTValueError)

        # Delete the second OLD
        deleted_old2 = m.delete_old(new_old2)
        self.assertGreater(m.get_now(), deleted_old2.end)
        self.assertEqual(1, len(m.get_olds()))
        try:
            m.get_old(old2_history_id)
        except Exception as e:
            self.assertIsInstance(e, NoResultFound)

        # After deleting the second OLD, we can reuse its slug.
        # Let's transition it through some plausible state transitions.
        blaold = m.create_old(new_old2.slug)
        self.assertEqual(m.old_state.not_synced,
                         m.get_old(blaold.history_id).state)
        blaold_syncing = m.transition_old(blaold, m.old_state.syncing)
        self.assertEqual(m.old_state.syncing,
                         m.get_old(blaold.history_id).state)
        blaold_synced = m.transition_old(blaold_syncing, m.old_state.synced)
        self.assertEqual(m.old_state.synced,
                         m.get_old(blaold.history_id).state)
        blaold_not_synced = m.transition_old(blaold_synced,
                                             m.old_state.not_synced)
        self.assertEqual(m.old_state.not_synced,
                         m.get_old(blaold.history_id).state)

        # We cannot transition a deactivated OLD
        try:
            m.transition_old(blaold_synced, m.old_state.not_synced)
        except Exception as e:
            self.assertIsInstance(e, m.DTValueError)

        # Transition a new OLD through a "sync failed" flow
        zinc_old = m.create_old('zinc')
        self.assertEqual(m.old_state.not_synced,
                         m.get_old(zinc_old.history_id).state)
        zinc_old_syncing = m.transition_old(zinc_old, m.old_state.syncing)
        self.assertEqual(m.old_state.syncing,
                         m.get_old(zinc_old.history_id).state)
        zinc_old_failed_to_sync = m.transition_old(zinc_old_syncing,
                                                   m.old_state.failed_to_sync)
        self.assertEqual(m.old_state.failed_to_sync,
                         m.get_old(zinc_old.history_id).state)
        zinc_old_not_synced = m.transition_old(zinc_old_failed_to_sync,
                                               m.old_state.not_synced)
        self.assertEqual(m.old_state.not_synced,
                         m.get_old(zinc_old.history_id).state)