Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set test #17

Open
wants to merge 9 commits into
base: deploy
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 0 additions & 48 deletions .github/workflows/django.yml

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,5 @@ yarn-debug.log*
yarn-error.log*
*.obfuscated.js
temp_pdf.txt
fulldata.json
analytics/analytics.py
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# ecomm
[![Django CI](https://github.com/Venkatnvs/ecomm/actions/workflows/django.yml/badge.svg?branch=master)](https://github.com/Venkatnvs/ecomm/actions/workflows/django.yml)


<img src="https://github.com/Venkatnvs/ecomm/assets/97899253/c98ffd1a-6ebe-49b2-a312-dd1959c2c030" alt="Image" width="400" height="300">
<img src="https://github.com/Venkatnvs/ecomm/assets/97899253/67d602bd-b515-405d-b0cd-21636a97515a" alt="Image" width="400" height="300">

Expand Down Expand Up @@ -67,5 +66,3 @@ If you would like to contribute to the Django e-commerce website, you can do so

## License
See [License](Licence).


Empty file added account/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions account/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions account/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AccountConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'account'
Empty file added account/migrations/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions account/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
6 changes: 6 additions & 0 deletions account/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.urls import path
from .views import *

urlpatterns = [
path('', MainProfile, name='account-home'),
]
12 changes: 12 additions & 0 deletions account/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.shortcuts import render
from store.utilitys import GetCartData

def MainProfile(request):
data = GetCartData(request)
order = data['order']
items = data['items']
context = {
'items':items,
'order':order,
}
return render(request,"account/index.html",context)
Empty file added analytics/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions analytics/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django.contrib import admin
from .models import Visitor

admin.site.register(Visitor)
6 changes: 6 additions & 0 deletions analytics/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AnalyticsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'analytics'
48 changes: 48 additions & 0 deletions analytics/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Generated by Django 4.0.7 on 2023-09-12 06:28

from django.db import migrations, models
import django.db.models.deletion
import uuid


class Migration(migrations.Migration):

initial = True

dependencies = [
('clients', '0011_alter_selleruser_company_name'),
]

operations = [
migrations.CreateModel(
name='Visitor',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('uuid', models.UUIDField(default=uuid.uuid4, editable=False, unique=True)),
('count', models.PositiveIntegerField(blank=True, default=0, null=True)),
('url', models.URLField(blank=True, null=True)),
('title', models.CharField(blank=True, max_length=255, null=True)),
('duration', models.CharField(blank=True, max_length=255, null=True)),
('devicetype', models.CharField(blank=True, max_length=255, null=True)),
('useragent', models.TextField(blank=True, default='', null=True)),
('ipaddress', models.CharField(blank=True, max_length=255, null=True)),
('is_geolocation', models.BooleanField(default=True)),
('is_active', models.BooleanField(default=True)),
('city', models.CharField(blank=True, default=None, max_length=255, null=True)),
('continent_code', models.CharField(blank=True, default=None, max_length=255, null=True)),
('continent_name', models.CharField(blank=True, default=None, max_length=255, null=True)),
('country_code', models.CharField(blank=True, default=None, max_length=255, null=True)),
('country_name', models.CharField(blank=True, default=None, max_length=255, null=True)),
('dma_code', models.CharField(blank=True, default=None, max_length=255, null=True)),
('is_in_european_union', models.CharField(blank=True, default=None, max_length=255, null=True)),
('postal_code', models.CharField(blank=True, default=None, max_length=255, null=True)),
('region', models.CharField(blank=True, default=None, max_length=255, null=True)),
('time_zone', models.CharField(blank=True, default=None, max_length=255, null=True)),
('latitude', models.FloatField(blank=True, default=0, null=True)),
('longitude', models.FloatField(blank=True, default=0, null=True)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='clients.customer')),
],
),
]
18 changes: 18 additions & 0 deletions analytics/migrations/0002_alter_visitor_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.0.7 on 2023-09-12 06:55

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('analytics', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='visitor',
name='user',
field=models.CharField(blank=True, max_length=255, null=True),
),
]
19 changes: 19 additions & 0 deletions analytics/migrations/0003_visitor_timestamp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.0.7 on 2023-09-12 08:12

import datetime
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('analytics', '0002_alter_visitor_user'),
]

operations = [
migrations.AddField(
model_name='visitor',
name='timestamp',
field=models.DateTimeField(default=datetime.datetime(2023, 9, 12, 13, 42, 21, 101302)),
),
]
19 changes: 19 additions & 0 deletions analytics/migrations/0004_alter_visitor_timestamp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.0.7 on 2023-09-29 12:56

import datetime
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('analytics', '0003_visitor_timestamp'),
]

operations = [
migrations.AlterField(
model_name='visitor',
name='timestamp',
field=models.DateTimeField(default=datetime.datetime(2023, 9, 29, 18, 26, 52, 142594)),
),
]
Empty file.
34 changes: 34 additions & 0 deletions analytics/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from django.db import models
import uuid
import datetime

class Visitor(models.Model):
uuid = models.UUIDField(default=uuid.uuid4, editable=False,unique=True)
user = models.CharField(null=True,blank=True,max_length=255)
count = models.PositiveIntegerField(default=0,null=True,blank=True)
url = models.URLField(null=True,blank=True)
title = models.CharField(max_length=255,blank=True,null=True)
duration = models.CharField(max_length=255,blank=True,null=True)
devicetype = models.CharField(max_length=255,blank=True,null=True)
useragent = models.TextField(default="",blank=True,null=True)
ipaddress = models.CharField(max_length=255,null=True,blank=True)
timestamp = models.DateTimeField(default=datetime.datetime.now())
is_geolocation = models.BooleanField(default=True)
is_active = models.BooleanField(default=True)
city = models.CharField(max_length=255,default=None, null=True,blank=True)
continent_code = models.CharField(max_length=255,default=None, null=True,blank=True)
continent_name = models.CharField(max_length=255,default=None, null=True,blank=True)
country_code = models.CharField(max_length=255,default=None, null=True,blank=True)
country_name = models.CharField(max_length=255,default=None, null=True,blank=True)
dma_code = models.CharField(max_length=255,default=None, null=True,blank=True)
is_in_european_union = models.CharField(max_length=255,default=None, null=True,blank=True)
postal_code = models.CharField(max_length=255,default=None, null=True,blank=True)
region = models.CharField(max_length=255,default=None, null=True,blank=True)
time_zone = models.CharField(max_length=255,default=None, null=True,blank=True)
latitude = models.FloatField(default=0,blank=True,null=True)
longitude = models.FloatField(default=0,blank=True,null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

def __str__(self):
return f"{self.city} - {self.continent_name} - {self.title}"
3 changes: 3 additions & 0 deletions analytics/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
10 changes: 10 additions & 0 deletions analytics/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.urls import path
from .views import *

urlpatterns = [
path('get-ip-geolocation', get_user_ip_geolocation, name='analytics_ip_geolocation'),
path('track-page-view', track_page_view, name='analytics_v1'),
path('get-ip-address', get_user_ip_address, name='analytics_ip_address'),
path('get-country_graph1', get_country_graph, name='analytics_country_graph1'),
path('get-country_graph2', get_country, name='analytics_country_graph2'),
]
110 changes: 110 additions & 0 deletions analytics/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
from django.shortcuts import render
from django.utils.dateparse import parse_datetime
from rest_framework.decorators import api_view
from rest_framework.response import Response
from django.contrib.gis.geoip2 import GeoIP2
from ipware import get_client_ip
from .models import Visitor
import uuid
from django.db.models import Count
import datetime


@api_view(['GET'])
def get_user_ip_address(request):
client_ip, re_te = get_client_ip(request)
print(client_ip,re_te)
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip_address = x_forwarded_for.split(',')[0]
else:
ip_address = request.META.get('REMOTE_ADDR')
return Response({'ip':ip_address})

def get_ip_geolocation(ip_address):
g = GeoIP2()
try:
geolocation = g.city(ip_address)
return geolocation
except Exception as e:
print(f'Error retrieving geolocation: {e}')
return False
return None

@api_view(['GET','POST'])
def get_user_ip_geolocation(request):
if request.method == 'POST':
ip_address = request.data['ip_address']
else:
ip_address = '106.216.230.181'
geolocation = get_ip_geolocation(ip_address)
return Response({"message": geolocation})

@api_view(['GET', 'POST'])
def track_page_view(request):
if request.method == 'POST':
data = request.data
geodata = data.get('geoLocation')
if not geodata:
response = Response({"message": "not found"})
return response
visit = Visitor.objects.create(
user = data.get('user',request.user),
url = data.get('url',None),
title = data.get('title',None),
duration = data.get('duration',0),
timestamp = parse_datetime(data.get('timestamp',None)),
devicetype = data.get('deviceType',None),
useragent = data.get('userAgent',None),
ipaddress = data.get('ipAddress',None),
city = geodata['city'],
continent_code = geodata['continent_code'],
continent_name = geodata['continent_name'],
country_code = geodata['country_code'],
country_name = geodata['country_name'],
dma_code = geodata['dma_code'],
is_in_european_union = geodata['is_in_european_union'],
postal_code = geodata['postal_code'],
region = geodata['region'],
time_zone = geodata['time_zone'],
latitude = geodata['latitude'],
longitude = geodata['longitude']
)
visit.save()
response = Response({"message": visit.uuid})
return response
return Response({"message": "save loc analytics"})

@api_view(['GET'])
def get_country_graph(request):
today_date = datetime.date.today()
today_date_1 = datetime.date.today()+datetime.timedelta(days=1)
last_months_ago =today_date - datetime.timedelta(days=30)
data = Visitor.objects.filter(timestamp__gte=last_months_ago, timestamp__lte=today_date_1)
print(data)
count_data = Visitor.objects.filter(timestamp__gte=last_months_ago, timestamp__lte=today_date_1).values('city').annotate(count=Count('city'))
geoData = []
for i in data:
# if i.city == None:
# continue
i_count = 1
for entry in count_data:
if entry['city'] == i.city:
i_count = entry['count']
break
geoData.append({'city':i.city,
"country":i.country_name,
"country_code":i.country_code,
'visitors': i_count,
'lat': i.latitude,
'lng': i.longitude
})
return Response(list(geoData))

@api_view(['GET'])
def get_country(request):
today_date = datetime.date.today()
last_months_ago =today_date - datetime.timedelta(days=30)
today_date_1 = datetime.date.today()+datetime.timedelta(days=1)
count_data = Visitor.objects.filter(timestamp__gte=last_months_ago, timestamp__lte=today_date_1).values('country_name').annotate(count=Count('country_name'))
return Response(count_data)
Loading