def create_biogas_contact(self, request, **kwargs): self.is_authenticated(request) data = json.loads(request.read()) fields = [ "contact_type", "firstname", "surname", "mobile", "country", "village", "region", "district", "ward", "latitude", "longitude", "what3words", "UIC" ] #pdb.set_trace() _schema_ = schema['create_biogas_contact'] vv = Validator(_schema_) if not vv.validate(data): errors_to_report = vv.errors raise CustomBadRequest(code="field_error", message=errors_to_report) data = only_keep_fields(data, fields) data = if_empty_fill_none(data, fields) #pdb.set_trace() bundle = self.build_bundle(data={}, request=request) try: uob = bundle.request.user part_of_groups = uob.groups.all() perm = Permissions(part_of_groups) list_of_company_ids_admin = perm.check_auth_admin() list_of_company_ids_tech = perm.check_auth_tech() contact = BiogasPlantContact( ) # if plant_id is a field we need to add it to the plant that has been specified contact.first_name = data['firstname'] contact.surname = data['surname'] contact.mobile = data['mobile'] contact.contact_type = data['contact_type'] contact.region = data['region'] contact.district = data['district'] contact.ward = data['ward'] contact.village = data['village'] bb = contact.save() try: if "latitude" in data.keys() and "longitude" in data.keys(): contact.update( lat_long=Point(data['longitude'], data['latitude'])) except: pass uuid = contact.uid #create(first_name=data['firstname'],surname=surname,mobile,contact_type=contact_type, mobile=mobile, email=email) bundle.data = {"message": "contact created", "uid": uuid.hex} except: bundle.data = {"message": "error"} return self.create_response(request, bundle)
def obj_update(self, bundle, **kwargs): #pdb.set_trace() try: pk = int(kwargs['pk']) except: pk = kwargs['pk'] uob = bundle.request.user part_of_groups = uob.groups.all() perm = Permissions(part_of_groups) list_of_company_ids_admin = perm.check_auth_admin() list_of_company_ids_tech = perm.check_auth_tech() if uob.is_superuser: try: bundle.obj = PendingJobs.objects.get(pk=pk) # a superuser can edit any technican's record except: raise CustomBadRequest( code="403", message="Object not found") else: flag = 0 if list_of_company_ids_admin[0] is True: # pk=pk,, try: bundle.obj = PendingJobs.objects.get(Q(pk=pk),Q(associated_company__company_id__in = list_of_company_ids_admin[1]) | Q(constructing_technicians__company__company_id__in = list_of_company_ids_admin[1]) ) # a superuser can edit any technican's record fields_to_allow_update_on = ['UIC','contact','constructing_technicians','funding_souce','funding_source_notes','country','region','district','ward','village','postcode','other_address_details','type_biogas','supplier','volume_biogas','what3words','location','QP_status','sensor_status','current_status','verfied'] bundle = keep_fields(bundle, fields_to_allow_update_on) flag = 2 except: flag = 1 elif ( (flag == 1 or flag==0) and list_of_company_ids_tech[0] is True ): try: bundle.obj = PendingJobs.objects.get(Q(pk=pk),Q(associated_company__company_id__in = list_of_company_ids_admin[1]) | Q(constructing_technicians__company__company_id__in = list_of_company_ids_admin[1])) #bundle.obj = UserDetail.objects.get(pk=pk,company__company_id__in = list_of_company_ids_tech[1]) # a superuser can edit any technican's record fields_to_allow_update_on = ['UIC','contact','constructing_technicians','funding_souce','funding_source_notes','country','region','district','ward','village','postcode','other_address_details','type_biogas','supplier','volume_biogas','what3words','location','QP_status','sensor_status','current_status','verfied'] bundle = keep_fields(bundle, fields_to_allow_update_on) except: raise CustomBadRequest( code="403", message="You do not have permission to edit that contact") else: bundle.obj = BiogasPlant.objects.none() bundle.data ={} bundle = self.full_hydrate(bundle) #pdb.set_trace(0) return super(PendingJobsResource, self).obj_update(bundle, user=uob)
def accept_job(self, request, **kwargs): self.is_authenticated(request) #pdb.set_trace() try: pk = int(kwargs['pk']) #job_id = except: pk = kwargs['pk'] try: uid = uuid.UUID(hex=pk) # introduce some error handling here in case the form of pk is wrong #pdb.set_trace() bundle = self.build_bundle(data={'job_id': kwargs['pk']}, request=request) # we specify the type of bundle in order to help us filter the action we take before we return uob = bundle.request.user part_of_groups = uob.groups.all() perm = Permissions(part_of_groups) list_of_company_ids_admin = perm.check_auth_admin() list_of_company_ids_tech = perm.check_auth_tech() #obj = self.cached_obj_get(bundle=bundle, **self.remove_api_resource_names(kwargs)) pending_job = PendingJobs.objects.get(job_id=uid) # job_id is the primary key #pdb.set_trace() history = JobHistory(job_id=uid,plant=pending_job.biogas_plant) if pending_job is not None: history = JobHistory() history.job_id=uid history.plant=pending_job.biogas_plant history.save() history.fixers.add(pending_job.technician) history.date_flagged=pending_job.datetime_created history.job_status=2 history.fault_description=pending_job.job_details history.rejected_jobs=pending_job.technicians_rejected his=history.save() #JobHistory.objects.create(job_id=uid, plant=pending_job.biogas_plant, fixers=pending_job.technician,,job_status=2,fault_description=pending_job.job_details,rejected_jobs=pending_job.technicians_rejected) # job_status=2 means the job is now being resolved deleted = pending_job.delete() # send_message_to_customer() - to inform them that their job has been accepted and a tech will be in touch soon except: pass return self.create_response(request, bundle)
def edit_biogas_plant(self, request, **kwargs): self.is_authenticated(request) #pdb.set_trace() bundle = self.build_bundle(data={}, request=request) data = json.loads(request.read()) data = only_keep_fields(data, [ 'biogas_plant_name', 'contact', 'funding_souce', 'funding_source_notes', 'country', 'region', 'district', 'ward', 'village', 'other_address_details', 'type_biogas', 'supplier', 'volume_biogas', 'location_estimated', 'QP_status', 'sensor_status', 'current_status', 'verfied', 'what3words' ]) _schema_ = schema['edit_biogas_plant'] vv = Validator(_schema_) if not vv.validate(data): errors_to_report = vv.errors raise CustomBadRequest(code="field_error", message=errors_to_report) try: pk = int(kwargs['pk']) except: pk = kwargs['pk'] try: #uid = uuid.UUID(hex=pk) # the id of the job that wants reasigning needs to be included in the URL uob = bundle.request.user part_of_groups = uob.groups.all() perm = Permissions(part_of_groups) list_of_company_ids_admin = perm.check_auth_admin() list_of_company_ids_tech = perm.check_auth_tech() if uob.is_superuser: plant_to_edit = BiogasPlant.objects.get(id=pk) for itm in data: # for simple text based changes this is very easy - no additional clauses needed setattr(plant_to_edit, itm, data[itm]) plant_to_edit.save() bundle.data = {"message": "Biogas Plant Updated"} except: pass return self.create_response(request, bundle)
def get_all_pending_jobs(self, request, **kwargs): self.is_authenticated(request) bundle = self.build_bundle(data={}, request=request) try: #bundle = self.build_bundle(data={}, request=request) # we specify the type of bundle in order to help us filter the action we take before we return uob = bundle.request.user part_of_groups = uob.groups.all() perm = Permissions(part_of_groups) list_of_company_ids_admin = perm.check_auth_admin() list_of_company_ids_tech = perm.check_auth_tech() pending_jobs = PendingJobs.objects.filter(technician__user=uob) #pdb.set_trace() serialized_jobs = json.loads( serializers.serialize('json', pending_jobs) ) all_jobs = [] for nn,ii in enumerate(serialized_jobs): job = {} job['uri'] = '/api/v1/biogasplants/'+str(ii['fields']['biogas_plant']) +'/' job['region'] = pending_jobs[nn].biogas_plant.region job['district'] = pending_jobs[nn].biogas_plant.district job['ward'] = pending_jobs[nn].biogas_plant.ward job['volume'] = pending_jobs[nn].biogas_plant.volume_biogas job['location'] = to_serializable(pending_jobs[nn].biogas_plant.location) if (job['location'][0] != 'None' and job['location'][0] != None): # we can remove this, but is useful to have explictly job['longitude'] = pending_jobs[nn].biogas_plant.location.get_x() job['latitude'] = pending_jobs[nn].biogas_plant.location.get_y() job['QP_status'] = pending_jobs[nn].biogas_plant.QP_status job['sensor_status'] = pending_jobs[nn].biogas_plant.sensor_status job["current_status"] = pending_jobs[nn].biogas_plant.current_status install_date = pending_jobs[nn].biogas_plant.install_date if install_date is not None: job['install_date']=install_date.strftime("%Y-%m-%d %H:%M:%S") job['type_biogas'] = pending_jobs[nn].biogas_plant.type_biogas job['uid'] = uuid.UUID(ii['pk']).hex job['job_details'] = ii['fields']['job_details'] job['date_created'] = ii['fields']['datetime_created'] all_jobs.append(job) bundle.data['jobs'] = all_jobs except Exception as error: print(error) pdb.set_trace() pass return self.create_response(request, bundle)
def get_unassigned_pending_jobs(self, request, **kwargs): self.is_authenticated(request) bundle = self.build_bundle(data={}, request=request) uob = bundle.request.user part_of_groups = uob.groups.all() perm = Permissions(part_of_groups) list_of_company_ids_admin = perm.check_auth_admin() list_of_company_ids_tech = perm.check_auth_tech() if uob.is_superuser: try: pending_jobs = PendingJobs.objects.filter(technician__user=None) serialized_jobs = json.loads( serializers.serialize('json', pending_jobs) ) all_jobs = [] for nn,ii in enumerate(serialized_jobs): job = {} job['uri'] = '/api/v1/biogasplants/'+str(ii['fields']['biogas_plant']) +'/' job['region'] = pending_jobs[nn].biogas_plant.region job['district'] = pending_jobs[nn].biogas_plant.district job['ward'] = pending_jobs[nn].biogas_plant.ward job['volume'] = pending_jobs[nn].biogas_plant.volume_biogas job['longitude'] = pending_jobs[nn].biogas_plant.location.get_x() job['latitude'] = pending_jobs[nn].biogas_plant.location.get_y() job['QP_status'] = pending_jobs[nn].biogas_plant.QP_status job['sensor_status'] = pending_jobs[nn].biogas_plant.sensor_status job["current_status"] = pending_jobs[nn].biogas_plant.current_status install_date = pending_jobs[nn].biogas_plant.install_date if install_date is not None: job['install_date']=install_date.strftime("%Y-%m-%d %H:%M:%S") job['type_biogas'] = pending_jobs[nn].biogas_plant.type_biogas job['uid'] = uuid.UUID(ii['pk']).hex job['job_details'] = ii['fields']['job_details'] job['date_created'] = ii['fields']['datetime_created'] job["technician"] = "unassigned" all_jobs.append(job) bundle.data['jobs'] = all_jobs except: pass return self.create_response(request, bundle)
def reject_job(self, request, **kwargs): self.is_authenticated(request) try: pk = int(kwargs['pk']) #job_id = except: pk = kwargs['pk'] try: uid = uuid.UUID(hex=pk) # introduce some error handling here in case the form of pk is wrong #pdb.set_trace() bundle = self.build_bundle(data={'job_id': kwargs['pk']}, request=request) # we specify the type of bundle in order to help us filter the action we take before we return uob = bundle.request.user part_of_groups = uob.groups.all() perm = Permissions(part_of_groups) list_of_company_ids_admin = perm.check_auth_admin() list_of_company_ids_tech = perm.check_auth_tech() #obj = self.cached_obj_get(bundle=bundle, **self.remove_api_resource_names(kwargs)) pending_job = PendingJobs.objects.get(job_id=uid) # job_id is the primary key if pending_job is not None: pending_job.technician = None pending_job.save() bundle.data = {"message":"job_rejected","job_id":pk} except: pass return self.create_response(request, bundle)
def obj_update(self, bundle, **kwargs): #pdb.set_trace() try: pk = int(kwargs['pk']) except: pk = kwargs['pk'] uob = bundle.request.user part_of_groups = uob.groups.all() perm = Permissions(part_of_groups) list_of_company_ids_admin = perm.check_auth_admin() list_of_company_ids_tech = perm.check_auth_tech() if uob.is_superuser: try: bundle.obj = BiogasPlantContact.objects.get( pk=pk) # a superuser can edit any technican's record except: raise CustomBadRequest(code="403", message="Object not found") else: flag = 0 if list_of_company_ids_admin[0] is True: try: bundle.obj = BiogasPlantContact.objects.get( pk=pk, associated_company__company_id__in= list_of_company_ids_admin[1] ) # a superuser can edit any technican's record fields_to_allow_update_on = [ 'contact_type', 'first_name', 'surname', 'mobile', 'email', 'associated_company' ] bundle = keep_fields(bundle, fields_to_allow_update_on) flag = 2 except: flag = 1 elif ((flag == 1 or flag == 0) and list_of_company_ids_tech[0] is True): try: bundle.obj = BiogasPlantContact.objects.get( pk=pk, associated_company__company_id__in= list_of_company_ids_admin[1]) #bundle.obj = UserDetail.objects.get(pk=pk,company__company_id__in = list_of_company_ids_tech[1]) # a superuser can edit any technican's record fields_to_allow_update_on = [ 'contact_type', 'first_name', 'surname', 'mobile', 'email', 'associated_company' ] bundle = keep_fields(bundle, fields_to_allow_update_on) except: raise CustomBadRequest( code="403", message= "You do not have permission to edit that contact") else: bundle.obj = BiogasPlantContact.objects.none() bundle.data = {} bundle = self.full_hydrate(bundle) #pdb.set_trace(0) return super(BiogasPlantContact, self).obj_update(bundle, user=uob)
def find_biogas_owner(self, request, **kwargs): #pdb.set_trace() self.is_authenticated(request) data = json.loads(request.read()) data = only_keep_fields(data, ["mobile"]) try: bundle = self.build_bundle(data={}, request=request) uob = bundle.request.user if uob.is_superuser: part_of_groups = uob.groups.all() perm = Permissions(part_of_groups) list_of_company_ids_admin = perm.check_auth_admin() list_of_company_ids_tech = perm.check_auth_tech() #pdb.set_trace() contacts = BiogasPlantContact.objects.filter( mobile=data['mobile']) data_to_return = [] for cc in contacts: biogas_owner = {} biogas_owner['first_name'] = cc.first_name biogas_owner['mobile'] = cc.mobile biogas_owner['first_name'] = cc.first_name biogas_owner['contact_type'] = cc.contact_type.name biogas_owner['email'] = cc.email #biogas_owner['contact'] = to_serializable(cc.uid)[0] biogas_owner['contact'] = cc.uid.hex biogas_owner['location'] = to_serializable(cc.lat_long) biogas_owner['village'] = cc.village biogas_owner['ward'] = cc.ward biogas_owner['region'] = cc.region biogas_owner['district'] = cc.district biogas_owner['country'] = cc.country biogas_plant_queryset = cc.biogas_plant_detail.get_queryset( ) biogas_plants_owned_list = [] #pdb.set_trace() for bb in biogas_plant_queryset: biogas_plant_owned = {} biogas_plant_owned['UIC'] = to_serializable(bb.UIC)[0] biogas_plant_owned[ 'biogas_plant_name'] = to_serializable( bb.biogas_plant_name)[0] biogas_plant_owned['country'] = to_serializable( bb.country)[0] biogas_plant_owned['region'] = to_serializable( bb.region)[0] biogas_plant_owned['district'] = to_serializable( bb.district)[0] biogas_plant_owned['ward'] = to_serializable( bb.ward)[0] biogas_plant_owned['village'] = to_serializable( bb.village)[0] biogas_plant_owned[ 'other_address_details'] = to_serializable( bb.other_address_details)[0] biogas_plant_owned['type_biogas'] = to_serializable( bb.type_biogas)[0] biogas_plant_owned['supplier'] = to_serializable( bb.supplier)[0] biogas_plant_owned['volume_biogas'] = to_serializable( bb.volume_biogas)[0] biogas_plant_owned['location'] = to_serializable( cc.lat_long) # biogas_plant_owned['latitude'] = to_serializable(bb.location)[1] # biogas_plant_owned['longitude'] = to_serializable(bb.location)[0] biogas_plant_owned['sensor_status'] = to_serializable( bb.sensor_status)[0] biogas_plant_owned['current_status'] = to_serializable( bb.current_status)[0] biogas_plant_owned['verfied'] = to_serializable( bb.verfied)[0] biogas_plant_owned['install_date'] = to_serializable( bb.install_date)[0] biogas_plants_owned_list.append(biogas_plant_owned) #pdb.set_trace() biogas_owner['biogas_plants'] = biogas_plants_owned_list data_to_return.append(biogas_owner) bundle.data['data'] = data_to_return else: bundle.data = {"message": "you are not authorised"} except Exception as e: print(e) return self.create_response(request, bundle)
def get_biogas_plants(self, request, **kwargs): """Gte the biogas plants of an owner form their mobile number""" self.is_authenticated(request) data = json.loads(request.read()) mobile = data['mobile'] bundle = self.build_bundle(data={}, request=request) try: # we specify the type of bundle in order to help us filter the action we take before we return uob = bundle.request.user #if uob.is_superuser: part_of_groups = uob.groups.all() perm = Permissions(part_of_groups) list_of_company_ids_admin = perm.check_auth_admin() list_of_company_ids_tech = perm.check_auth_tech #bundle.data['technicians'] = data_list biogas_plants = BiogasPlant.objects.filter(contact__mobile=mobile) #bundle.data['biogas_plants'] = [i for i in biogas_plants] data_list = [] for bi in biogas_plants: data = { "owner": [{ "first_name": ii.first_name, "surname": ii.surname, "mobile": ii.mobile, "contact_type": ii.contact_type.name } for ii in bi.contact.all()], "biogas_plant_name": bi.biogas_plant_name, "associated_company": bi.associated_company, "country": bi.country, "region": bi.region, "district": bi.district, "ward": bi.ward, "village": bi.village, "type_biogas": bi.type_biogas, "supplier": bi.supplier, "volume_biogas": bi.volume_biogas, "QP_status": bi.QP_status, "sensor_status": bi.sensor_status, "current_status": bi.current_status, "verfied": bi.verfied, "uri": "/api/v1/biogasplant/" + str(bi.id) + "/", "location_estimated": bi.location_estimated, "location": to_serializable(bi.location), } data_list.append(data) bundle.data['biogas_plants'] = data_list except: pass return self.create_response(request, bundle)
def create_biogas_plant(self, request, **kwargs): self.is_authenticated(request) data = json.loads(request.read()) fields = [ "UIC", "biogas_plant_name", "associated_company", "contact", "funding_source", "latitude", "longitude", "country", "village", "region", "district", "ward", "what3words", "type_biogas", "volume_biogas", "install_date", "other_address_details", "current_status", "contruction_tech", "location_estimated" ] _schema_ = schema['create_biogas_plant'] vv = Validator(_schema_) if not vv.validate(data): errors_to_report = vv.errors raise CustomBadRequest(code="field_error", message=errors_to_report) data = only_keep_fields(data, fields) data = if_empty_fill_none(data, fields) fields = data.keys() bundle = self.build_bundle(data={}, request=request) try: uob = bundle.request.user part_of_groups = uob.groups.all() perm = Permissions(part_of_groups) list_of_company_ids_admin = perm.check_auth_admin() list_of_company_ids_tech = perm.check_auth_tech() biogasplant = BiogasPlant( ) # if plant_id is a field we need to add it to the plant that has been specified field_relations = { 'UIC': biogasplant.UIC, 'biogas_plant_name': biogasplant.biogas_plant_name, 'funding_souce': biogasplant.funding_souce, 'country': biogasplant.country, 'region': biogasplant.region, 'district': biogasplant.district, 'ward': biogasplant.ward, 'village': biogasplant.village, 'other_address_details': biogasplant.other_address_details, 'type_biogas': biogasplant.type_biogas, 'volume_biogas': biogasplant.volume_biogas, 'location': biogasplant.location, 'location_estimated': biogasplant.location_estimated, 'current_status': biogasplant.current_status } #pdb.set_trace() #biogasplant.UIC = data['UIC'] #biogasplant.biogas_plant_name = data['biogas_plant_name'] #biogasplant.funding_souce = data['funding_source'] #biogasplant.country = data['country'] #biogasplant.region = data['region'] #biogasplant.district = data['district'] #biogasplant.ward = data['ward'] #biogasplant.village = data['village'] #biogasplant.other_address_details = data['other_address_details'] #biogasplant.type_biogas = data['type_biogas'] #biogasplant.volume_biogas = data['volume_biogas'] #biogasplant.location = Point(data['longitude'],data['latitude']) #biogasplant.location_estimated = data["location_estimated"] #biogasplant.current_status = data['current_status'] for fld in fields: try: if fld == 'location': field_relations[fld] = Point(data['longitude'], data['latitude']) else: field_relations[fld] = data[fld] except: pass bb = biogasplant.save() if data["contruction_tech"] == "me": biogasplant.constructing_technicians.add( UserDetail.objects.get(user=uob)) if (data['contact'] is not None): # now link the biogas plant to a contact #pdb.set_trace() uid = uuid.UUID(hex=data['contact']) biogasplant.contact.add( BiogasPlantContact.objects.get(uid=uid)) if (data['associated_company'] is not None): #biogasplant.associated_company = data['associated_company']] pass plant_id = biogasplant.plant_id.hex #create(first_name=data['firstname'],surname=surname,mobile,contact_type=contact_type, mobile=mobile, email=email) bundle.data = {"message": "biogas plant created", "uid": plant_id} except: bundle.data = {"message": "error"} return self.create_response(request, bundle)