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

create dashboard from analysis #251

Merged
merged 8 commits into from
Jan 23, 2025
Merged
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
21 changes: 20 additions & 1 deletion django_project/analysis/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from django.contrib import admin
from django.contrib.gis.admin import OSMGeoAdmin

from .models import Analysis, InterventionArea, Landscape, LandscapeCommunity
from .models import (
Analysis,
InterventionArea,
Landscape,
LandscapeCommunity,
UserAnalysisResults
)


@admin.register(Analysis)
Expand Down Expand Up @@ -93,3 +99,16 @@ class LandscapeCommunityAdmin(OSMGeoAdmin):
list_filter = ('landscape',)

map_template = 'gis/admin/osm.html'


class UserAnalysisResultsAdmin(admin.ModelAdmin):
list_display = ('created_by', 'source', 'created_at',)
search_fields = ('created_by__username', 'source',)
list_filter = ('source',)

def view_analysis_results(self, obj):
return str(obj.analysis_results)[:100]
view_analysis_results.short_description = 'Analysis Results...'


admin.site.register(UserAnalysisResults, UserAnalysisResultsAdmin)
26 changes: 26 additions & 0 deletions django_project/analysis/migrations/0004_useranalysisresults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.2.15 on 2025-01-22 07:02

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('analysis', '0003_alter_landscape_options_landscape_project_name_and_more'),
]

operations = [
migrations.CreateModel(
name='UserAnalysisResults',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('analysis_results', models.JSONField()),
('created_at', models.DateTimeField(auto_now_add=True)),
('source', models.CharField(blank=True, max_length=255, null=True)),
('created_by', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.2.15 on 2025-01-22 07:34

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('analysis', '0004_useranalysisresults'),
]

operations = [
migrations.AlterField(
model_name='useranalysisresults',
name='analysis_results',
field=models.JSONField(blank=True, null=True),
),
migrations.AlterField(
model_name='useranalysisresults',
name='created_by',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
25 changes: 25 additions & 0 deletions django_project/analysis/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,28 @@ class Meta:
def __str__(self):
"""Return string representation of LandscapeArea."""
return self.community_name



class UserAnalysisResults(models.Model):
created_by = models.ForeignKey(
User,
on_delete=models.CASCADE,
null=True,
blank=True
)
analysis_results = models.JSONField(
null=True,
blank=True
)
created_at = models.DateTimeField(auto_now_add=True)
source = models.CharField(
max_length=255,
null=True,
blank=True
)

def __str__(self):
created_by = self.created_by.username if self.created_by else 'Unknown'
created_at = self.created_at
return f"Analysis by {created_by} on {created_at}"
14 changes: 14 additions & 0 deletions django_project/analysis/serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from rest_framework import serializers
from .models import UserAnalysisResults


class UserAnalysisResultsSerializer(serializers.ModelSerializer):
class Meta:
model = UserAnalysisResults
fields = [
'id',
'created_by',
'analysis_results',
'created_at',
'source'
]
10 changes: 10 additions & 0 deletions django_project/analysis/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import UserAnalysisResultsViewSet

router = DefaultRouter()
router.register(r'user_analysis_results', UserAnalysisResultsViewSet)

urlpatterns = [
path('', include(router.urls)),
]
32 changes: 30 additions & 2 deletions django_project/analysis/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
from django.shortcuts import render # noqa: F401
from rest_framework import viewsets
from .models import UserAnalysisResults
from .serializer import UserAnalysisResultsSerializer
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import action

# Create your views here.

class UserAnalysisResultsViewSet(viewsets.ModelViewSet):
queryset = UserAnalysisResults.objects.all()
serializer_class = UserAnalysisResultsSerializer
permission_classes = [IsAuthenticated]

def perform_create(self, serializer):
serializer.save(created_by=self.request.user)

@action(detail=False, methods=['get'])
def fetch_analysis_results(self, request):
analysis_results = UserAnalysisResults.objects.filter(
created_by=request.user
)
serializer = self.get_serializer(analysis_results, many=True)
return Response(serializer.data)

@action(detail=False, methods=['post'])
def save_analysis_results(self, request):
serializer = self.get_serializer(data=request.data)
if serializer.is_valid():
serializer.save(created_by=request.user)
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)
1 change: 1 addition & 0 deletions django_project/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
path('', include('support.urls')),
path('', include('cloud_native_gis.urls')),
path('', include('dashboard.urls')),
path('', include('analysis.urls')),
]

if settings.DEBUG:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.15 on 2025-01-22 12:56

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('analysis', '0005_alter_useranalysisresults_analysis_results_and_more'),
('dashboard', '0003_dashboard_created_by_dashboard_title_and_more'),
]

operations = [
migrations.AddField(
model_name='dashboard',
name='analysis_results',
field=models.ManyToManyField(blank=True, help_text='Analysis results associated with this dashboard.', related_name='dashboards', to='analysis.useranalysisresults'),
),
]
8 changes: 8 additions & 0 deletions django_project/dashboard/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.db import models

from base.models import Organisation
from analysis.models import UserAnalysisResults


class Dashboard(models.Model):
Expand Down Expand Up @@ -74,6 +75,13 @@ class Dashboard(models.Model):
help_text="Privacy level of the dashboard."
)

analysis_results = models.ManyToManyField(
UserAnalysisResults,
related_name="dashboards",
blank=True,
help_text="Analysis results associated with this dashboard."
)

created_at = models.DateTimeField(
auto_now_add=True,
help_text="The date and time when this dashboard was created."
Expand Down
Loading
Loading