def merge_question_and_feedback(self, fedbacks=None, questions=None): ''' :param fedbacks: :param questions: :return: void - it's only modify self.questions ''' from itertools import groupby # Get feedbacks user_fedbacks = fedbacks f_sorted = sorted(user_fedbacks, key=lambda k: k['q_id']) grouped = groupby(f_sorted, lambda x: x['q_id']) groups = {k: list(v).pop() for k, v in grouped} # Get questions ids = list(groups.keys()) user_questions = questions or Insight.question_contents(ids).json() # For every question adds a feedback property containing a list of the answers of both the users for k, feedback in groups.items(): id = str(feedback['q_id']) if id in user_questions: fb = [{'label': feedback['answer_value'], 'value': feedback['answer_id']}] user_questions[id]['feedbacks'] = fb if not 'feedbacks' in user_questions[id] else user_questions[id]['feedbacks'] + fb if questions is None: user_questions[id]['is_private'] = feedback['is_private'] return user_questions
def reccomended(self): try: res = Insight.reccomended_user(self.crm_id) users_ids = res.json()['users'][0]['recommended_users'] profiles = Profile.objects.filter(crm_id__in=users_ids) return profiles except: pass
def post(self, request, action=None): ''' :param request: :param action: :return: 200 code for success 403 if there is an error + {'error': [Error description]} ''' temp_id = request.data.get('temp_id', None) question_id = request.data.get('question_id', None) feedback = request.data.get('feedback', None) is_private = request.data.get('is_private', None) try: # Send Chatbot Feedback to Insight if action == 'chatbot' and request.user.is_authenticated: crm_id = request.user.profile.crm_id # Entity Feedback if temp_id is not None: return Response(Insight.feedback(temp_id=temp_id, crm_id=crm_id, feedback=feedback)) # Change Question privacy elif feedback is None and is_private is not None: return Response(Insight.question_privacy(crm_id=crm_id, question_ids=[question_id], is_private=is_private)) # Send answer to question elif question_id is not None: return Response(Insight.question_feedback(crm_id=crm_id, question_id=question_id, answer_id=feedback)) # Update User not action and self.update_user(request) except Exception as e: print(' ###################################################') print(e) return Response(data={'error': str(e) }, status=403) return Response()
def update_user(self, request): ''' :param request: :return: SUCCESS : void ; ERROR : 403 response ''' user = request.user profile = request.user.profile print('im on user update') city = request.data.get('city', profile.city) place = request.data.get('place', profile.place) # try: # User user.first_name = request.data.get('first_name', user.first_name) user.last_name = request.data.get('last_name', user.last_name) # Profile profile.city = city if not not city else profile.city profile.place = place if bool(place) else profile.palce profile.birthdate = request.data.get('birthdate', profile.birthdate) profile.occupation = request.data.get('occupation', profile.occupation) profile.statement = request.data.get('statement', profile.statement) profile.gender = request.data.get('gender', profile.gender) # Activity profile.activity('area', request.data.get('area', None)) profile.activity('technology', request.data.get('technology', None)) profile.activity('skills', request.data.get('skills', None)) profile.activity('domain', request.data.get('domain', None)) # Profile Extra #profile.tags_create_or_update(request.data.get('tags', None), clear=True) picture = request.data.get('picture', None) profile.picture_set_or_update(picture) if picture and len(picture) > 0 else None user.save() profile.save() try: party = profile.create_or_update_to_crm(profile.user) crm_id = party and party.get_crm_id() crm_id and Insight.notify_user_creation(crm_id) except Exception as e: logger.log('error', e)
def profile_questions(self, request, profile_id): ''' :param request: :param profile_id: :return: void - it's only modify self.questions ''' try: crm_ids = [ Profile.objects.filter(pk=request.user.profile.id).first().crm_id, Profile.objects.filter(pk=profile_id).first().crm_id ] are_the_same_profiles = crm_ids[0] == crm_ids[1] # Get feedbacks from insight feedbacks = Insight.profile_questions(crm_ids) # check if the response from insight is ok if len(feedbacks) > 0: logged_user_feedbacks = feedbacks[0]['feedbacks']['questions'] target_user_feedbacks = feedbacks[1]['feedbacks']['questions'] # if the target user does not have questions stop the execution if len(target_user_feedbacks) < 1: self.questions = [] return # Create list of merged questions else: self.questions = self.merge_question_and_feedback(target_user_feedbacks) if not are_the_same_profiles: self.questions = self.merge_question_and_feedback(logged_user_feedbacks, self.questions) self.questions = [v for k, v in self.questions.items() if not v['is_private']] else: self.questions = [] except KeyboardInterrupt as e: print(e)
def get(self, request, entity, entity_id): results = [] if entity == 'projects': local_entities = Project.objects.get(pk=entity_id) results = ProjectSerializer(local_entities).data elif entity == 'challenges': local_entities = Challenge.objects.get(pk=entity_id) results = ChallengeSerializer(local_entities).data elif entity == 'profile': local_entities = Profile.objects.get(pk=entity_id) results = ProfileSerializer(local_entities).data else: try: method_to_call = 'get_' + entity + '_detail' # results = getattr(DSPConnectorV13, method_to_call)(entity_id=entity_id)[entity][0] results = Insight.entity_details(entity, entity_id) except DSPConnectorException: pass except AttributeError as a: pass return Response(results)
def create(cls, **kwargs): user = kwargs.get('user', None) tags = kwargs.get('tags', None) profile = cls(user=user) profile.picture = kwargs.get('picture', None) profile.gender = kwargs.get('gender', None) profile.birthdate = kwargs.get('birthdate', None) profile.city = kwargs.get('city', None) profile.occupation = kwargs.get('occupation', None) profile.place = kwargs.get('place', None) profile.reset_token = Profile.get_new_reset_token() profile.save() tags and profile.tags_create_or_update(tags) # Set activities as tags profile.activity('area', kwargs.get('area', None)) profile.activity('technology', kwargs.get('technology', None)) profile.activity('skills', kwargs.get('skills', None)) profile.activity('domain', kwargs.get('domain', None)) profile.save() try: # Create user on CRM party = cls.create_or_update_to_crm(profile.user) # Try to set crm_id to profile model profile.crm_id = party.get_crm_id() if party else None profile.save() # Notify insight about the new user profile.crm_id is not None and Insight.notify_user_creation(profile.crm_id) except Exception as e: print('[ERROR : dashboard.models.profile.create]') print(e) return profile
def chatbot_question(self, request): ''' :param request: :return: void - it's only modify self.questions ''' entity_name = request.query_params.get('entity_name', None) entity_id = request.query_params.get('entity_id', None) temp_id = request.query_params.get('entity_temp_id', None) profile_id = request.query_params.get('profile_id', None) try: welcome = self.question('welcome', first_name=request.user.first_name) bye = self.question('nice_talking', first_name=request.user.first_name) crm_id = request.user.profile.crm_id response = Insight.questions(crm_ids=[crm_id], amount=10) if response.status_code < 205: res_dict = response.json() questions = res_dict['users'][0]['questions'] \ if isinstance(res_dict['users'], dict) or isinstance(res_dict['users'], list) \ else [] if len(questions) > 0: self.questions = [welcome] + [self.map_remote_to_local_questions(q) for q in questions] + [bye] else: self.questions = [self.question('no_questions', first_name=request.user.first_name)] else: print('[dashboard.questions.questions.chatbot_question] CHATBOT QUESTION insight response error') print(response) except Exception as e: print('[ERROR : dashboard.questions.questions.chatbot_question]') print(e) self.questions = self.questions = [self.question('no_questions', first_name=request.user.first_name)]
def interest(request, entity, user_id=None): """ :param request: :param entity: :param user_id: :return: GET: return all the interest shown by specified user that belongs to specific entitiy type if no user id specified will use the logged user """ profile = request.user.profile if \ request.user.is_authenticated and \ not user_id else \ Profile.objects.filter(pk=user_id).first() if not profile: return Response({}, status=status.HTTP_404_NOT_FOUND) try: singular_entity = EntityProxy.singular_name(entity) if entity in [ 'projects', 'challenges' ] else entity interests = profile.get_interests(entity) if entity in ['news', 'events']: ids = [x.externalId for x in interests] res = Insight.entity_details(entity, ids) else: model_serializer = ModelHelper.get_serializer( singular_entity.capitalize()) res = model_serializer(interests, many=True).data return Response(res) except Exception as e: print('ERROR[dashboard.api14.interest]') print(e) return Response({}, status=status.HTTP_400_BAD_REQUEST)
def test2_recommend_profiles(self): resp = Insight.reccomended_user(self.crm_id) print(resp.json()) self.assertLess(resp.status_code, 205)
def test2_recommend_profiles_response(self): resp = Insight.reccomended_user(self.crm_id) self.assertLess(resp.status_code, 205)
def test_1_connection(self): print(Colorizer.LightPurple('\n[ CRM Test : Connection test ]')) response = Insight.questions(crm_ids=[self.crm_id]) self.assertEqual(response.status_code, 200, '[INSIGHT-CONNECTOR ERROR] Response error ')
def reccomended_content(self, request, entity, page): return Insight.reccomended_entity( crm_id=request.user.profile.crm_id if request.user.is_authenticated else None, entity_name=entity, page=page)