Пример #1
0
    def post(self):
        """
        POST Method for Event. Login required

        :return: JSON response
        """
        try:
            self.validate_fields(Event.required_fields(), request.form)
        except ValueError:
            return self.response(400, 'Required fields: ' + ' '.join(Event.required_fields()))

        params = self.get_form_values(Event.get_columns(), request.form)

        country = Country.query.filter_by(iso_code=str(params['country_id'].upper())).first()
        if country is None:
            return self.response(400, "The country_id doesn't exists")

        if params['category_id']:
            category = EventCategory.query.filter_by(id=params['category_id']).first()
            if category is None:
                return self.response(400, "The category_id doesn't exists")

        new_event = Event(
            title=params['title'],
            description=params['description'],
            category_id=params['category_id'],
            country_id=country.id,
        )
        new_event.save()

        return self.response(201)
Пример #2
0
    def put(self, event_id):
        """
        PUT Method for Event. Login required

        :param event_id:
        :return: JSON response
        """
        event = Event.query.filter_by(id=event_id).first()
        if event is None:
            return self.response(404)

        params = self.get_form_values(Event.get_columns(), request.form)

        country = Country.query.filter_by(iso_code=str(params['country_id'].upper())).first()
        if country is None:
            return self.response(400, "The country_id doesn't exists")

        if params['title']:
            event.title = params['title']
        if params['description']:
            event.description = params['description']
        if params['category_id']:
            event.category_id = params['category_id']
        if params['country_id']:
            event.country_id = country.id

        event.save()

        return self.response(200)