def recommended_list(self, request, instance): """ye recommended list bar assasse category va session""" if request.user.is_authenticated: viewed_item = self.filter_by_model( instance=instance).filter(user=request.user) elif request.user.is_anonymous: ip_address = get_client_ip(request) viewed_item = self.filter_by_model( instance=instance).filter(user__isnull=True, ip_address=ip_address) same_category = list() for item in viewed_item: # category e post haei ke user dide same_category = item.content_object.category.values_list( 'id', flat=True) queryset = instance.__class__.objects.filter( category__id__in=same_category) try: """ye session e listi misaze ke Model o bar assasse category e bala filter mikone""" request.session['same_categories'] += list(queryset.values_list( 'pk', flat=True)) # bareye in az values_list o pk estefade kardim ke dg niaz be serialize kardan nabshe except: request.session['same_categories'] = list( queryset.values_list('pk', flat=True)) same_item = instance.__class__.objects.filter( pk__in=request.session.get('same_categories')) return same_item
def __call__(self, request): if '/admin/' not in request.path and not 'iam_ghosting' in request.session: match = resolve(str(request.path)) instance = None try: if '_' in str(match.kwargs.keys()): myfilter = {} for key, val in list(match.kwargs.items())[:1]: key_val = str(key).split('_') myfilter[f'{key_val[1]}'] = val Model = ContentType.objects.get( model=str(key_val[0])).model_class() instance = Model.objects.get(**myfilter) except Exception as e: # print(str(e)) pass # ----------------------------------------------------- protocol = urlsplit(request.build_absolute_uri(None)).scheme current_site = get_current_site(request) current_path = request.path url = ''.join( [str(protocol), '://', str(current_site), str(current_path)]) # ----------------------------------------------------- if request.user.is_authenticated: UserTracker.objects.create_by_model( user=request.user, url=url, instance=instance, user_agent=str(get_user_agent(request)), ip_address=get_client_ip(request)) else: try: th_user = get_object_or_404(User, phone=request.session['phone']) except: th_user = None UserTracker.objects.create_by_model( user=th_user, url=url, instance=instance, user_agent=str(get_user_agent(request)), ip_address=get_client_ip(request)) response = self.get_response(request) return response
def dislike_btn_color(context, id, model_type): request = context['request'] ip_address = get_client_ip(request) content_type = ContentType.objects.get(model=model_type) Model = content_type.model_class() model_qs = get_object_or_404(Model, id=id) likedislike = LikeDislike.objects.filter_by_model(model_qs).filter( likedislike='dislike').first() try: if request.user.is_authenticated: if likedislike.user == request.user or likedislike.ip_address == ip_address: return True elif request.user.is_anonymous: if likedislike.ip_address == ip_address: return True except: return False
def create_for_instance_model(self, instance, request, likedislike, user=None): """ bar assasse model create mkonim age user anonymous bud bar assasse IP_address taghirat emal mishe age authenticate bud ke nega mikone bebine ip sh hast ya na age bud faqat user behesh mide... """ ip_address = get_client_ip(request) content_type = ContentType.objects.get_for_model(instance.__class__) try: queryset = self.get( models.Q( user=user, content_type=content_type, object_id=instance.id, ) | models.Q( ip_address=ip_address, content_type=content_type, object_id=instance.id, )) except: queryset = self.create( user=user, ip_address=ip_address, content_type=content_type, object_id=instance.id, likedislike=likedislike, ) else: like_signal.send(sender=queryset, ip_address=ip_address, likedislike=likedislike, request=request) return queryset