Exemple #1
0
    def post(self):
        post_data = loads(self.request.body)

        date_start = post_data.get('date_start')
        date_end = post_data.get('date_end')
        institution_id = post_data.get('institution_id')

        if not date_start or not date_end or not institution_id:
            self.set_status(400, 'Invalid Legislature')
            return

        data = {
            'date_start': date_start,
            'date_end': date_end,
            'institution_id': institution_id,
        }

        legislature = Legislature.add_legislature(self.db, data)
        self.write_json(legislature.to_dict())
    def test_can_add_legislature(self, logging_mock):
        institution = InstitutionFactory.create()

        now = datetime.utcnow()
        d1 = now.date()
        date_start = date_to_timestamp(d1)
        d2 = (now + timedelta(days=10)).date()
        date_end = date_to_timestamp(d2)

        data = {
            'institution_id': institution.id,
            'date_start': date_start,
            'date_end': date_end,
        }
        legislature = Legislature.add_legislature(self.db, data)

        expect(legislature.institution).to_equal(institution)
        expect(legislature.date_start).to_equal(d1)
        expect(legislature.date_end).to_equal(d2)
        expect(logging_mock.mock_calls).to_include(
            call.debug('Added legislature: "%s"', str(legislature))
        )
    def post(self):
        post_data = loads(self.request.body)

        date_start = post_data.get('date_start')
        date_end = post_data.get('date_end')
        institution_id = post_data.get('institution_id')

        if not date_start or not date_end or not institution_id:
            self.set_status(422, 'Invalid Legislature')
            self.write_json({'message': 'Invalid Legislature'})
            return

        data = {
            'date_start': date_start,
            'date_end': date_end,
            'institution_id': institution_id,
        }

        try:
            legislature = Legislature.add_legislature(self.db, data)
            self.write_json(legislature.to_dict())
        except IntegrityError:
            self.set_status(409, 'Legislature already exists')
            self.write_json({'message': 'Legislature already exists'})