Skip to content

Commit

Permalink
Add Plug Config API (#2574)
Browse files Browse the repository at this point in the history
Co-authored-by: Aakash Singh <[email protected]>
  • Loading branch information
vigneshhari and sainak authored Oct 30, 2024
1 parent 0a4b338 commit fa80b2c
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 3 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-python@v3

- uses: actions/setup-python@v5
with:
python-version: "3.13"

- uses: pre-commit/[email protected]
with:
extra_args: --color=always --from-ref ${{ github.event.pull_request.base.sha }} --to-ref ${{ github.event.pull_request.head.sha }}
9 changes: 9 additions & 0 deletions care/users/api/serializers/plug_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rest_framework import serializers

from care.users.models import PlugConfig


class PLugConfigSerializer(serializers.ModelSerializer):
class Meta:
model = PlugConfig
exclude = ("id",)
44 changes: 44 additions & 0 deletions care/users/api/viewsets/plug_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from django.core.cache import cache
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet, ModelViewSet

from care.users.api.serializers.plug_config import PLugConfigSerializer
from care.users.models import PlugConfig


class PlugConfigViewset(
ModelViewSet,
GenericViewSet,
):
lookup_field = "slug"
serializer_class = PLugConfigSerializer
queryset = PlugConfig.objects.all().order_by("slug")
cache_key = "care_plug_viewset_list"
authentication_classes = []

def list(self, request, *args, **kwargs):
# Cache data and return
response = cache.get(self.cache_key)
if not response:
serializer = self.get_serializer(self.queryset, many=True)
response = serializer.data
cache.set(self.cache_key, response)
return Response({"configs": [response]})

def perform_create(self, serializer):
cache.delete(self.cache_key)
serializer.save()

def perform_update(self, serializer):
cache.delete(self.cache_key)
serializer.save()

def perform_destroy(self, instance):
cache.delete(self.cache_key)
instance.delete()

def get_permissions(self):
if self.action in ["list", "retrieve"]:
return []
return [IsAdminUser()]
21 changes: 21 additions & 0 deletions care/users/migrations/0020_plugconfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 5.1.1 on 2024-10-29 19:34

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('users', '0019_rename_doctor_qualification_user_qualification'),
]

operations = [
migrations.CreateModel(
name='PlugConfig',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('slug', models.CharField(max_length=255, unique=True)),
('meta', models.JSONField(default=dict)),
],
),
]
8 changes: 8 additions & 0 deletions care/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,14 @@ def __str__(self):
return self.facility.name


class PlugConfig(models.Model):
slug = models.CharField(max_length=255, unique=True)
meta = models.JSONField(default=dict)

def __str__(self):
return self.slug


class UserFlag(BaseFlag):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=False, blank=False)

Expand Down
4 changes: 4 additions & 0 deletions config/api_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,17 @@
StateViewSet,
WardViewSet,
)
from care.users.api.viewsets.plug_config import PlugConfigViewset
from care.users.api.viewsets.skill import SkillViewSet
from care.users.api.viewsets.users import UserViewSet
from care.users.api.viewsets.userskill import UserSkillViewSet

router = DefaultRouter() if settings.DEBUG else SimpleRouter()

router.register("users", UserViewSet, basename="users")

router.register("plug_config", PlugConfigViewset, basename="plug_configs")

user_nested_router = NestedSimpleRouter(router, r"users", lookup="users")
user_nested_router.register("skill", UserSkillViewSet, basename="users-skill")

Expand Down
2 changes: 1 addition & 1 deletion docker/dev.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ RUN ARCH=$(dpkg --print-architecture) && \

# use pipenv to manage virtualenv
RUN python -m venv /venv
RUN pip install pipenv
RUN pip install pipenv==2024.2.0

COPY Pipfile Pipfile.lock ./
RUN pipenv install --system --categories "packages dev-packages"
Expand Down
2 changes: 1 addition & 1 deletion docker/prod.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ RUN ARCH=$(dpkg --print-architecture) && \

# use pipenv to manage virtualenv
RUN python -m venv /venv
RUN pip install pipenv
RUN pip install pipenv==2024.2.0

COPY Pipfile Pipfile.lock $APP_HOME
RUN pipenv sync --system --categories "packages"
Expand Down

0 comments on commit fa80b2c

Please sign in to comment.