예제 #1
0
 def make_mandatory_fields(self,
                           project_uuid,
                           profile_uuid,
                           item_type):
     """ makes mandatory predicates for a profile
         depending on it's type
     """
     if item_type in InputField.PREDICATES_OC:
         mandatory_predicates = InputField.PREDICATES_OC[item_type]
         exist_man_fields = InputField.objects\
                                      .filter(profile_uuid=profile_uuid,
                                              predicate_uuid__in=mandatory_predicates)[:1]
         if len(exist_man_fields) > 0:
             # at least one of the mandatory fields already exists,
             # we sould add missing mandatory fields to this field-group
             fgroup_uuid = exist_man_fields[0].fgroup_uuid
         else:
             # we don't have any mandatory fields for this profile,
             # so create a group for the mandatory fields
             fgroup_uuid = GenUUID.uuid4()
             fgroup_uuid = str(fgroup_uuid)
             fgroup = InputFieldGroup()
             fgroup.uuid = fgroup_uuid
             fgroup.project_uuid = project_uuid
             fgroup.profile_uuid = profile_uuid
             fgroup.label = 'Open Context Required Fields'
             fgroup.visibility = 'open'
             fgroup.sort = 0
             fgroup.note = 'Automatically generated group of mandatory fields for ' + item_type + ' records.'
             fgroup.save()
         new_field = 1
         for man_pred in mandatory_predicates:
             # now check to see if the current mandatory predicate is already present
             exist_man_field = InputField.objects\
                                         .filter(profile_uuid=profile_uuid,
                                                 predicate_uuid=man_pred)
             if len(exist_man_field) < 1:
                 # only create the mandatory fields if they don't already exist for this profile
                 f_uuid = GenUUID.uuid4()
                 f_uuid = str(f_uuid)
                 inp_field = InputField()
                 inp_field.uuid = f_uuid
                 inp_field.project_uuid = project_uuid
                 inp_field.profile_uuid = profile_uuid
                 inp_field.fgroup_uuid = fgroup_uuid
                 inp_field.row_num = 1
                 inp_field.col_num = 1
                 inp_field.sort = new_field
                 inp_field.predicate_uuid = man_pred
                 preset = InputField.PREDICATE_ITEMS[inp_field.predicate_uuid]
                 inp_field.label = preset['label']
                 inp_field.note = preset['note']
                 inp_field.validation = json.dumps(preset['validation'],
                                                   indent=4,
                                                   ensure_ascii=False)
                 inp_field.save()
                 new_field += 1
예제 #2
0
 def create_update_from_post(self, post, field_uuid=False):
     """ Creates or updates an input field from posted data
     """
     ok = True
     sort_val = 0
     note = ''
     action = ''
     if 'sort' in post:
         try:
             sort_val = int(float(post['sort']))
         except:
             sort_val = self.make_default_sort_val(self.profile_uuid)
     if 'label' in post:
         label = post['label'].strip()
     else:
         label = ''
     if 'note' in post:
         data_note = post['note'].strip()
     else:
         data_note = ''
     predicate_uuid = False
     if 'predicate_uuid' in post:
         predicate_uuid = post['predicate_uuid'].strip()
         # check first to see if the predicate is in the list of required fields
         if predicate_uuid not in InputField.PREDICATE_ITEMS:
             # check to make sure the predicate actually exists
             try:
                 pred_man = Manifest.objects.get(uuid=predicate_uuid)
             except Manifest.DoesNotExist:
                 # we have a problem with the predicate
                 pred_man = False
                 ok = False
                 self.errors[
                     'predicate_uuid'] = 'predicate_uuid: ' + predicate_uuid + ' not found.'
                 note = self.errors['predicate_uuid']
             if pred_man is not False:
                 if pred_man.item_type != 'predicates':
                     pred_man = False
                     ok = False
                     self.errors[
                         'predicate_uuid'] = 'predicate_uuid: ' + predicate_uuid + ' is the wrong item_type.'
                     note = self.errors['predicate_uuid']
     validaton_json = self.make_validation_json_from_post(post)
     if ok:
         if field_uuid is False:
             label = self.check_make_valid_label(label)
             action = 'create-input-field'
             note = ' field created.'
             uuid = GenUUID.uuid4()
             field_uuid = str(uuid)
             inp_field = InputField()
             inp_field.uuid = field_uuid
             inp_field.project_uuid = self.project_uuid
             inp_field.profile_uuid = self.profile_uuid
             inp_field.fgroup_uuid = self.fgroup_uuid
             inp_field.predicate_uuid = predicate_uuid
             note = ' created.'
         else:
             action = 'update-input-field'
             inp_field = self.get_field(field_uuid)
             note = ' updated.'
         if inp_field is False:
             label = field_uuid + ' not found'
             note = 'Input Field ' + field_uuid + ' does not exist'
         else:
             if label != inp_field.label:
                 # the user is updating the item label
                 # so validate it
                 label = self.check_make_valid_label(label)
             inp_field.sort = sort_val
             inp_field.label = label
             inp_field.note = data_note
             inp_field.predicate_uuid = predicate_uuid
             inp_field.validation = validaton_json
             inp_field.save()
             inp_check = InputField.objects.get(uuid=field_uuid)
             note = 'Input Field "' + label + '" (' + field_uuid + ')' + note
     self.response = {
         'action': action,
         'ok': ok,
         'change': {
             'uuid': field_uuid,
             'label': label,
             'note': note
         }
     }
     return self.response
예제 #3
0
 def create_update_from_post(self, post, field_uuid=False):
     """ Creates or updates an input field from posted data
     """
     ok = True
     sort_val = 0
     note = ''
     action = ''
     if 'sort' in post:
         try:
             sort_val = int(float(post['sort']))
         except:
             sort_val = self.make_default_sort_val(self.profile_uuid)
     if 'label' in post:
         label = post['label'].strip()
     else:
         label = ''
     if 'note' in post:
         data_note = post['note'].strip()
     else:
         data_note = ''
     predicate_uuid = False
     if 'predicate_uuid' in post:
         predicate_uuid = post['predicate_uuid'].strip()
         # check first to see if the predicate is in the list of required fields
         if predicate_uuid not in InputField.PREDICATE_ITEMS:
             # check to make sure the predicate actually exists
             try:
                 pred_man = Manifest.objects.get(uuid=predicate_uuid)
             except Manifest.DoesNotExist:
                 # we have a problem with the predicate
                 pred_man = False
                 ok = False
                 self.errors['predicate_uuid'] = 'predicate_uuid: ' + predicate_uuid + ' not found.'
                 note = self.errors['predicate_uuid']
             if pred_man is not False:
                 if pred_man.item_type != 'predicates':
                     pred_man = False
                     ok = False
                     self.errors['predicate_uuid'] = 'predicate_uuid: ' + predicate_uuid + ' is the wrong item_type.'
                     note = self.errors['predicate_uuid']
     validaton_json = self.make_validation_json_from_post(post)
     if ok:
         if field_uuid is False:
             label = self.check_make_valid_label(label)
             action = 'create-input-field'
             note = ' field created.'
             uuid = GenUUID.uuid4()
             field_uuid = str(uuid)
             inp_field = InputField()
             inp_field.uuid = field_uuid
             inp_field.project_uuid = self.project_uuid
             inp_field.profile_uuid = self.profile_uuid
             inp_field.fgroup_uuid = self.fgroup_uuid
             inp_field.predicate_uuid = predicate_uuid
             note = ' created.'
         else:
             action = 'update-input-field'
             inp_field = self.get_field(field_uuid)
             note = ' updated.'
         if inp_field is False:
             label = field_uuid + ' not found'
             note = 'Input Field ' + field_uuid + ' does not exist'
         else:
             if label != inp_field.label:
                 # the user is updating the item label
                 # so validate it
                 label = self.check_make_valid_label(label)
             inp_field.sort = sort_val
             inp_field.label = label
             inp_field.note = data_note
             inp_field.predicate_uuid = predicate_uuid
             inp_field.validation = validaton_json
             inp_field.save()
             inp_check = InputField.objects.get(uuid=field_uuid)
             note = 'Input Field "' + label + '" (' + field_uuid + ')' + note
     self.response = {'action': action,
                      'ok': ok,
                      'change': {'uuid': field_uuid,
                                 'label': label,
                                 'note': note}}
     return self.response