Skip to content

Commit

Permalink
filter stool samples by user province; added in user profile
Browse files Browse the repository at this point in the history
  • Loading branch information
thapaliya19 committed Oct 9, 2023
1 parent 81fc6fe commit 60f9adc
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 16 deletions.
9 changes: 8 additions & 1 deletion sample/sample_views/stool_sample_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
CreateBreadcrumbMixin,
DetailBreadcrumbMixin,
)
from django.core.exceptions import PermissionDenied

from address.forms import AddressForm
from sample.const import IMAGE_COUNT, IMAGE_TYPE_CHOICES, SLIDE_COUNT
Expand Down Expand Up @@ -71,9 +72,15 @@ def get_queryset(self, **kwargs):
today_date = datetime.datetime.today().strftime('%Y-%m-%d')
self.end_date = self.request.GET.get('to', today_date)
queryset = queryset.filter(date_of_collection__range=[self.start_date, self.end_date])
self.province = self.request.GET.get('province', '')

self.province = self.request.GET.get('province', "")
user_provinces = self.request.user.profile.provinces.all()
if self.province:
if not user_provinces.filter(id=self.province):
raise PermissionDenied(f"No permission to view items from Province {self.province}")
queryset = queryset.filter(site__district__province__id=self.province)
else:
queryset = queryset.filter(site__district__province__id__in=user_provinces)
return queryset


Expand Down
34 changes: 19 additions & 15 deletions sample/templates/sample/sample_home.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,26 @@
{% with detail_url_name="sample:"|add:sample_type|add:"_detail" %}
<h3 class="text-center">{{ sample_type|title }} Samples</h3>

<div class="container">
{% with list_url="sample:"|add:sample_type|add:"_list" %}
<form action="{% url list_url %}" method="GET" >
{% endwith %}
<div class="row">
<div class="col">

</div>
<div class="col">
{{ filter_form }}
</div>
<div class="col">
<button type="submit" class="btn btn-primary">Filter</button>
<div class="accordion" id="filterAccordion">
<div class="accordion-item">
<h2 class="accordion-header" id="headingOne">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne"
aria-expanded="true" aria-controls="collapseOne">
Filter Samples
</button>
</h2>
<div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne"
data-bs-parent="#filterAccordion">
<div class="accordion-body">
{% with list_url="sample:"|add:sample_type|add:"_list" %}
<form class="row g-3 row-cols-lg-auto" action="{% url list_url %}" method="GET">
{% endwith %}
{{ filter_form }}
<button type="submit" class="btn btn-primary">Filter</button>
</form>
</div>
</div>
</form>
</div>
</div>

<div class="row">
Expand Down Expand Up @@ -120,4 +124,4 @@ <h5>No samples to show.</h5>

{% endwith %}
{% endwith %}
{% endblock %}
{% endblock %}
27 changes: 27 additions & 0 deletions user/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

from user.models import Profile


class ProfileInline(admin.StackedInline):
model = Profile
can_delete = False
verbose_name_plural = 'Profile'
fk_name = 'user'


class CustomUserAdmin(UserAdmin):
inlines = (ProfileInline, )

def get_inline_instances(self, request, obj=None):
if not obj:
return list()
return super(CustomUserAdmin, self).get_inline_instances(request, obj)


# Register your models here.
admin.site.register(Profile)
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
40 changes: 40 additions & 0 deletions user/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by Django 4.1.3 on 2023-10-09 16:56

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


class Migration(migrations.Migration):

initial = True

dependencies = [
("address", "0003_province_code"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="Profile",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("provinces", models.ManyToManyField(to="address.province")),
(
"user",
models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
),
]
19 changes: 19 additions & 0 deletions user/migrations/0002_alter_profile_provinces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.1.3 on 2023-10-09 17:14

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("address", "0003_province_code"),
("user", "0001_initial"),
]

operations = [
migrations.AlterField(
model_name="profile",
name="provinces",
field=models.ManyToManyField(blank=True, to="address.province"),
),
]
Empty file added user/migrations/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions user/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.db import models
from django.contrib.auth.models import User
from address.models import Province
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.core.exceptions import ObjectDoesNotExist


class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
provinces = models.ManyToManyField(Province, blank=True)


@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
try:
instance.profile.save()
except ObjectDoesNotExist:
Profile.objects.create(user=instance)


@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()

0 comments on commit 60f9adc

Please sign in to comment.