Ejemplo n.º 1
0
 def save(self,
          force_insert=False,
          force_update=False,
          using=None,
          update_fields=None):
     """ "Set id and call parents save()."""
     self.id = hash_with_namespace_uuid(  # pylint: disable=C0103
         self.study_id, self.name, cache=False)
     super().save(
         force_insert=force_insert,
         force_update=force_update,
         using=using,
         update_fields=update_fields,
     )
Ejemplo n.º 2
0
def _import_question_items(
    question: Question, items: List[Dict[str, str]]
) -> List[QuestionItem]:

    question_items = []
    for position, item in enumerate(items):
        question_item = QuestionItem(question=question, position=position)
        for field in ITEM_FIELDS:
            setattr(question_item, field, item[_field_mapper(field)])

        question_item.id = hash_with_namespace_uuid(
            question.id, str(question_item.position), cache=False
        )

        question_items.append(question_item)
    return question_items
Ejemplo n.º 3
0
 def save(
     self,
     force_insert: bool = False,
     force_update: bool = False,
     using: Optional[str] = None,
     update_fields: Optional[Iterable[str]] = None,
 ) -> None:
     if not self.id:
         self.id = hash_with_namespace_uuid(  # pylint: disable = invalid-name
             self.variable.id,
             f"{self.plot_type}{self.independent_variable_names}")
     return super().save(
         force_insert=force_insert,
         force_update=force_update,
         using=using,
         update_fields=update_fields,
     )
Ejemplo n.º 4
0
 def save(self,
          force_insert=False,
          force_update=False,
          using=None,
          update_fields=None):
     """ "Set id and call parents save()."""
     self.id = hash_with_namespace_uuid(  # pylint: disable=C0103
         self.dataset_id, self.name, cache=False)
     # Disable attribute-defined-outside-init warning.
     # Django magic defines _id fields.
     self.period_id = self.dataset.period_id  # pylint: disable=W0201
     super().save(
         force_insert=force_insert,
         force_update=force_update,
         using=using,
         update_fields=update_fields,
     )
Ejemplo n.º 5
0
def instrument_import(file: Path, study: Study) -> None:
    """Import instrument data from CSV"""
    instruments_with_questions = _get_instruments_with_questions(file.parent)

    periods = {period.name: period for period in Period.objects.filter(study=study)}
    analysis_units = {
        unit.name: unit for unit in AnalysisUnit.objects.filter(study=study)
    }

    with open(file, encoding="utf8") as instruments_csv:
        instruments = []
        for row in DictReader(instruments_csv):
            instrument = Instrument()
            instrument.study = study
            instrument.id = hash_with_namespace_uuid(  # pylint: disable=invalid-name
                study.id, row["name"], cache=False
            )
            instrument.period = periods.get(row["period"], None)
            instrument.analysis_unit = analysis_units.get(row["analysis_unit"], None)

            for field in required_fields:
                setattr(instrument, field, row[field])
            for field in optional_fields:
                setattr(instrument, field, row.get(field, ""))
            for field, field_mapper in optional_nested_fields.items():
                value = {}
                for model_field, csv_field in field_mapper.items():
                    value[model_field] = row.get(csv_field, "")
                setattr(instrument, field, value)
            instrument.has_questions = instrument.name in instruments_with_questions

            instruments.append(instrument)

    Instrument.objects.bulk_update(
        instruments,
        required_fields
        + ["analysis_unit", "period", "has_questions"]
        + optional_fields
        + list(optional_nested_fields.keys()),
    )
Ejemplo n.º 6
0
 def generate_id(self, cache=False):
     """Generate UUID used in the objects save method."""
     return hash_with_namespace_uuid(self.question_id,
                                     str(self.position),
                                     cache=cache)
Ejemplo n.º 7
0
 def generate_id(self, cache=False):
     """Generate UUID used in the objects save method."""
     return hash_with_namespace_uuid(self.instrument_id,
                                     self.name,
                                     cache=cache)