def dashboard(request): try: topics_list = DSPConnectorV12.get_topics()['topics'] selected_topic = random.choice(topics_list) hot_news = DSPConnectorV12.search_news( selected_topic['topic_id'])['news'][:4] top_influencers = DSPConnectorV12.get_audiences( selected_topic['topic_id'])['audiences'][:4] except DSPConnectorException as e: messages.error(request, e.message) topics_list = {} selected_topic = 'No themes' context = {'selected_topic': selected_topic, 'topics': topics_list} return render(request, 'dashboard/theme.html', context) hot_tags = [t[0] for t in Profile.get_hot_tags(30)] last_members = Profile.get_last_n_members(3) context = { 'selected_topic': selected_topic, 'topics': topics_list, 'last_members': last_members, 'hot_tags': hot_tags, 'json_hot_tags': json.dumps(hot_tags), 'hot_news': hot_news, 'top_influencers': top_influencers } return render(request, 'dashboard/dashboard.html', context)
def get_events(request, topic_ids, cursor=-1): try: events = DSPConnectorV12.get_events(topic_ids, cursor) except DSPConnectorException: events = {} return JsonResponse({'status': 'ok', 'result': events}, status=200)
def get_suggested_topic(request): try: topics = DSPConnectorV12.get_topics()['topics'] random_topic = topics[random.randint(0, len(topics) - 1)] except DSPConnectorException: random_topic = {} return JsonResponse({'status': 'ok', 'result': random_topic}, status=200)
def get(self, request, entity, user_id=None): """ :param request: :param entity: :param user_id: :return: """ #TODO make cursor works profile = None results = [] local_entities = None try: profile = request.user.profile except: profile = None # Local entities if entity == 'loved': return interest(request._request, entity='profile', user_id=user_id) elif entity == 'lovers': return interested(request._request, entity='profile', entity_id=user_id) elif entity == 'projects': local_entities = Project.objects.order_by('-end_date') # local_entities = Project.objects.order_by('-end_date') if not profile: local_entities = local_entities[:5] results = results+ProjectSerializer(local_entities, many=True).data # local_entities = Challenge.objects.order_by('-end_date') local_entities = Challenge.objects.order_by('-end_date') if not profile: local_entities = local_entities[:5] results = results+ChallengeSerializer(local_entities, many=True).data results = sorted(results, key=lambda k: k['end_date'] or '', reverse=False) elif entity == 'matches': local_entities = Profile.objects.get(pk=user_id).best_matches() results = ProfileSerializer(local_entities, many=True).data else: # Remote Entities try: topics_list = DSPConnectorV12.get_topics()['topics'] topics_id_list = [x['topic_id'] for x in topics_list] method_to_call = 'get_' + entity if not profile: selected_topic = random.choice(topics_id_list) results = getattr(DSPConnectorV13, method_to_call)(topic_id=selected_topic)[entity] results = results[:5] else: for index,topic_id in enumerate(topics_id_list): results.append(getattr(DSPConnectorV13, method_to_call)(topic_id=topic_id)[entity]) results = mix_result_round_robin(*results) except DSPConnectorException: pass except AttributeError as a: pass return Response(results[:20] if len(results) > 20 else results)
def theme(request, topic_id): print topic_id try: topics_list = DSPConnectorV12.get_topics()['topics'] selected_topic = filter(lambda x: str(x['topic_id']) == str(topic_id), topics_list)[0] if topic_id else \ random.choice(topics_list) except DSPConnectorException as e: messages.error(request, e.message) topics_list = {} selected_topic = 'No themes' except IndexError: return HttpResponseRedirect(reverse('dashboard:theme')) context = {'selected_topic': selected_topic, 'topics': topics_list} return render(request, 'dashboard/theme.html', context)
def get_news(request, topic_ids, date_name='yesterday', cursor=-1): date_dict = { 'yesterday': date.today() - timedelta(1), 'week': date.today() - timedelta(7), 'month': date.today() - timedelta(30) } try: since = date_dict[date_name].strftime('%d-%m-%Y') news = DSPConnectorV12.search_news(topic_ids, { 'since': since, 'cursor': cursor }) except DSPConnectorException: news = {} return JsonResponse({'status': 'ok', 'result': news}, status=200)
def get_entity(request, entity='news'): #TODO make cursor works profile = None results = [] local_entities = None try: profile = request.user.profile except: # NOt logged user profile = None try: topics_list = DSPConnectorV12.get_topics()['topics'] topics_id_list = [x['topic_id'] for x in topics_list] method_to_call = 'get_' + entity if not profile: selected_topic = random.choice(topics_id_list) results = getattr( DSPConnectorV13, method_to_call)(topic_id=selected_topic)[entity] results = results[:5] else: for index, topic_id in enumerate(topics_id_list): results.append( getattr(DSPConnectorV13, method_to_call)(topic_id=topic_id)[entity]) results = mix_result_round_robin(*results) except DSPConnectorException: pass except AttributeError as a: local_entities = Project.objects.all() if not profile: local_entities = local_entities[:5] results.extend(ProjectSerializer(local_entities, many=True).data) local_entities = Challenge.objects.all() if not profile: local_entities = local_entities[:5] results.extend(ChallengeSerializer(local_entities, many=True).data) return success('ok', 'entity list', results)
def events(request, topic_id): user_profile_location = {} user_profile_location['short_code'] = json.loads( Profile.get_by_email( request.user.email).place)['country_short'].lower() user_profile_location['label'] = json.loads( Profile.get_by_email(request.user.email).place)['country'] try: topics_list = DSPConnectorV12.get_topics()['topics'] selected_topic = filter(lambda x: str(x['topic_id']) == str(topic_id), topics_list)[0] if topic_id else \ random.choice(topics_list) except DSPConnectorException as e: messages.error(request, e.message) topics_list = {} selected_topic = 'No themes' except IndexError: return HttpResponseRedirect(reverse('dashboard:events')) context = { 'selected_topic': selected_topic, 'topics': topics_list, 'country': user_profile_location } return render(request, 'dashboard/events.html', context)
def theme(request, topic_id): selected_location = '' try: topics_list = DSPConnectorV12.get_topics()['topics'] selected_topic = filter(lambda x: str(x['topic_id']) == str(topic_id), topics_list)[0] if topic_id else \ random.choice(topics_list) except DSPConnectorException as e: messages.error(request, e.message) topics_list = {} selected_topic = 'No themes' except IndexError: return HttpResponseRedirect(reverse('dashboard:theme')) try: selected_location = json.loads( request.user.profile.place)['country_short'] except: selected_location = '' context = { 'selected_topic': selected_topic, 'topics': topics_list, 'selected_location': selected_location } return render(request, 'dashboard/theme.html', context)
def get_audiences(request, topic_id): try: audiences = DSPConnectorV12.get_audiences(topic_id) except DSPConnectorException: audiences = {} return JsonResponse({'status': 'ok', 'result': audiences}, status=200)
def get_topics(request): try: topics = DSPConnectorV12.get_topics() except DSPConnectorException: topics = {} return JsonResponse({'status': 'ok', 'result': topics}, status=200)