def update(self, instance, validated_data): try: original_amenities_list = instance.amenities_json['data']['amenities_dict'] amenities_patch_list = validated_data['data']['amenities_dict'] for i in amenities_patch_list: amenity_id = str(amenities_patch_list[str(i)]['id']) if str(amenity_id) != str(i): raise ValidationError({'error': 'Inconistent Data'}) amenity_patch_data = amenities_patch_list[amenity_id] if amenity_id in original_amenities_list: if amenity_patch_data['status'] not in [OK, DAMAGED, MISSING]: raise ValidationError({'error': 'status must be one of ok, damaged or missing'}) original_amenities_list[amenity_id]['status'] = amenity_patch_data['status'] validate_amenity_id(amenity_id) else: raise ValidationError({'detail': 'Specified Amenity Does Not Exist'}) except ValidationError as E: raise E except Exception: sentry_debug_logger.error("error in updating scout move out amenity checkup", exc_info=True) raise ValidationError({'detail': 'Bad Request'}, code=400) return super(MoveOutAmenitiesCheckupUpdateSerializer, self).update(instance, validated_data)
def get_task_id(obj): try: from scouts.api.serializers import ScoutTaskDetailSerializer return obj.conversation.task.id except Exception as E: sentry_debug_logger.error(str(E), exc_info=True) return None
def customer_post_save_task(sender, instance, created, **kwargs): if created: try: if instance.user.username != HALANX_SUPPORT_USERNAME: # Creating Conversation and message between new customer and halanx support create_halanx_support_default_initial_message_for_registering_customer( customer=instance) except Exception as E: sentry_debug_logger.error(str(E)) # create new cart for customer from Carts.models import Cart Cart.objects.get_or_create(customer=instance) # create new tenant object from Homes.Tenants.models import Tenant Tenant.objects.get_or_create(customer=instance) return # send welcome mail from UserBase.tasks import send_welcome_mail send_welcome_mail.delay(instance.user.first_name, instance.user.email) # populate news feed from News.tasks import add_articles_to_feed add_articles_to_feed.delay(instance.id) # set customer profile pic from fb from UserBase.tasks import set_profile_pic_from_social_account if SocialAccount.objects.filter(user=instance.user).count(): set_profile_pic_from_social_account.delay(instance.id)
def get_other_participant(self, obj): try: other_participant = obj.other_participant( self.context['requesting_participant']) return ParticipantSerializer(other_participant).data except Exception as E: from utility.logging_utils import sentry_debug_logger sentry_debug_logger.error("error is " + str(E), exc_info=True) return None
def new_lead_from_zoho_lead(request): # lead_id = request.query_params['lead_id'] lead_id = request.data['lead_id'] instance = ZCRMRecord.get_instance('Leads', lead_id) try: result = instance.get() lead_data = result.response_json['data'][0] create_tenant_lead_data_from_zoho_lead_data(lead_data) return Response({STATUS: SUCCESS}) except Exception as E: sentry_debug_logger.error(E, exc_info=True) return Response({STATUS: ERROR, "message": 'Some Error Occured'})
def scout_assignment_request_set_rejected(instance_id): try: from utility.logging_utils import sentry_debug_logger from scouts.models import ScoutTaskAssignmentRequest from scouts.utils import REQUEST_AWAITED, REQUEST_REJECTED scout_task_assign_request = ScoutTaskAssignmentRequest.objects.filter(id=instance_id).first() # Auto Reject a task after 2 minutes if scout_task_assign_request: if scout_task_assign_request.status == REQUEST_AWAITED: scout_task_assign_request.status = REQUEST_REJECTED scout_task_assign_request.auto_rejected = True scout_task_assign_request.save() # sentry_debug_logger.debug("rejected after two minutes" + str(instance_id), exc_info=True) except Exception as E: sentry_debug_logger.error("execption occured is " + str(E), exc_info=True)
def get_param_dict_for_rent_agreement(booking): facilities = get_tabular_facilities(booking) param_dict = { 'tenant': booking.tenant, 'booking': booking, 'owner': booking.space.house.owner, 'facilities': facilities, 'agreement_date': booking.license_start_date.date().strftime('%d %b, %Y'), } if booking.agreement_verification_status == AGREEMENT_SIGN_VERIFIED_SUCCESS: try: param_dict['digital_signature'] = booking.signature.signature.url except Exception as E: sentry_debug_logger.error(E, exc_info=True) return param_dict
def create_property_on_boarding_scout_task_by_scout_himself(request, task_id=''): scout = get_object_or_404(Scout, user=request.user) data = {'location': request.data.get('location', ""), 'scheduled_at': request.data.get('scheduled_at', None), 'manually_chosen_scout_id': scout.id} request_data = { DATA: data, TASK_TYPE: 'Property Onboarding' } x = requests.post(SCOUT_TASK_URL, data=json.dumps(request_data), headers={'Content-type': 'application/json'}, timeout=10, auth=(config('SCOUT_BACKEND_ADMIN_USERNAME'), config('SCOUT_BACKEND_ADMIN_PASSWORD'))) if 200 <= x.status_code < 300: return Response({STATUS: SUCCESS}) else: sentry_debug_logger.error(str(x.status_code) + str(x.content)) return Response({STATUS: ERROR, 'message': 'An Error Occured'})
def get_amenities_json_from_move_out_request_id(move_out_request_id): move_out_request = TenantMoveOutRequest.objects.using(settings.HOMES_DB).filter(id=move_out_request_id).first() from scouts.sub_tasks.utils import MOVE_OUT_AMENITIES_CHECKUP_DEFAULT_JSON initial_json = MOVE_OUT_AMENITIES_CHECKUP_DEFAULT_JSON try: if move_out_request: for booking_facility in move_out_request.tenant.current_booking.facilities.all(): name = booking_facility.item.name quantity = booking_facility.quantity status = booking_facility.status booking_facility_id = booking_facility.id initial_json['data']['amenities_dict'][str(booking_facility_id)] = { 'id': booking_facility_id, 'name': name, 'quantity': quantity, 'status': status } except Exception as E: sentry_debug_logger.error(E, exc_info=True) return initial_json
def scout_task_assignment_request_post_save_hook(sender, instance, created, **kwargs): # just sending the notification task = instance.task if created and task: new_task_notification_category, _ = ScoutNotificationCategory.objects.get_or_create( name=NEW_TASK_NOTIFICATION) from scouts.api.serializers import NewScoutTaskNotificationSerializer ScoutNotification.objects.create( category=new_task_notification_category, scout=instance.scout, payload=NewScoutTaskNotificationSerializer(task).data, display=False) try: send_date = timezone.now() + timedelta(seconds=20) scout_assignment_request_set_rejected.apply_async([instance.id], eta=send_date) # sentry_debug_logger.debug('auto rejecting after 2 minutes', exc_info=True) except Exception as E: sentry_debug_logger.error('error while auto rejecting task is ' + str(E), exc_info=True)
def update(self, instance, validated_data): print(validated_data) try: original_amenities_list = instance.amenities_json['data']['amenities_dict'] amenities_patch_list = validated_data['data']['amenities_dict'] for i in amenities_patch_list: amenity_id = str(amenities_patch_list[str(i)]['id']) if str(amenity_id) != str(i): raise ValidationError({'error': 'Inconistent Data'}) validate_amenity_id(amenity_id) amenity_patch_data = amenities_patch_list[amenity_id] original_amenities_list[amenity_id] = amenity_patch_data except ValidationError as E: raise E except Exception: sentry_debug_logger.error("error in updating scout property on board amenity lising", exc_info=True) raise ValidationError({'detail': 'Bad Request'}, code=400) return super(PropertyOnBoardHouseAmenitiesUpdateSerializer, self).update(instance, validated_data)
def notify_scout_new_move_out_task(tenant_moveout_requirement_id): try: from Homes.Tenants.models import TenantMoveOutRequest move_out_request = TenantMoveOutRequest.objects.filter( id=tenant_moveout_requirement_id).first() if move_out_request: tenant = move_out_request.tenant booking_id = tenant.current_booking.id space_id = tenant.current_booking.space.id house_id = tenant.current_booking.space.house.id move_out_request_id = move_out_request.id data = { 'house_id': house_id, 'space_id': space_id, 'booking_id': booking_id, 'move_out_request_id': move_out_request_id } task_type = MOVE_OUT send_scout_task_to_scout_backend(data=data, task_type=task_type) except Exception as E: sentry_debug_logger.error( 'error occurred while sending scout_task_for_move_out' + str(E), exc_info=True)
def rate_scout(request): try: data = request.data scout_id = data['scout_id'] task_id = data['task_id'] rating = int(data['rating']) review_tag_ids = data.get('review_tags', []) remarks = data.get('remarks', '') if rating < 1 or rating > 5: return Response({ STATUS: ERROR, 'message': 'Rating must lie between 1 to 5' }) scout_task = ScoutTask.objects.get(scout__id=scout_id, id=task_id, rating_given=False, status=COMPLETE) customer = Customer.objects.using( settings.HOMES_DB).get(user=request.user) task_customer = None # to verify the same customer if scout_task.category == ScoutTaskCategory.objects.filter( name=HOUSE_VISIT).first(): house_visit = HouseVisit.objects.using(settings.HOMES_DB).get( id=scout_task.visit_id, visited=True) task_customer = house_visit.customer else: return Response({ STATUS: ERROR, 'message': 'No task category found' }) if task_customer == customer: # Manage Rating and Reviewing scout_task.rating = rating scout_task.remarks = remarks for review_tag_id in review_tag_ids: review_tag = ScoutTaskReviewTagCategory.objects.get( id=review_tag_id) if review_tag not in scout_task.review_tags.all(): scout_task.review_tags.add(review_tag) else: scout_task.review_tags.remove(review_tag) scout = scout_task.scout if review_tag not in scout.review_tags.all(): scout.review_tags.add(review_tag) else: scout.review_tags.remove(review_tag) scout_task.rating_given = True scout_task.save() success_response = { STATUS: SUCCESS, DATA: { 'rating': scout_task.scout.rating, 'remarks': scout_task.remarks } } return Response(success_response) else: return Response( { STATUS: ERROR, 'message': 'You are not allowed to rate this task' }, status=status.HTTP_403_FORBIDDEN) except Exception as E: sentry_debug_logger.error('some error occured' + str(E), exc_info=True) error_response = {STATUS: ERROR, 'message': 'Some error occured'} return Response(error_response)
def post(self, request): data = request.data['data'] if request.data[TASK_TYPE] == HOUSE_VISIT: # Create a task task_category = ScoutTaskCategory.objects.get(name=HOUSE_VISIT) # fetch visit details house_id = House.objects.using( settings.HOMES_DB).get(id=data['house_id']).id visit_id = HouseVisit.objects.using(settings.HOMES_DB).get( id=data['visit_id'], house_id=house_id).id scheduled_at = HouseVisit.objects.using( settings.HOMES_DB).get(id=visit_id).scheduled_visit_time scout_task = ScoutTask.objects.create( category=task_category, house_id=house_id, visit_id=visit_id, scheduled_at=scheduled_at, status=UNASSIGNED, earning=task_category.earning) scout_task.sub_tasks.add( *list(task_category.sub_task_categories.all())) # Select a scout for a particular task and create a Scout Task Assignment Request try: scout = get_appropriate_scout_for_the_task( task=scout_task, scouts=Scout.objects.filter(active=True)) if scout: ScoutTaskAssignmentRequest.objects.create(task=scout_task, scout=scout) return JsonResponse({'detail': 'done'}) else: return JsonResponse({'detail': 'No scout found'}, status=400) except Exception as E: sentry_debug_logger.error( 'Error while creating new scout with error' + str(E), exc_info=True) return JsonResponse({'detail': 'No new scout found'}) elif request.data[TASK_TYPE] == MOVE_OUT: # Create a task move_out_task_category = ScoutTaskCategory.objects.get( name=MOVE_OUT) # fetch visit details house_id = House.objects.using( settings.HOMES_DB).get(id=data['house_id']).id booking_id = Booking.objects.using( settings.HOMES_DB).get(id=data['booking_id']).id move_out_request = TenantMoveOutRequest.objects.using( settings.HOMES_DB).get(id=data['move_out_request_id']) scheduled_at = move_out_request.timing scout_task = ScoutTask.objects.create( category=move_out_task_category, house_id=house_id, booking_id=booking_id, scheduled_at=scheduled_at, status=UNASSIGNED, move_out_request_id=move_out_request.id, earning=move_out_task_category.earning) scout_task.sub_tasks.add( *list(move_out_task_category.sub_task_categories.all())) # Select a scout for a particular task and create a Scout Task Assignment Request try: scout = get_appropriate_scout_for_the_task( task=scout_task, scouts=Scout.objects.filter(active=True)) if scout: ScoutTaskAssignmentRequest.objects.create(task=scout_task, scout=scout) return JsonResponse({'detail': 'done'}) else: return JsonResponse({'detail': 'No scout found'}, status=400) except Exception as E: sentry_debug_logger.error( 'Error while creating new scout with error' + str(E), exc_info=True) return JsonResponse({'detail': 'No new scout found'}) elif request.data[TASK_TYPE] == HOUSE_VISIT_CANCELLED: data = request.data['data'] scout_task = ScoutTask.objects.filter( category=ScoutTaskCategory.objects.get(name=HOUSE_VISIT), house_id=data['house_id'], visit_id=data['visit_id']).first() scout = scout_task.scout if scout_task: scout_task.status = CANCELLED scout_task.scout = None scout_task.save() house_visit_cancel_notification_category, _ = ScoutNotificationCategory.objects. \ get_or_create(name=HOUSE_VISIT_CANCELLED) if scout: ScoutNotification.objects.create( category=house_visit_cancel_notification_category, scout=scout, payload=ScoutTaskDetailSerializer(scout_task).data, display=True) return JsonResponse({STATUS: SUCCESS}) else: return JsonResponse({ STATUS: ERROR, 'message': "No such task exists" }) elif request.data[TASK_TYPE] == PROPERTY_ONBOARDING: # Create a task property_on_board_task_category = ScoutTaskCategory.objects.get( name=PROPERTY_ONBOARDING) property_onboarding_details_serializer = PropertyOnboardingDetailSerializer( data=data) property_onboarding_details_serializer.is_valid( raise_exception=True) property_onboarding_details = property_onboarding_details_serializer.save( ) scheduled_at = property_onboarding_details.scheduled_at scout_task = ScoutTask.objects.create( category=property_on_board_task_category, scheduled_at=scheduled_at, status=UNASSIGNED, earning=property_on_board_task_category.earning, onboarding_property_details_id=property_onboarding_details.id) scout_task.sub_tasks.add(*list( property_on_board_task_category.sub_task_categories.all())) # Select a scout for a particular task and create a Scout Task Assignment Request try: # If we send this parameter then this scout with the provided id will be chosen manually_chosen_scout_id = data.get("manually_chosen_scout_id", None) if manually_chosen_scout_id: scout = Scout.objects.filter( id=manually_chosen_scout_id).first() # don't divert call if rejected because this task is created by scout itself pass_to_another_scout = False else: scout = get_appropriate_scout_for_the_task( task=scout_task, scouts=Scout.objects.filter(active=True)) pass_to_another_scout = True if scout: ScoutTaskAssignmentRequest.objects.create( task=scout_task, scout=scout, pass_to_another_scout=pass_to_another_scout) return JsonResponse({'detail': 'done'}) else: return JsonResponse({'detail': 'No scout found'}, status=400) except Exception as E: sentry_debug_logger.error( 'Error while creating new scout with error' + str(E), exc_info=True) return JsonResponse({'detail': 'No new scout found'})
from utility.logging_utils import sentry_debug_logger # from .tasks import run_zohocrm_startup from utility.zohocrm.zohocrm_leads import get_oauthclient_oauth_token_access_token try: get_oauthclient_oauth_token_access_token() except Exception as E: sentry_debug_logger.error(E, exc_info=True)
def create_tenant_lead_data_from_zoho_lead_data(lead_data): assert (lead_data['Lead_Type'] == 'Tenant') name = lead_data['Name1'] gender = lead_data['Gender'] phone_no = lead_data['Mobile'] email = lead_data['Email'] description = lead_data['Description'] zoho_id = lead_data['id'] created_by = lead_data['Created_By'] demand = lead_data['Demand'] zone = lead_data['Zone'] street = lead_data['Street'] accomodation_for = [str(i).lower() for i in lead_data['Accommodation_For']] space_type = None space_sub_type = lead_data['Space_Sub_Type'] arguments_create_dict = {} # Space Type try: accomodation_type = lead_data['Accomodation_Type1'] if accomodation_type: space_type = get_reverse_dictionary_from_list_of_tuples(HouseSpaceTypeCategories)[accomodation_type] except Exception as E: sentry_debug_logger.error(E, exc_info=True) if name: arguments_create_dict['name'] = name if gender: try: arguments_create_dict['gender'] = get_reverse_dictionary_from_list_of_tuples(GenderChoices)[gender] except Exception as E: sentry_debug_logger.error(E, exc_info=True) if phone_no: arguments_create_dict['phone_no'] = phone_no if email: arguments_create_dict['email'] = email if description: arguments_create_dict['description'] = description if zoho_id: arguments_create_dict['zoho_id'] = zoho_id if space_type: arguments_create_dict['space_type'] = space_type if accomodation_for: arguments_create_dict['accomodation_for'] = accomodation_for if created_by: lead_manager = LeadManager.objects.filter(zoho_id=created_by['id']).first() if lead_manager: arguments_create_dict['created_by'] = lead_manager if space_sub_type: arguments_create_dict['space_subtype'] = space_sub_type if len(demand): try: if demand[0]['Move_In_Date']: arguments_create_dict['expected_movein_start'] = demand[0]['Move_In_Date'] if demand[0]['Rental_Budget']: arguments_create_dict['expected_rent_min'] = demand[0]['Rental_Budget'] if demand[0]['Max_Rental_Budget']: arguments_create_dict['expected_rent_max'] = demand[0]['Max_Rental_Budget'] if demand[0]['TO_Move_in_date']: arguments_create_dict['expected_movein_end'] = demand[0]['TO_Move_in_date'] except Exception as E: sentry_debug_logger.error(E, exc_info=True) category, _ = LeadSourceCategory.objects.get_or_create(name=lead_data['Lead_Source']) lead = TenantLead.objects.create(**arguments_create_dict) lead.source.category = category lead.source.name = '' lead.source.save() lead.save() address_update_condition = zone or street if address_update_condition: if zone: lead.preferred_location.zone = zone if street: lead.preferred_location.street_address = street lead.preferred_location.save() lead.save()
def create_zoho_lead_from_tenant_lead_task(tenant_lead_id): time.sleep(1) # added sleep so that related objects data is also received tenant_lead = TenantLead.objects.get(id=tenant_lead_id) if not tenant_lead.zoho_id: # send record only if it does not have zoho id initally try: tenant_lead = TenantLead.objects.get( id=tenant_lead.id) # updated instance print(tenant_lead.preferred_location) record_ins_list = list() record = ZCRMRecord.get_instance('Leads') full_name = tenant_lead.name last_name = str(full_name) if full_name: last_name = full_name.split()[-1] record.set_field_value('Last_Name', last_name) record.set_field_value('Lead_Type', 'Tenant') if tenant_lead.name: record.set_field_value('Name1', tenant_lead.name) if tenant_lead.gender: record.set_field_value('Gender', tenant_lead.gender) if tenant_lead.phone_no: record.set_field_value('Mobile', tenant_lead.phone_no) if tenant_lead.email: record.set_field_value('Email', tenant_lead.email) if tenant_lead.description: record.set_field_value('Description', tenant_lead.description) if tenant_lead.space_type: record.set_field_value('AccomodationType', tenant_lead.space_type) if tenant_lead.space_subtype: record.set_field_value('Space_Sub_Type', tenant_lead.space_subtype) if tenant_lead.accomodation_for: record.set_field_value('Accommodation_For', tenant_lead.accomodation_for) if tenant_lead.preferred_location: if tenant_lead.preferred_location.street_address: record.set_field_value( 'Street', tenant_lead.preferred_location.street_address) if tenant_lead.preferred_location.zone: record.set_field_value('Zone', tenant_lead.preferred_location.zone) if tenant_lead.created_by.zoho_id: user = ZCRMUser.get_instance(tenant_lead.created_by.zoho_id) record.set_field_value('Owner', user) # Demand Data demand_data = {} if tenant_lead.expected_rent_min: demand_data['Rental_Budget'] = int( tenant_lead.expected_rent_min) if tenant_lead.expected_rent_max: demand_data['Max_Rental_Budget'] = int( tenant_lead.expected_rent_max) if tenant_lead.expected_movein_start: demand_data[ 'Move_In_Date'] = tenant_lead.expected_movein_start.strftime( "%Y-%m-%d") if tenant_lead.expected_movein_end: demand_data[ 'TO_Move_in_date'] = tenant_lead.expected_movein_end.strftime( "%Y-%m-%d") if demand_data: record.set_field_value('Demand', [demand_data]) record_ins_list.append(record) resp = ZCRMModule.get_instance('Leads').create_records( record_ins_list) bulk_entity_response, bulk_status_code = resp.bulk_entity_response, resp.status_code single_record_data = bulk_entity_response[0] if bulk_status_code == 201: if single_record_data.status == SUCCESS: tenant_lead.zoho_id = single_record_data.response_json[ 'details']['id'] tenant_lead.save() else: sentry_debug_logger.debug( 'status code for single record is' + str(single_record_data.status) + 'and message is' + str(single_record_data.message)) else: print(single_record_data.details) sentry_debug_logger.debug('status code for bulk record is' + str(resp.status_code) + 'and message is' + str(resp.message) + "error due to" + str(single_record_data.details)) # print(resp.status_code) # entity_responses = resp.bulk_entity_response # for entity_response in entity_responses: # print(entity_response.details) # print(entity_response.status) # print(entity_response.message) except ZCRMException as ex: # print(ex.status_code) # print(ex.error_message) # print(ex.error_code) # print(ex.error_details) # print(ex.error_content) print(traceback.format_exc()) sentry_debug_logger.error(ex, exc_info=True) except Exception as E: print(traceback.format_exc()) sentry_debug_logger.error(E, exc_info=True)