def deserialize_contact(data, persistent=False): """ Transforms data (as JSON string or a dict) to a contact object """ if not data: return None if not persistent: pass # TODO if necessary else: from repository.models import Contact from vocabulary.models import CountryCode if isinstance(data, basestring): data = DictKeyAttribute(simplejson.loads(data)) elif isinstance(data, dict): data = DictKeyAttribute(data) try: obj = Contact.objects.get(pk=data.pk) except Contact.DoesNotExist: obj = Contact() obj.id = data.pk obj.firstname = data.firstname obj.middlename = data.middlename obj.lastname = data.lastname obj.email = data.email obj.affiliation = deserialize_institution(data.affiliation, True) obj.address = data.address obj.city = data.city obj.country = deserialize_vocabulary(data.country, True, CountryCode) obj.zip = data.zip obj.telephone = data.telephone obj.creator = deserialize_user(data.creator, True) obj.save() return obj
def set_trial_children(self, ct, fields): _vocabularies = {'decs':'DeCS', 'icd10':'ICD-10'} for country in fields.get('recruitment_country', []): if country.get('label', None): country_obj, new = CountryCode.objects.get_or_create( label=country['label'], defaults={'description': country.get('description', '')}, ) else: country_obj = CountryCode.objects.get(description=country['description']) ct.recruitment_country.add(country_obj) for person in fields.get('persons', []): contact = Contact.objects.filter(email=person['email'], creator=self.creator) if contact: contact = contact[0] else: contact = Contact() contact.creator = self.creator contact.firstname = person.get('firstname') contact.middlename = person.get('middlename') contact.lastname = person.get('lastname') contact.address = person.get('address') contact.city = person.get('city') if person.get('country_code', None): if person['country_code'].get('label', None): contact.country = CountryCode.objects.get(label=person['country_code']['label']) else: contact.country = CountryCode.objects.get(description=person['country_code']['description']) contact.zip = person.get('zip') contact.telephone = person.get('telephone') contact.email = person.get('email') if person.get('affiliation', None): contact.affiliation = self.get_instituion_from_db(person['affiliation']) contact.save() for item in fields.get('public_contact', []): if item.get('pid', None): try: contact = Contact.objects.get(pk=item['pid']) except Contact.DoesNotExist: contact = Contact.objects.filter(email=person['email'], creator=self.creator)[0] else: contact = Contact.objects.filter(email=person['email'], creator=self.creator)[0] PublicContact.objects.get_or_create(trial=ct, contact=contact) for item in fields.get('scientific_contact', []): if item.get('pid', None): try: contact = Contact.objects.get(pk=item['pid']) except Contact.DoesNotExist: contact = Contact.objects.filter(email=person['email'], creator=self.creator)[0] else: contact = Contact.objects.filter(email=person['email'], creator=self.creator)[0] ScientificContact.objects.get_or_create(trial=ct, contact=contact) for item in fields.get('site_contact', []): if item.get('pid', None): try: contact = Contact.objects.get(pk=item['pid']) except Contact.DoesNotExist: contact = Contact.objects.filter(email=person['email'], creator=self.creator)[0] else: contact = Contact.objects.filter(email=person['email'], creator=self.creator)[0] SiteContact.objects.get_or_create(trial=ct, contact=contact) TrialNumber.objects.get_or_create( trial=ct, issuing_authority=fields.get('reg_name', ''), id_number=fields['trial_id'], ) for item in fields.get('secondary_ids', []): TrialNumber.objects.get_or_create( trial=ct, issuing_authority=item.get('issuing_authority', ''), id_number=item['sec_id'], ) for item in fields.get('secondary_sponsors', []): inst = self.get_instituion_from_db(item) TrialSecondarySponsor.objects.get_or_create(trial=ct, institution=inst) for item in fields.get('source_support', []): inst = self.get_instituion_from_db(item) TrialSupportSource.objects.get_or_create(trial=ct, institution=inst) #TODO! This try-except shoud be refactored, its is used in several places for item in fields.get('primary_outcomes', []): outcome, new = Outcome.objects.get_or_create(trial=ct, interest='primary', description=item['value']) for trans in item.get('translations', []): try: trans = outcome.translations.get(language=trans['lang'], description=trans['value']) except outcome.translations.model.DoesNotExist: trans = outcome.translations.model(language=trans['lang'], description=trans['value']) trans.content_object = outcome trans.save() for item in fields.get('secondary_outcomes', []): outcome, new = Outcome.objects.get_or_create(trial=ct, interest='secondary', description=item['value']) for trans in item.get('translations', []): try: trans = outcome.translations.get(language=trans['lang'], description=trans['value']) except outcome.translations.model.DoesNotExist: trans = outcome.translations.model(language=trans['lang'], description=trans['value']) trans.content_object = outcome trans.save() for item in fields.get('hc_codes', []): descriptor, new = Descriptor.objects.get_or_create( trial=ct, aspect='HealthCondition', level='general', vocabulary=_vocabularies[item.get('vocabulary', 'decs')], # FIXME code=item['code'], defaults={ 'version': item.get('version', ''), 'text': item.get('value', ''), } ) for trans in item.get('translations', []): trans_obj = descriptor.translations.get_translation_for_object(trans['lang'], descriptor, create_if_not_exist=True) trans_obj.text=trans['value'] trans_obj.save() for item in fields.get('hc_keywords', []): descriptor, new = Descriptor.objects.get_or_create( trial=ct, aspect='HealthCondition', level='specific', vocabulary=_vocabularies[item.get('vocabulary', 'decs')], # FIXME code=item['code'], defaults={ 'version': item.get('version', ''), 'text': item.get('value', ''), } ) for trans in item.get('translations', []): trans_obj = descriptor.translations.get_translation_for_object(trans['lang'], descriptor, create_if_not_exist=True) trans_obj.text=trans['value'] trans_obj.save() for item in fields.get('i_codes', []): i_code, new = InterventionCode.objects.get_or_create(label=item['value']) ct.i_code.add(i_code) for item in fields.get('i_keywords', []): descriptor, new = Descriptor.objects.get_or_create( trial=ct, aspect='Intervention', level='general', vocabulary=_vocabularies[item.get('vocabulary', 'decs')], # FIXME code=item['code'], defaults={ 'version': item.get('version', ''), 'text': item.get('value', ''), } ) for trans in item.get('translations', []): trans_obj = descriptor.translations.get_translation_for_object(trans['lang'], descriptor, create_if_not_exist=True) trans_obj.text=trans['value'] trans_obj.save()