model.py
из django .db импорт моделей
из django .utils импорт часовых поясов
Создайте здесь свои модели.
класс PageOnHold (models.Model):
timestamp = models.DateTimeField(max_length=None,blank=True, null=True,default=timezone.now)
spent_time = models.IntegerField(max_length=None,blank=True, null=True)
elapsed_time = models.DateTimeField(max_length=None,blank=True, null=True,default=timezone.now)
класс MouseClick (models.Model):
timestamp = models.DateTimeField(blank=True, null=True,default=timezone.now)
x_cord = models.IntegerField(max_length=None,blank=True, null=True)
y_cord = models.IntegerField(max_length=None,blank=True, null=True)
класс MouseOver (models.Model):
timestamp = models.DateTimeField(blank=True, null=True,default=timezone.now)
x_cord = models.IntegerField(max_length=None,blank=True, null=True)
y_cord = models.IntegerField(max_length=None,blank=True, null=True)
класс InteractionInfo (models.Model):
page_in_time = models.DateTimeField(blank=True, null=True,default=timezone.now)
page_on_hold = models.ForeignKey('PageOnHold',on_delete=models.CASCADE)
page_out_time = models.DateTimeField(blank=True, null=True,default=timezone.now)
estimated_time_spent = models.DateTimeField(max_length=None,blank=True, null=True,default=timezone.now)
mouse_click = models.ForeignKey('MouseClick',on_delete= models.CASCADE)
mouse_over = models.ForeignKey('MouseOver',on_delete= models.CASCADE)
класс NetworkInfo (models.Model):
city = models.CharField(max_length=100)
country = models.CharField(max_length=100)
hostname = models.CharField(max_length=100)
latitude = models.IntegerField(max_length=None)
longitude = models.IntegerField(max_length=None)
org = models.CharField(max_length=100)
postal = models.IntegerField(max_length=None)
region = models.CharField(max_length=100)
timezone = models.DateTimeField(max_length=100,default=timezone.now)
def __str__(self):
return self.city
класс BrowserInfo (models.Model):
app_code = models.CharField(max_length=100)
app_name = models.CharField(max_length=100)
app_version = models.CharField(max_length=100)
cookie_enabled = models.BooleanField(default=False)
language = models.CharField(max_length=100)
online = models.BooleanField(default=False)
platform = models.CharField(max_length=100)
# plugins = models.ForeignKey(plugins) # another table should be implement
user_agent = models.CharField(max_length=100)
has_err = models.BooleanField(default=False)
def __str__(self):
return self.app_code
Класс GeoLocation (models.Model):
timestamp = models.DateTimeField(null=True,blank=True,default=timezone.now)
coords = models.ForeignKey('Coords',on_delete=models.CASCADE)
hasErr = models.BooleanField(default=False)
errCode = models.CharField(max_length=100)
def __str__(self):
return self.errCode
Класс Coords (models.Model):
accuracy = models.IntegerField(max_length=None,blank=True, null=True,default='SOME STRING')
altitude = models.IntegerField(max_length=None,blank=True, null=True,default='SOME STRING')
altitudeAccuracy= models.CharField(max_length=100,blank=True, null=True,default='SOME STRING')
heading = models.CharField(max_length=100,blank=True, null=True,default='SOME STRING')
latitude = models.IntegerField(max_length=None,blank=True, null=True,default='SOME STRING')
longitude = models.IntegerField(max_length=None,blank=True, null=True,default='SOME STRING')
speed = models.IntegerField(max_length=None,blank=True, null=True,default='SOME STRING')
def __str__(self):
return self.heading
Класс UserLog (models.Model):
IP = models.IntegerField(unique=True,null= False,blank = False)
session_id = models.IntegerField(max_length=None,null=False)
time_in = models.DateTimeField(null=True, blank=True,default=timezone.now)
time_out = models.DateTimeField(null=True, blank=True,default=timezone.now)
interaction_info = models.ForeignKey('InteractionInfo',on_delete=models.CASCADE)
network_info = models.ForeignKey('NetworkInfo',on_delete=models.CASCADE)
browser_info = models.ForeignKey('BrowserInfo',on_delete=models.CASCADE)
geoLocation = models.ForeignKey('GeoLocation',on_delete=models.CASCADE)
timestamp =models.DateTimeField(null=True,blank=True,default=timezone.now)
screen_width = models.CharField(max_length=100)
url= models.CharField(max_length=100)
browserCookie = models.CharField(max_length=100)
def __str__(self):
return self.screen_width
serializer.py
из django .utils импорт часовых поясов
из rest_framework импорт сериализаторов
из .models import
UserLog, GeoLocation , BrowserInfo, Coords, InteractionInfo, MouseClick, MouseOver, PageOnHold, NetworkInfo
, класс CoordsSeializer (serializers.ModelSerializer):
accuracy = serializers.IntegerField()
altitude = serializers.IntegerField()
altitudeAccuracy= serializers.CharField()
heading = serializers.CharField()
latitude = serializers.IntegerField()
longitude = serializers.IntegerField()
speed = serializers.IntegerField()
class Meta:
model = Coords
fields = ('id','accuracy','altitude','altitudeAccuracy','heading','latitude','longitude','speed')
depth = 8
, класс GeoLocationSerializer (serializers.ModelSeriali) ноль):
coords = CoordsSeializer(many = False,read_only=True)
# timestamp = serializers.DateTimeField()
# coords = CoordsSeializer()
# hasErr = serializers.BooleanField()
# errCode = serializers.CharField()
class Meta:
model = GeoLocation
fields = ('id','timestamp','coords','hasErr','errCode')
depth = 4
класс BrowserInfoSerializer (serializers.ModelSerializer):
app_code = serializers.CharField()
app_name = serializers.CharField()
app_version = serializers.CharField()
cookie_enabled = serializers.BooleanField()
language = serializers.CharField()
online = serializers.BooleanField()
platform = serializers.CharField()
# plugins = models.ForeignKey(plugins) # another table should be implement
user_agent = serializers.CharField()
has_err = serializers.BooleanField()
class Meta:
model = BrowserInfo
fields = ('id','app_code','app_name','app_version','cookie_enabled','language','online','platform','user_agent','has_err')
depth = 10
класс NetworkInfoSerializer (serializers.ModelSerializer):
city = serializers.CharField()
country = serializers.CharField()
hostname = serializers.CharField()
latitude = serializers.IntegerField()
longitude = serializers.IntegerField()
org = serializers.CharField()
postal = serializers.IntegerField()
region = serializers.CharField()
timezone = serializers.DateTimeField()
class Meta:
model = NetworkInfo
fields = ('id','city','country','hostname','latitude','longitude','org','postal','region','timezone')
depth = 10
класс PageOnHoldserializer (сериализаторы) .ModelSerializer):
timestamp = serializers.DateTimeField()
spent_time = serializers.IntegerField()
elapsed_time = serializers.DateTimeField()
class Meta:
model = PageOnHold
fields = ('id','timestamp','spent_time','elapsed_time')
depth = 4
класс MouseClickserializer (serializers.ModelSerializer):
timestamp = serializers.DateTimeField()
x_cord = serializers.IntegerField()
y_cord = serializers.IntegerField()
class Meta:
model = MouseClick
fields = ('id','timestamp','x_cord','y_cord')
depth = 4
класс MouseOverserializer (serializers.ModelSerializer):
timestamp = serializers.DateTimeField()
x_cord = serializers.IntegerField()
y_cord = serializers.IntegerField()
class Meta:
model = MouseOver
fields = ('id','timestamp','x_cord','y_cord')
depth = 4
класс InteractionInfoSerializer ( serializers.ModelSerializer):
page_on_hold =PageOnHoldserializer(many=False,read_only=True)
mouse_click = MouseClickserializer(many=False,read_only=True)
mouse_over = MouseOverserializer(many=False,read_only=True)
class Meta:
model = InteractionInfo
fields = ('id','page_in_time','page_out_time','estimated_time_spent','page_on_hold','mouse_click','mouse_over')
depth= 4
класс UserLogSerializer (serializers.ModelSerializer):
interaction_info =InteractionInfoSerializer(many=False,read_only=True)
network_info = NetworkInfoSerializer(many=False,read_only=True)
browser_info = BrowserInfoSerializer(many=False,read_only=True)
geoLocation = GeoLocationSerializer(many=False,read_only=True)
class Meta:
model = UserLog
fields = ('id','IP','session_id','time_in','time_out','timestamp','screen_width','url','browserCookie','interaction_info','network_info','browser_info','geoLocation')
depth =9
views.py
из django .shortcuts импорт рендеринга
из django .http import HttpResponse
из django .shortcuts import get_list_or_404
из rest_framework.views import APIView
из rest_framework.response import Response
из rest_framework.decorators import api_view
из rest_framework статуса импорта
из .models import UserLog, GeoLocation, BrowserInf o, Coords, InteractionInfo, MouseClick, MouseOver, PageOnHold, NetworkInfo
из .serializers импортируют UserLogSerializer, GeoLocationSerializer, BrowserInfoSerializer, CoordsSeializer, InteractionInfoSerializer, MouseClickserializer * userOlizeSist ):
def get(self,request):
user = UserLog.objects.all()
serializers = UserLogSerializer(user,many=True)
return Response(serializers.data)
class userLogCreate (APIView):
def post(self,request):
serilizer = UserLogSerializer(data=request.data)
if serilizer.is_valid():
serilizer.save()
return Response(serilizer.data)
class userLogUpdate (APIView):
def post(self,request,pk):
user = UserLog.objects.get(id=pk)
serializer = UserLogSerializer(instance=user,data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)