示例#1
0
    def create_test_observation(self, plant_id, user_id):
        # create and insert new observation
        observation = Observation(user_id=user_id,
                                  date=self.test_observation['date'],
                                  plant_id=plant_id,
                                  notes=self.test_observation['notes'])
        observation.insert()

        return observation.id
示例#2
0
    def post_plant_observation_api(jwt):
        '''
        Handles API POST requests for adding new observation.
        '''

        # get request body
        body = request.get_json()

        # get user table id from session or jwt
        if 'profile' in session:
            user_id = session['profile']['user_table_id']
        else:
            auth0_user_id = jwt['sub']
            user_id = User.query.filter_by(
                user_id=auth0_user_id).one_or_none().id

        # load observation body data
        plant_id = body.get('plantID')
        date = body.get('date')
        notes = body.get('notes')

        # ensure required fields have data
        if ((date == '') or (plant_id == '')):
            abort(422)

        # create a new observation
        observation = Observation(user_id=user_id,
                                  date=date,
                                  plant_id=plant_id,
                                  notes=notes)

        try:
            # add observation to the database
            observation.insert()
        except Exception as e:
            print('ERROR: ', str(e))
            abort(422)

        # flash success message
        flash('Observation successfully created!')

        # return observation
        return jsonify({'success': True, 'observation': observation.format()})