def Filter_circuits(self): """ Returns a dictionary with all the circuits that are able to be recommended to user """ R = RedisQuery(self.user) #Get circuits ids user has visited #vis_cts_ids = R.user_visited_circuits() # Get possibly recommended circuits if self.has_gmac: # TODO: @mathiasbc , improve with Redis dont query to DB all_gmacs_ids = GMAC.get_id_list_from_redis(self.gmac.pk) all_gmacs = GMAC.objects.filter(pk__in=all_gmacs_ids) circuits = Circuit.filter_by_gmacs(all_gmacs) all_circuits = [] for ct in circuits: if ct.pk not in all_circuits: all_circuits.append(ct.pk) else: all_circuits = R.all_circuits() # Initiate scores dict with posible recommendation circuits for i in all_circuits: #if i not in vis_cts_ids: self.scores[i] = 0
def test_recsys(request, gmac_slug=None): """ Testing recsys view """ from recsys.circuits.circuit_recsys import CircuitRecsys # Find GMAC gmacs = None gmac_id = None gmac_get_short_formatted_name = None filtered_circuits = None if gmac_slug is not None: try: gmacs = GMAC.objects.filter(slug=gmac_slug) filtered_circuits = Circuit.filter_by_gmacs(gmacs) gmac_get_short_formatted_name = gmacs[0].get_short_formatted_name() gmac_id = gmacs[0].pk except GMAC.DoesNotExist: pass except IndexError: pass categories = circuit_category_list() if request.user.is_authenticated(): # Recsys functionallity recsys = CircuitRecsys(request.user.pk) if gmac_id is None: gmac_id = request.session.get('gmac_id', None) if gmac_id is not None: recsys.Set_gmac(gmac_id) circuits_ids = recsys.Give_me_the_fun(top_n=15) circuits = Circuit.objects.filter(id__in=circuits_ids).\ filter(published=True) if filtered_circuits is not None : circuits = [val for val in filtered_circuits if val in circuits] else: if gmac_id is not None: gmac = GMAC.objects.get(pk=gmac_id) circuits = Circuit.filter_by_gmac(gmac) else: # case when gmac is not detected cpp = circuit_settings.DEFAULT_CIRCUITS_LIMIT circuits=Circuit.objects.all()[:cpp] return render(request, 'circuits/recommendations.html', { 'categories': categories, 'circuits': circuits, 'topbar_item': 'explore', 'page_type': 'recommendation', 'GOOGLE_API_KEY': settings.GOOGLE_API_KEY, 'gmac_slug': gmac_slug, 'gmacs': gmacs, 'gmac_get_short_formatted_name': gmac_get_short_formatted_name, }, )
def test_recsys(request, gmac_slug=None): """ Testing recsys view """ from recsys.circuits.circuit_recsys import CircuitRecsys # Find GMAC gmacs = None gmac_id = None gmac_get_short_formatted_name = None filtered_circuits = None if gmac_slug is not None: try: gmacs = GMAC.objects.filter(slug=gmac_slug) filtered_circuits = Circuit.filter_by_gmacs(gmacs) gmac_get_short_formatted_name = gmacs[0].get_short_formatted_name() gmac_id = gmacs[0].pk except GMAC.DoesNotExist: pass except IndexError: pass categories = circuit_category_list() circuits = Circuit.objects.filter(**params) circuits = circuits.filter(published=True) circuits = circuits.select_related(depth=3).order_by( '-highlighted', '-modified' )[:cpp] return render(request, 'circuits/recommendations.html', { 'categories': categories, 'circuits': circuits, 'topbar_item': 'explore', 'page_type': 'recommendation', 'STATIC_MAPS_ROOT': settings.STATIC_MAPS_ROOT, 'GOOGLE_API_KEY': settings.GOOGLE_API_KEY, 'gmac_slug': gmac_slug, 'gmacs': gmacs, 'gmac_get_short_formatted_name': gmac_get_short_formatted_name, }, )
def get_circuits(self): """ Returns a Queryset containing the circuits to display on CircuitCollection """ limit = self.cleaned_data.get('limit') offset = self.cleaned_data.get('offset') category = self.cleaned_data.get('category') gmac_id = self.cleaned_data.get('gmac_id') gmac_slug = self.cleaned_data.get('gmac_slug') if limit is None: limit = constants.API_DEFAULT_CIRCUITS_LIMIT if offset is None: offset = constants.API_DEFAULT_CIRCUITS_OFFSET # try to filter circuit by GMAC if gmac_id is not None: gmac = GMAC.objects.get(pk=gmac_id) circuits = Circuit.filter_by_gmac(gmac) # try to filter by gmac_slug elif gmac_slug is not None: gmacs = GMAC.objects.filter(slug=gmac_slug) circuits = Circuit.filter_by_gmacs(gmacs) # if not filtered by GMAC then filter by any other criteria else: circuits = Circuit.objects.all() circuits = circuits.order_by('-created') if category is not None and len(category)>0: if self.category_is_alphanumeric(): category = CircuitCategory(category) category = category.get_value() circuits = circuits.filter(category=category) return circuits[offset:offset + limit]
def show_home(request, gmac_slug=None): # Find GMAC gmacs = None if gmac_slug is not None: try: gmacs = GMAC.objects.filter(slug=gmac_slug) except GMAC.DoesNotExist: pass params = {} gmac_get_short_formatted_name = None if gmacs is not None: params['pk__in'] = [c.pk for c in Circuit.filter_by_gmacs(gmacs)] gmac_get_short_formatted_name = gmacs[0].get_short_formatted_name() cpp = circuit_settings.DEFAULT_CIRCUITS_LIMIT if request.is_ajax(): page = request.POST.get('page', None) if page is not None: page = int(page) else: error_doc = OrderedDict() error_doc['error'] = unicode(strings.MISSING_PAGE_PARAM_ERROR) return HttpResponse( content=render_as_json(error_doc), content_type='application/json' ) response = { 'page': int(page) + 1 } circuits = Circuit.objects.filter(**params) circuits = circuits.filter(published=True).order_by( '-highlighted', '-modified' )[page * cpp: cpp * (page + 1)] if circuits: html_response = u'' for circuit in circuits: html_response += render_to_string( 'circuits/circuit_list_item.html', {'circuit': circuit, 'user': request.user}) response['raw_html'] = html_response else: response['hide_button'] = True return HttpResponse( json.dumps(response), mimetype='application/json' ) categories = circuit_category_list() # Circuits that are not flagged as 18+ circuits = Circuit.objects.filter(adult_content=False) circuits = circuits.filter(published=True).order_by('?')[:cpp] return render(request, 'circuits/recommendations.html', { 'categories': categories, 'circuits': circuits, 'topbar_item': 'explore', 'page_type': 'recommendation', 'GOOGLE_API_KEY': settings.GOOGLE_API_KEY, 'gmac_slug': gmac_slug, 'gmacs': gmacs, 'gmac_get_short_formatted_name': gmac_get_short_formatted_name, }, )
def circuit_category_listing(request, category_slug, gmac_slug=None): """ Displays the first 10 circuits of a category """ # Find GMAC gmacs = None if gmac_slug is not None: try: gmacs = GMAC.objects.filter(slug=gmac_slug) except GMAC.DoesNotExist: pass cpp = circuit_settings.DEFAULT_CIRCUITS_LIMIT try: category_value = CIRCUIT_CATEGORY_CHOICES.get_value( category_slug.upper()) except KeyError: raise Http404 categories = circuit_category_list() category = CircuitCategory(category_value) params = {} params['category'] = category_value gmac_get_short_formatted_name = None if gmacs is not None: params['pk__in'] = [c.pk for c in Circuit.filter_by_gmacs(gmacs)] gmac_get_short_formatted_name = gmacs[0].get_short_formatted_name() if request.is_ajax(): page = request.POST.get('page', None) if page is not None: page = int(page) else: error_doc = OrderedDict() error_doc['error'] = 'Missing POST parameter: page' return HttpResponse(content=render_as_json(error_doc), content_type='application/json') response = {'page': int(page) + 1} circuits = Circuit.objects.available().filter( **params).order_by('-modified')[page * cpp:(page + 1) * cpp] if circuits: html_response = u'' for circuit in circuits: html_response += render_to_string( 'circuits/circuit_list_item.html', { 'circuit': circuit, 'user': request.user }) response['raw_html'] = html_response else: response['hide_button'] = True return HttpResponse(content=json.dumps(response), content_type='application/json') # FIXME circuits filter should also include location of user circuits = Circuit.objects.available().filter( **params).order_by('-modified')[:cpp] return render( request, 'circuits/categories.html', { 'categories': categories, 'category': category, 'selected_category': category_value, 'category_slug': category_slug, 'is_preferred_category': category.is_followed(request.user), 'circuits': circuits, 'topbar_item': 'explore', 'page_type': 'category', 'gmacs': gmacs, 'gmac_slug': gmac_slug, 'STATIC_MAPS_ROOT': settings.STATIC_MAPS_ROOT, 'GOOGLE_API_KEY': settings.GOOGLE_API_KEY, 'gmac_get_short_formatted_name': gmac_get_short_formatted_name, }, )
def circuit_category_listing(request, category_slug, gmac_slug=None): """ Displays the first 10 circuits of a category """ # Find GMAC gmacs = None if gmac_slug is not None: try: gmacs = GMAC.objects.filter(slug=gmac_slug) except GMAC.DoesNotExist: pass cpp = circuit_settings.DEFAULT_CIRCUITS_LIMIT try: category_value = CIRCUIT_CATEGORY_CHOICES.get_value( category_slug.upper() ) except KeyError: raise Http404 categories = circuit_category_list() category = CircuitCategory(category_value) params = {} params['category'] = category_value gmac_get_short_formatted_name = None if gmacs is not None: params['pk__in'] = [ c.pk for c in Circuit.filter_by_gmacs(gmacs)] gmac_get_short_formatted_name = gmacs[0].get_short_formatted_name() if request.is_ajax(): page = request.POST.get('page', None) if page is not None: page = int(page) else: error_doc = OrderedDict() error_doc['error'] = 'Missing POST parameter: page' return HttpResponse( content=render_as_json(error_doc), content_type='application/json' ) response = { 'page': int(page) + 1 } circuits = Circuit.objects.available().filter(**params).order_by( '-modified' )[page * cpp: (page + 1) * cpp] if circuits: html_response = u'' for circuit in circuits: html_response += render_to_string( 'circuits/circuit_list_item.html', {'circuit': circuit, 'user': request.user} ) response['raw_html'] = html_response else: response['hide_button'] = True return HttpResponse( content=json.dumps(response), content_type='application/json' ) # FIXME circuits filter should also include location of user circuits = Circuit.objects.available().filter(**params).order_by( '-modified' )[:cpp] return render(request, 'circuits/categories.html', { 'categories': categories, 'category': category, 'selected_category': category_value, 'category_slug': category_slug, 'is_preferred_category': category.is_followed(request.user), 'circuits': circuits, 'topbar_item': 'explore', 'page_type': 'category', 'gmacs': gmacs, 'gmac_slug': gmac_slug, 'STATIC_MAPS_ROOT': settings.STATIC_MAPS_ROOT, 'GOOGLE_API_KEY': settings.GOOGLE_API_KEY, 'gmac_get_short_formatted_name': gmac_get_short_formatted_name, }, )