Skip to content

Commit 4298881

Browse files
authored
Merge pull request #49 from aclark4life/main
Misc updates
2 parents c3efd75 + fb29758 commit 4298881

File tree

16 files changed

+339
-34
lines changed

16 files changed

+339
-34
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
django_mongodb_cli.egg-info/
2-
django_mongodb_cli/__pycache__/
2+
__pycache__
33
/src/
44
.idea
55
server.log

demo/__init__.py

Whitespace-only changes.

demo/medical_records/__init__.py

Whitespace-only changes.

demo/medical_records/admin.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.contrib import admin
2+
from .models import Patient
3+
4+
5+
@admin.register(Patient)
6+
class PatientAdmin(admin.ModelAdmin):
7+
pass

demo/medical_records/apps.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class MedicalRecordsConfig(AppConfig):
5+
default_auto_field = "django_mongodb_backend.fields.ObjectIdAutoField"
6+
name = "demo.medical_records"
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import os
2+
import random
3+
from django.core.management.base import BaseCommand
4+
from faker import Faker
5+
6+
from django_mongodb_demo.models import Patient, PatientRecord, Billing
7+
8+
9+
class Command(BaseCommand):
10+
help = "Create patients with embedded patient records and billing using Faker. Optionally set MONGODB_URI."
11+
12+
def add_arguments(self, parser):
13+
parser.add_argument(
14+
"num_patients", type=int, help="Number of patients to create"
15+
)
16+
parser.add_argument(
17+
"--flush",
18+
action="store_true",
19+
help="Delete all existing patients before creating new ones",
20+
)
21+
parser.add_argument(
22+
"--mongodb-uri",
23+
type=str,
24+
help="MongoDB connection URI to set as MONGODB_URI env var",
25+
)
26+
27+
def handle(self, *args, **options):
28+
fake = Faker()
29+
30+
num_patients = options["num_patients"]
31+
32+
# Set MONGODB_URI if provided
33+
if options.get("mongodb_uri"):
34+
os.environ["MONGODB_URI"] = options["mongodb_uri"]
35+
self.stdout.write(
36+
self.style.SUCCESS(f"MONGODB_URI set to: {options['mongodb_uri']}")
37+
)
38+
39+
# Optionally flush
40+
if options["flush"]:
41+
Patient.objects.all().delete()
42+
self.stdout.write(self.style.WARNING("Deleted all existing patients."))
43+
44+
for _ in range(num_patients):
45+
# Create a Billing object
46+
billing = Billing(
47+
cc_type=fake.credit_card_provider(), cc_number=fake.credit_card_number()
48+
)
49+
50+
# Create a PatientRecord object
51+
record = PatientRecord(
52+
ssn=fake.ssn(),
53+
billing=billing,
54+
bill_amount=round(random.uniform(50.0, 5000.0), 2),
55+
)
56+
57+
# Create Patient
58+
patient = Patient(
59+
patient_name=fake.name(),
60+
patient_id=random.randint(100000, 999999),
61+
patient_record=record,
62+
)
63+
patient.save()
64+
65+
self.stdout.write(
66+
self.style.SUCCESS(
67+
f"Created Patient: {patient.patient_name} ({patient.patient_id})"
68+
)
69+
)
70+
self.stdout.write(f" SSN: {record.ssn}")
71+
self.stdout.write(f" Billing CC Type: {billing.cc_type}")
72+
self.stdout.write(f" Billing CC Number: {billing.cc_number}")
73+
self.stdout.write(f" Bill Amount: ${record.bill_amount}")
74+
75+
self.stdout.write(
76+
self.style.SUCCESS(f"Successfully created {num_patients} patient(s).")
77+
)

demo/medical_records/models.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from django.db import models
2+
from django_mongodb_backend.models import EmbeddedModel
3+
from django_mongodb_backend.fields import (
4+
EmbeddedModelField,
5+
EncryptedEmbeddedModelField,
6+
EncryptedCharField,
7+
)
8+
9+
10+
class Patient(models.Model):
11+
patient_name = models.CharField(max_length=255)
12+
patient_id = models.BigIntegerField()
13+
patient_record = EmbeddedModelField("PatientRecord")
14+
15+
def __str__(self):
16+
return f"{self.patient_name} ({self.patient_id})"
17+
18+
19+
class PatientRecord(EmbeddedModel):
20+
ssn = EncryptedCharField(max_length=11)
21+
billing = EncryptedEmbeddedModelField("Billing")
22+
bill_amount = models.DecimalField(max_digits=10, decimal_places=2)
23+
24+
25+
class Billing(EmbeddedModel):
26+
cc_type = models.CharField(max_length=50)
27+
cc_number = models.CharField(max_length=20)

demo/medical_records/tests.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from django.test import TestCase
2+
from .models import Author, Article
3+
4+
5+
class DemoTest(TestCase):
6+
def test_create_author_and_article(self):
7+
author = Author.objects.create(name="Alice", email="alice@example.com")
8+
article = Article.objects.create(
9+
title="Hello MongoDB",
10+
slug="hello-mongodb",
11+
author=author,
12+
content="Testing MongoDB backend.",
13+
)
14+
self.assertEqual(article.author.name, "Alice")

demo/medical_records/urls.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.urls import path
2+
from . import views
3+
4+
urlpatterns = [
5+
path("", views.article_list, name="article_list"),
6+
path("article/<slug:slug>/", views.article_detail, name="article_detail"),
7+
]

demo/medical_records/views.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from django.shortcuts import render, get_object_or_404
2+
from .models import Article
3+
4+
5+
def article_list(request):
6+
articles = Article.objects.all().order_by("-published_at")
7+
return render(request, "demo/article_list.html", {"articles": articles})
8+
9+
10+
def article_detail(request, slug):
11+
article = get_object_or_404(Article, slug=slug)
12+
comments = article.comments.all().order_by("-created_at")
13+
return render(
14+
request, "demo/article_detail.html", {"article": article, "comments": comments}
15+
)

0 commit comments

Comments
 (0)