def replace_data(form_id, query_key, query_value, data): mongo_ids = settings.MONGO_DB.instances.aggregate([{ '$match': { 'fs_project_uuid': str(form_id), query_key: query_value } }, { '$project': { '_id': 1 } }], cursor={}) mongo_ids = list(mongo_ids) ids = [] for mongo_id in mongo_ids: ids.append(mongo_id['_id']) instances = Instance.objects.filter(id__in=ids) instance_ids = [] for instance in instances: instance_ids.append(instance.id) for key, value in data.items(): print instance.id print key, "------", instance.json.get(key, None) instance.json[key] = value print key, ">>>>>>", instance.json[key] print "::::::" events = instance.json[query_key].split(" ") if query_value in events: events.remove(query_value) new_event = "" for event in events: new_event = new_event + event + " " instance.json[query_key] = new_event instance.save(hack=True) d = instance.parsed_instance.to_dict_for_mongo() for key, value in instance.json.items(): d[key] = value d.update({ 'fs_project_uuid': str(instance.fieldsight_instance.project_fxf_id), 'fs_project': instance.fieldsight_instance.project_id, 'fs_status': instance.fieldsight_instance.form_status, 'fs_site': str(instance.fieldsight_instance.site_id), 'fs_uuid': str(instance.fieldsight_instance.site_fxf_id) }) update_mongo_instance(d)
def update_mongo(i): """ Update the instance data to mongo database when a existing submission is submitted from enketo Args: i: Instance object Returns: None """ d = i.parsed_instance.to_dict_for_mongo() try: x = i.fieldsight_instance d.update({ 'fs_project_uuid': str(x.project_fxf_id), 'fs_project': x.project_id, 'fs_status': 0, 'fs_site': x.site_id, }) if x.project_fxf: d['fs_project_uuid'] = str(x.project_fxf_id) if x.site_fxf: d['fs_uuid'] = str(x.site_fxf_id) try: synced = update_mongo_instance(d, i.id) except Exception as e: print(str(e)) except Exception as e: print(str(e))
def move_submission(sheet_columns, project_id): sn, site_name, submission_id, site_id = tuple(sheet_columns) print(submission_id, site_id) project = Project.objects.get(pk=project_id) to_site = project.sites.get(pk=site_id) if FInstance.objects.filter(instance=submission_id).exists(): instance = FInstance.objects.get(instance=submission_id) instance.site = to_site instance.save() d = instance.instance.parsed_instance.to_dict_for_mongo() d.update({ 'fs_project_uuid': str(instance.project_fxf_id), 'fs_project': instance.project_id, 'fs_status': 0, 'fs_site': instance.site_id, 'fs_uuid': instance.site_fxf_id }) try: synced = update_mongo_instance(d, instance.id) print(synced, "updated in mongo success") except Exception as e: print(str(e)) else: print("submision ", submission_id, "doesnot exists")
def handle(self, *args, **options): organization_id = 13 # project_id = 30 sites = Site.objects.filter(project__organization__id=organization_id).values_list('id', flat=True) for site_id in sites: # self.stdout.write('Operating in site '+str(site_id)) with transaction.atomic(): finstances = FInstance.objects.filter(site_id=site_id, site_fxf_id__isnull=False) for fi in finstances: site_fsxf = fi.site_fxf if site_fsxf.site.id != site_id: correct_form = FieldSightXF.objects.get(site__id=site_id, is_staged=True, fsform=fi.project_fxf, is_deleted=False) fi.site_fxf = correct_form fi.save() parsed_instance = fi.instance.parsed_instance d = parsed_instance.to_dict_for_mongo() d.update({'fs_uuid': correct_form.id}) update_mongo_instance(d) self.stdout.write('Successfully corrected form')
def handle(self, *args, **options): instances = Instance.objects.filter(is_synced_with_mongo=False).exclude(pk__in=OLD_INSTANCES) for i in instances: d = i.parsed_instance.to_dict_for_mongo() try: x = i.fieldsight_instance d.update({'fs_project_uuid': str(x.project_fxf_id), 'fs_project': x.project_id, 'fs_status': 0, 'fs_site':x.site_id, 'fs_uuid':x.site_fxf_id}) try: synced = update_mongo_instance(d, i.id) print(synced, "updated in mongo success") except Exception as e: print(str(e)) except Exception as e: print(str(e))
def create_finstance_from_mongo(sheet_columns, project_id): submission_id, site_identifier = tuple(sheet_columns) project = Project.objects.get(pk=project_id) site = project.sites.get(identifier=site_identifier) if FInstance.objects.filter(instance=submission_id).exists(): print("Submission Id Exist ", submission_id) else: print("creating Finstance for ", submission_id, ".......") # query = {"_id": {"$in": submission_id_list}} query = {"_id": submission_id} xform_instances = settings.MONGO_DB.instances cursor = list( xform_instances.find( query, { "_id": 1, "fs_project_uuid": 1, "fs_project": 1, "fs_site": 1, 'fs_uuid': 1 })) instance = Instance.objects.get(pk=submission_id) fi = FInstance(instance=instance, site=site, project=site.project, project_fxf_id=cursor[0]["fs_project_uuid"], form_status=0, submitted_by=instance.user) # fi.set_version() fi.save() instance = FInstance.objects.get(instance=instance) d = instance.instance.parsed_instance.to_dict_for_mongo() d.update({ 'fs_project_uuid': str(instance.project_fxf_id), 'fs_project': instance.project_id, 'fs_status': 0, 'fs_site': instance.site_id, 'fs_uuid': instance.site_fxf_id }) try: synced = update_mongo_instance(d, instance.id) print(synced, "updated in mongo success") except Exception as e: print(str(e))
def handle(self, *args, **options): only_mongo = options.get("only_mongo", "0") if only_mongo == "0": submission_id = options['submission_id'] transfer_to = options['transfer_to'] instance = Instance.objects.get(id=submission_id) transfer_to = FieldSightXF.objects.get(id=transfer_to) valid_xf = transfer_to.xf instance.xform = valid_xf instance.save() fieldsight_instance = instance.fieldsight_instance fieldsight_instance.project_fxf = transfer_to fieldsight_instance.save() # d = instance.parsed_instance.to_dict_for_mongo() else: try: submission_id = options['submission_id'] transfer_to = options['transfer_to'] instance = Instance.objects.get(id=submission_id) transfer_to = FieldSightXF.objects.get(id=transfer_to) # valid_xf = transfer_to.xf # instance.xform = valid_xf # instance.save() d = instance.parsed_instance.to_dict_for_mongo() x = instance.fieldsight_instance d.update({ 'fs_project_uuid': str(x.project_fxf_id), 'fs_project': x.project_id, 'fs_status': 0, 'fs_site': x.site_id, 'fs_uuid': x.site_fxf_id }) try: synced = update_mongo_instance(d, instance.id) print(synced, "updated in mongo success") except Exception as e: print(str(e)) except Exception as e: print(str(e)) self.stdout.write( 'Successfully transfer project submission form in "%s"' % transfer_to)
def update_mongo(i): d = i.parsed_instance.to_dict_for_mongo() try: x = i.fieldsight_instance d.update({ 'fs_project_uuid': str(x.project_fxf_id), 'fs_project': x.project_id, 'fs_status': 0, 'fs_site': str(x.site_id), 'fs_uuid': str(x.site_fxf_id) }) try: synced = update_mongo_instance(d, i.id) print(synced, "updated in mongo success") except Exception as e: print(str(e)) except Exception as e: print(str(e))
def handle(self, *args, **options): finstances = FInstance.deleted_objects.all() for x in finstances: d = x.instance.parsed_instance.to_dict_for_mongo() try: d.update({ 'fs_project_uuid': str(x.project_fxf_id), 'fs_project': x.project_id, 'fs_status': 0, 'fs_site': str(x.site_id), 'fs_uuid': str(x.site_fxf_id), '_deleted_at': True }) try: synced = update_mongo_instance(d) print(synced, "updated in mongo success") except Exception as e: print(str(e)) except Exception as e: print(str(e))
for key, value in instance.json.items(): d[key] = value d.update({ 'fs_project_uuid': str(instance.fieldsight_instance.project_fxf_id), 'fs_project': instance.fieldsight_instance.project_id, 'fs_status': instance.fieldsight_instance.form_status, 'fs_site': str(instance.fieldsight_instance.site_id), 'fs_uuid': str(instance.fieldsight_instance.site_fxf_id) }) update_mongo_instance(d) ############################################### mongo_ids = settings.MONGO_DB.instances.aggregate([{ '$match': { 'fs_project_uuid': "992794", 'visit_purpose': { '$regex': 'drawing_regist' } } }, { '$project': { '_id': 1, 'drawing_regist': 1, 'visit_purpose': 1