def user_logged_in_receiver(sender, instance, request, *args, **kwargs): user = instance ip_address = get_client_ip(request) session_key = request.session.session_key UserSession.objects.create(user=user, ip_address=ip_address, session_key=session_key)
def object_viewed_receiver(sender, instance, request, *args, **kwargs): c_type = ContentType.objects.get_for_model(sender) # instance.__class__ new_view_obj = ObjectViewed.objects.create( user=request.user, content_type=c_type, object_id=instance.id, ip_address=get_client_ip(request))
def media(request, movie_id): # Analytics page_view(request) if request.method == 'POST': form = SearchForm(request.POST) if form.is_valid(): return redirect('/search/{}'.format(form.cleaned_data['search'])) else: form = SearchForm() try: media = Movie.objects.get(movie_id=movie_id) except: return HttpResponse(status=404) new_view = MovieView(movie_id=movie_id, ip=get_client_ip(request), media_type=media.media_type) new_view.save() if media.media_type == "movie": url = 'https://videospider.in/getvideo?key={}&video_id={}&tmdb=1'.format( player_key, media.movie_id) episode_urls = {} else: url = 'https://streamvideo.link/getvideo?key={}&video_id={}&tmdb=1&tv=1&s=1&e=1'.format( player_key, media.movie_id) with urllib.request.urlopen( 'https://api.themoviedb.org/3/tv/{}?api_key={}&language=en-US'. format(media.movie_id, tmdb_key)) as resp: data = json.loads(resp.read().decode("utf-8")) seasons = [] episodes = [] for i in data["seasons"]: seasons.append(i["id"]) episodes.append(i["episode_count"]) episode_urls = {} c1 = 1 for i in range(len(seasons)): c2 = 1 for j in range(len(episodes)): episode_url = 'https://streamvideo.link/getvideo?key={}&video_id={}&tmdb=1&tv=1&s={}&e={}'.format( player_key, media.movie_id, c1, c2) episode_tag = 's{}e{}'.format(c1, c2) episode_urls[episode_tag] = episode_url c2 += 1 c1 += 1 context = { 'url': url, 'media': media, 'episode_urls': episode_urls, 'form': form, } return render(request, 'home/media.html', context)
def get_context_data(self, *args, **kwargs): context = super(SearchProductView, self).get_context_data(*args, **kwargs) query = self.request.GET.get('q') context['query'] = query print(query) Search.objects.create(query=query, ip_address=get_client_ip(self.request)) return context
def object_view_receiver(sender, instance, request, *args, **kwargs): c_type = ContentType.objects.get_for_model(sender) user = None if request.user.is_authenticated: user = request.user new_view_object = ObjectViewed.objects.create( user=user, content_type=c_type, object_id=instance.id, ip_address=get_client_ip(request))
def object_viewed_receiver(sender, instance, request, *args, **kwargs): c_type = ContentType.objects.get_for_model(sender) ip_address = None try: ip_address = get_client_ip(request) except: pass ObjectViewed.objects.create( user=request.user, content_type=c_type, object_id=instance.id, ip_address=ip_address )
def test_get_client_ip(monkeypatch): request = {'HTTP_X_FORWARDED_FOR': "123.4.5.6"} def mockreturn(request): return BytesIO( json.dumps(results, sort_keys=True, indent=4, separators=(',', ': ')).encode()) monkeypatch.setattr(urllib.request, 'urlopen', mockreturn) test_IP = "123.4.5.6" assert test_IP == get_client_ip(request)