Skip to content

Commit

Permalink
Merge pull request #15 from MohamedHamed12/visit
Browse files Browse the repository at this point in the history
edit attachments
  • Loading branch information
MohamedHamed12 authored May 11, 2024
2 parents d403afc + e54a15b commit c016733
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 17 deletions.
2 changes: 2 additions & 0 deletions project/visit/filters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class AttachmentFilter(filters.FilterSet):
class Meta:
model = Attachment
fields = {
'user': ['exact'],
'visit__patient': ['exact'],
'visit': ['exact'],
'kind': ['exact'],
'created_at': ['year', 'month', 'day']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Generated by Django 5.0.3 on 2024-05-11 16:46

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


class Migration(migrations.Migration):

dependencies = [
("visit", "0001_initial"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.AddField(
model_name="attachment",
name="file_name",
field=models.CharField(blank=True, max_length=255, null=True),
),
migrations.AddField(
model_name="attachment",
name="file_type",
field=models.CharField(blank=True, max_length=255, null=True),
),
migrations.AddField(
model_name="attachment",
name="user",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="user_attachments",
to=settings.AUTH_USER_MODEL,
),
),
migrations.AlterField(
model_name="attachment",
name="visit",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="visit_attachments",
to="visit.visit",
),
),
]
29 changes: 14 additions & 15 deletions project/visit/models/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.db import models

from django.contrib.auth.models import User
# Create your models here.
class Visit(models.Model):
id = models.AutoField(primary_key=True)
Expand All @@ -20,23 +20,22 @@ class Visit(models.Model):
def save(self, *args, **kwargs):
self.visit_number = Visit.objects.count()+1
super(Visit, self).save(*args, **kwargs)
# class Measurement(models.Model):
# visit = models.ForeignKey('visit.Visit', on_delete=models.CASCADE)
# height = models.CharField(max_length=255)
# weight = models.CharField(max_length=255)
# blood_pressure = models.CharField(max_length=255)
# temperature = models.CharField(max_length=255)
# pulse = models.CharField(max_length=255)
# oxygen_level = models.CharField(max_length=255)
# created_at = models.DateTimeField(auto_now_add=True)
# updated_at = models.DateTimeField(auto_now=True)
# is_deleted = models.BooleanField(default=False)


class Attachment(models.Model):
visit = models.ForeignKey('visit.Visit', on_delete=models.CASCADE)
file = models.FileField(upload_to='attachments')
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_attachments', null=True, blank=True)
visit = models.ForeignKey('visit.Visit', on_delete=models.CASCADE, related_name='visit_attachments', null=True, blank=True)
kind = models.CharField(max_length=255)

file_name = models.CharField(max_length=255, null=True, blank=True)
file_type = models.CharField(max_length=255, null=True, blank=True)
file = models.FileField(upload_to='attachments')

notes = models.TextField(blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
is_deleted = models.BooleanField(default=False)
is_deleted = models.BooleanField(default=False)
def save(self, *args, **kwargs):
self.file_type = self.file.name.split('.')[-1]
super(Attachment, self).save(*args, **kwargs)

3 changes: 2 additions & 1 deletion project/visit/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ class MeasurementSerializer(serializers.Serializer):
oxygen_level = serializers.CharField(max_length=255 ,required=False)

class AttachmentSerializer(serializers.ModelSerializer):
file_type=serializers.CharField(read_only=True)
class Meta:
model = Attachment
# fields = '__all__'
exclude = ['is_deleted']

class VisitSerializer(serializers.ModelSerializer):
measurement = MeasurementSerializer( required=False)
attachment = AttachmentSerializer( read_only=True, many=True,source='attachments')
attachment = AttachmentSerializer( read_only=True, many=True,source='visit_attachments', required=False)
class Meta:
model = Visit
# fields = '__all__'
Expand Down
59 changes: 59 additions & 0 deletions project/visit/tests/test_attachment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from django.test import TestCase
from visit.models import Attachment
from django.core.files.uploadedfile import SimpleUploadedFile
from django.urls import reverse
from unittest.mock import patch, MagicMock

class AttachmentAPITestCase(TestCase):

def setUp(self):
self.attachment_data = {'file': SimpleUploadedFile("test.txt", b"file_content"), 'notes': "Test notes"}

@patch('visit.views.AttachmentViewSet')
def test_attachment_api_list(self, MockAttachment):
mock_attachment = MagicMock()
mock_attachment.file.name = 'attachments/test.txt'
MockAttachment.objects.all.return_value = [mock_attachment]

response = self.client.get(reverse('attachment-list'))
self.assertEqual(response.status_code, 200)
# self.assertContains(response, 'attachments/test.txt')

@patch('visit.views.AttachmentViewSet')
def test_attachment_api_detail(self, MockAttachment):
mock_attachment = MagicMock()
mock_attachment.file.name = 'attachments/test.txt'
mock_attachment.notes = 'Test notes'
MockAttachment.objects.get.return_value = mock_attachment


# response = self.client.get(reverse('attachment-detail', kwargs={'pk': 1}))
# self.assertEqual(response.status_code, 200)
# # self.assertContains(response, 'attachments/test.txt')
# self.assertContains(response, 'Test notes')

# @patch('visit.views.AttachmentViewSet')
# def test_attachment_api_create(self, MockAttachment):
# MockAttachment.objects.create.return_value = MagicMock()

# response = self.client.post(reverse('attachment-list'), self.attachment_data, format='multipart')
# self.assertEqual(response.status_code, 201)
# self.assertTrue(MockAttachment.objects.create.called)

# @patch('myapp.views.AttachmentViewSet')
# def test_attachment_api_update(self, MockAttachment):
# mock_attachment_instance = MagicMock()
# MockAttachment.objects.get.return_value = mock_attachment_instance

# response = self.client.put(reverse('attachment-detail', kwargs={'pk': 1}), self.attachment_data, format='multipart')
# self.assertEqual(response.status_code, 200)
# mock_attachment_instance.save.assert_called_once()

# @patch('myapp.views.AttachmentViewSet')
# def test_attachment_api_delete(self, MockAttachment):
# mock_attachment_instance = MagicMock()
# MockAttachment.objects.get.return_value = mock_attachment_instance

# response = self.client.delete(reverse('attachment-detail', kwargs={'pk': 1}))
# self.assertEqual(response.status_code, 204)
# mock_attachment_instance.delete.assert_called_once()
7 changes: 6 additions & 1 deletion project/visit/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ class AttachmentViewSet(viewsets.ModelViewSet):
queryset = Attachment.objects.all()
serializer_class = AttachmentSerializer
pagination_class = CustomPagination
permission_classes=[RelatedVisitPermission]

# def get_queryset(self):
# if self.request.user.is_superuser:
# return Attachment.objects.all()
# else:
# return Attachment.objects.filter(user=self.request.user)
filter_backends = [
DjangoFilterBackend,
rest_filters.SearchFilter,
Expand Down

0 comments on commit c016733

Please # to comment.