예제 #1
0
    def activity_update(self, activity_id, request_object):

      if not request_object.data:
        logger.error('No data was submitted with the activity_create request')
        return status_.status_400('You didn\'t include any `data` with your request.'), 400

      activity_ = self.activity_get(activity_id)

      """
      Prepare the content of the request_object to use in creating an Activity
      """
      activity = json.loads(request_object.data)

      if hasattr(activity_, 'name'):
        activity_.name = sanitize.sanitize_string(activity.get('name', activity_.name))

      if hasattr(activity_, 'description'):
        activity_.description = sanitize.sanitize_string(activity.get('description', activity_.description))

      if hasattr(activity_, 'result'):
        activity_.result = sanitize.sanitize_string(activity.get('result', activity_.result))

      if hasattr(activity_, 'template_id'):
        activity_.template_id = sanitize.sanitize_integer(activity.get('template_id', activity_.template_id))

      if hasattr(activity_, 'status'):
        activity_.status = sanitize.sanitize_boolean(activity.get('status', activity_.status))

      if hasattr(activity_, 'updated'):
        activity_.updated = activity.get('updated', activity_.updated)

      db.session.commit()

      return activity_
예제 #2
0
    def activity_create(self, request_object):

      if not request_object.data:
        logger.error('No data was submitted with the activity_create request')
        return status_.status_400('You didn\'t include any `data` with your request.'), 400

      """
      Prepare the content of the request_object to use in creating an Activity
      """
      activity = json.loads(request_object.data)

      new_activity = {
        'name': sanitize.sanitize_string(statistic_content.get('name', '')),
        'description': sanitize.sanitize_string(statistic_content.get('description', '')),
        'result': sanitize.sanitize_string(statistic_content.get('result', '')),
        'status': 'pending',
        'template_id': sanitize.sanitize_integer(activity.get('template_id', ''))
      }

      activity_ = Activity(**new_activity)

      db.session.add(activity_)
      db.session.commit()

      return activity_
예제 #3
0
    def statistic_update(self, template_id, statistic_id, request_object):

        explicitly_allowed_templates_ = self.explicitly_allowed_templates()
        if not template_id in explicitly_allowed_templates_:
          logger.error('User %d update Statistic request failed because they are\'t allowed to modify the associated Template', \
              self.current_user.id)
          return status_.status_401('You are\'t allowed to modify the Template you\'re trying to add a statistic to'), 401

        """
        Make sure that some data was submitted before proceeding
        """
        if not request_object.data:
          logger.error('User %d update Statistic request failed because they didn\'t submit any `data` with their request', \
              self.current_user.id)
          return status_.status_400('You didn\'t include any `data` with your request.'), 400

        """
        Make sure we can use the request data as json
        """
        statistic_ = Statistic.query.get(statistic_id)

        if not statistic_.id:
          logger.error('User %d Statistic request failed because Statistic does\'t exist', \
              self.current_user.id)
          return status_.status_404('The Statistic you\'re looking for doesn\'t exist'), 404

        statistic_content = json.loads(request_object.data)

        if hasattr(statistic_, 'name'):
          statistic_.name = sanitize.sanitize_string(statistic_content.get('name', statistic_.name))

        if hasattr(statistic_, 'units'):
          statistic_.units = sanitize.sanitize_string(statistic_content.get('units', statistic_.units))

        if hasattr(statistic_, 'function'):
          statistic_.function = sanitize.sanitize_string(statistic_content.get('function', statistic_.function))

        if hasattr(statistic_, 'field_id'):
          statistic_.field_id = sanitize.sanitize_integer(statistic_content.get('field_id', statistic_.field_id))

        if hasattr(statistic_, 'status'):
          statistic_.status = sanitize.sanitize_boolean(statistic_content.get('status', statistic_.status))

        db.session.commit()

        return statistic_
예제 #4
0
    def statistic_create(self, template_id, request_object):

        """
        Make sure that some data was submitted before proceeding
        """
        if not request_object.data:
          logger.error('User %d new Statistic request failed because they didn\'t submit any `data` with their request', \
              self.current_user.id)
          return status_.status_400('You didn\'t include any `data` with your request.'), 400

        """
        Make sure we can use the request data as json
        """
        statistic_content = json.loads(request_object.data)

        field_id = sanitize.sanitize_integer(statistic_content.get('field_id', ''))

        explicitly_allowed_fields_ = self.explicitly_allowed_fields()
        template_fields_ = self.template_field_list(template_id)
        if not field_id in explicitly_allowed_fields_ or \
              not field_id in template_fields_:
          logger.error('User %d new Statistic request failed because they are\'t allowed to modify the associated field', \
              self.current_user.id)
          return status_.status_400('You are\'t allowed to modify the field you\'re trying to add a statistic to'), 400

        new_statistic = {
          'name': sanitize.sanitize_string(statistic_content.get('name', '')),
          'units': sanitize.sanitize_string(statistic_content.get('units', '')),
          'function': sanitize.sanitize_string(statistic_content.get('function', '')),
          'field_id': field_id
        }

        statistic_ = Statistic(**new_statistic)

        db.session.add(statistic_)
        db.session.commit()

        return statistic_
예제 #5
0
    def field_update(self, request_object, template_id, field_id):

        """
        Make sure that we have everything we need to created the
        template successfully, including things like a Name, an associated
        Application, and a Storage mechanism
        """
        if not field_id:
          logger.error('User %d update Field request failed because they didn\'t submit a Field ID with their request', \
              self.current_user.id)
          return status_.status_400('You didn\'t include a Field ID to update this field with'), 400

        """
        Fields are directly tied to Templates and really have no life of their
        own outside of Templates. Because of that we need to instantiate a
        Template object that we can work with
        """
        allowed_fields = self.allowed_fields(permission_type='write')
        if not field_id in allowed_fields:
          logger.warning('User %d with Fields %s tried to access Field %d', \
              self.current_user.id, allowed_fields, field_id)
          return status_.status_401('You can\'t edit this Field because it\'s not yours'), 401

        """
        Make sure that some data was submitted before proceeding
        """
        if not request_object.data:
          logger.error('User %d update Field request failed because they didn\'t submit any `data` with their request', \
              self.current_user.id)
          return status_.status_400('You didn\'t include any `data` with your request.'), 400

        """
        Convert our request object into usable data
        """
        field_content = json.loads(request_object.data)

        """
        Fields are directly tied to Templates and really have no life of their
        own outside of Templates. Because of that we need to instantiate a
        Template object that we can work with
        """
        field_ = Field().query.get(field_id)

        """
        Part 2: Update the fields that we have data for
        """
        if hasattr(field_, 'label'):
          field_.label = sanitize.sanitize_string(field_content.get('label', field_.label))

        if hasattr(field_, 'help'):
          field_.help = sanitize.sanitize_string(field_content.get('help', field_.help))

        if hasattr(field_, 'is_listed'):
          field_.is_listed = sanitize.sanitize_boolean(field_content.get('is_listed', field_.is_listed))

        if hasattr(field_, 'is_searchable'):
          field_.is_searchable = sanitize.sanitize_boolean(field_content.get('is_searchable', field_.is_searchable))

        if hasattr(field_, 'is_required'):
          field_.is_required = sanitize.sanitize_boolean(field_content.get('is_required', field_.is_required))

        if hasattr(field_, 'weight'):
          field_.weight = sanitize.sanitize_integer(field_content.get('weight', field_.weight))

        if hasattr(field_, 'status'):
          field_.status = sanitize.sanitize_boolean(field_content.get('status', field_.status))

        if hasattr(field_, 'options'):
          field_.options = sanitize.sanitize_string(field_content.get('options', field_.options))

        #
        # @todo
        #    We probably need to make the API capable of changing
        #    data_types, after all, PostgreSQL does it out of the box
        #
        # @see
        #    http://www.postgresql.org/docs/9.3/static/sql-altertable.html
        #

        db.session.commit()

        return field_
예제 #6
0
    def field_create(self, request_object, template_id):

        """
        Make sure that we have everything we need to created the
        template successfully, including things like a Name, an associated
        Application, and a Storage mechanism
        """
        if not template_id:
          logger.error('User %d new Field request failed because they didn\'t submit an Template ID with their request', \
              self.current_user.id)
          return status_.status_400('You didn\'t include a Template to add this field to ... or else you\'re not the admin of this Template'), 400

        """
        Make sure that some data was submitted before proceeding
        """
        if not request_object.data:
          logger.error('User %d new Field request failed because they didn\'t submit any `data` with their request', \
              self.current_user.id)
          return status_.status_400('You didn\'t include any `data` with your request.'), 400

        """
        Convert our request object into usable data
        """
        content_ = json.loads(request_object.data)

        """
        Fields are directly tied to Templates and really have no life of their
        own outside of Templates. Because of that we need to instantiate a
        Template object that we can work with
        """
        allowed_fields = self.allowed_fields(template_id)
        if not template_id in allowed_fields:
          logger.warning('User %d with Templates %s tried to access Template %d', \
              self.current_user.id, allowed_fields, template_id)
          return status_.status_401(), 401

        Template_ = Template().query.get(template_id)

        """
        To create a Field you must at least provide a name for your field
        """
        if not content_.get('name', ''):
          logger.error('User %d new Field request failed because they didn\'t submit any `name` in the `data` of their request', \
              self.current_user.id)
          return status_.status_400('You didn\'t include a `name` in the `data` of your request.'), 400

        """
        To create a Field you must at least provide a data_type for your field
        """
        if not content_.get('data_type', ''):
          logger.error('User %d new Field request failed because they didn\'t submit any `data_type` in the `data` of their request', \
              self.current_user.id)
          return status_.status_400('You didn\'t include a `data_type` in the `data` of your request.'), 400
        elif 'relationship' in content_.get('data_type', '') and not content_.get('relationship', ''):
          logger.error('User %d new Field request failed because they didn\'t submit any `data_type` in the `data` of their request', \
              self.current_user.id)
          return status_.status_400('You can\'t create a Relationship field without specifying the `relationship` ... this starts with type_'), 400


        user_defined_label = sanitize.sanitize_string(content_.get('name', ''))

        """
        If someone's creating a relationship the storage string they've specified
        needs to belong to a template that they are allowed to access
        """
        if content_.get('relationship', ''):

          relationship_storage = content_.get('relationship', '')
          storage_check = Template().query.filter_by(storage=relationship_storage).first()

          if not storage_check.id in allowed_fields:
            logger.error('User %d tried to add a Field to a Template %d which they do not own', \
                self.current_user.id, template_id)
            return status_.status_401('The Template `relationship` string you entered either doesn\'t exist or you don\'t have permission to use it'), 401

          """
          Lastly, make sure that an identical relationship doesn't already exist.
          If the type_ and the template type_ already have a relationship it will
          cause bad things to happen when searching via the API
          """
          duplicate_check = self.check_relationship_field_duplicate(Template_.fields, relationship_storage)
          if duplicate_check:
            logger.warning('User %d tried to add a duplicate relationship type', \
                self.current_user.id)
            return status_.status_400('You already defined a relationship with this Template, you cannot create two relationship fields with the same relationship table.'), 400

        new_field = {
          'label': user_defined_label,
          'name': self.generate_machine_name(user_defined_label),
          'help': sanitize.sanitize_string(content_.get('help', '')),
          'data_type': sanitize.sanitize_string(content_.get('data_type', 'text')),
          'relationship': sanitize.sanitize_string(content_.get('relationship', None)),
          'is_public': sanitize.sanitize_boolean(content_.get('is_public', False)),
          'is_visible': sanitize.sanitize_boolean(content_.get('is_visible', False)),
          'is_listed': sanitize.sanitize_boolean(content_.get('is_listed', False)),
          'is_searchable': sanitize.sanitize_boolean(content_.get('is_searchable', True)),
          'is_required': sanitize.sanitize_boolean(content_.get('is_required', False)),
          'weight': sanitize.sanitize_integer(content_.get('created', 1)),
          'status': sanitize.sanitize_boolean(content_.get('status', True)),
          'options': sanitize.sanitize_string(content_.get('options', '')),
          'templates': [Template_]
        }

        logger.debug('Checking Field, %s', new_field)

        field_ = Field(**new_field)

        db.session.add(field_)
        db.session.commit()

        logger.warning('field %s', field_)


        """
        Section 2: Relate the Template with the Field and the User with the Field
        """
        permission = {
          'read': True,
          'write': True,
          'is_admin': True
        }

        self.set_user_field_permissions(field_, permission, self.current_user)

        self.set_template_field_relationship(field_, Template_)


        """
        Section 3: Create the Field in the Template Storage, if the field will
                   hold data. Some fields, like fieldset, are only for visual
                   and aesthetic purposes and do not need added to the Storage
        """
        if not 'fieldset' in field_.data_type:
          field_storage = self.create_storage_field(Template_, field_)

          if 'relationship' in content_.get('data_type', 'text') or 'file' in content_.get('data_type', 'text'):
              field_.association = field_storage['association']
              field_.relationship = field_storage['relationship']
              db.session.commit()

        return field_