Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2023
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.20 KB | None | 0 0
  1. ## Models.py ##
  2. class MathPlacement(models.Model):
  3.     BLUE = "bg-primary-200"
  4.     GREEN = "bg-success-200"
  5.     YELLOW = "bg-warning-200"
  6.     RED = "bg-danger-200"
  7.     PLACEMENT_COLOR_CHOICES = [
  8.         (BLUE, "Blue"),
  9.         (GREEN, "Green"),
  10.         (YELLOW, "Yellow"),
  11.         (RED, "Red"),
  12.     ]
  13.     id = models.IntegerField(db_column="mathPlacementID", primary_key=True)
  14.     placement_data = models.OneToOneField(
  15.         MathPlacementData,
  16.         on_delete=models.PROTECT,
  17.         related_name="%(class)s_datas",
  18.         db_column="placement_person_id",
  19.         to_field="person",
  20.     )
  21.     color = models.CharField(
  22.         max_length=15, choices=PLACEMENT_COLOR_CHOICES, null=True, blank=True
  23.     )
  24.     class Meta:
  25.         verbose_name = "Placement"
  26.         verbose_name_plural = "Placements"
  27.         managed = getattr(settings, "UNDER_TEST", False)
  28.         db_table = "[schema_b].[mathPlacement]"
  29.  
  30. ## Views.py ##
  31. # MathPlacement ListView (Initial view to list out all placements)
  32. class MathPlacementListView(ListView):
  33.     model = MathPlacement
  34.     template_name = "math_placements/mathplacement_list.html"
  35.     context_object_name = "math_placements"
  36.     def get_queryset(self) -> QuerySet:
  37.         # logic to get placements
  38.         return placements
  39.     def get_context_data(self, **kwargs) -> dict:
  40.         context = super().get_context_data(**kwargs)
  41.         context["placement_colors"] = MathPlacement.PLACEMENT_COLOR_CHOICES
  42.         context["courses"] = MathPlacementCourse.objects.all()
  43.         return context
  44.  
  45. # MathPlacement UpdateView (handles the POST from htmx when a user selects a color)
  46. class MathPlacementUpdateView(UpdateView):
  47.     model = MathPlacement
  48.     fields = [
  49.         "color",
  50.     ]
  51.     template_name = "math_placements/snippets/mathplacement_update_form.html"
  52.     context_object_name = "placement"
  53.     http_method_names = ["post"]
  54.  
  55.     def get_success_url(self):
  56.         return reverse_lazy(
  57.             "math_placements:math-placement-detail", args=[str(self.object.pk)]
  58.         )
  59.  
  60.     def get_context_data(self, **kwargs):
  61.         context = super().get_context_data(**kwargs)
  62.         context["placement_colors"] = MathPlacement.PLACEMENT_COLOR_CHOICES
  63.         return context
  64.  
  65.     def form_invalid(self, form):
  66.         # TODO: handle errors in the form if they exist
  67.         return super().form_invalid(form)
  68.  
  69.     def form_valid(self, form):
  70.         return super().form_valid(form)
  71.  
  72. # MathPlacement DetailView (called to return the template of the element updated)
  73. class MathPlacementDetailView(DetailView):
  74.     model = MathPlacement
  75.     template_name = "math_placements/snippets/mathplacement_update_form.html"
  76.     context_object_name = "placement"
  77.     http_method_names = ["get"]
  78.  
  79.     def get_context_data(self, **kwargs) -> dict:
  80.         context = super().get_context_data(**kwargs)
  81.         context["courses"] = MathPlacementCourse.objects.all()
  82.         context["placement_colors"] = MathPlacement.PLACEMENT_COLOR_CHOICES
  83.         return context
  84.  
  85.  
  86.  
  87. ## mathplacement_update_form.html ##
  88. <form id="placementUpdateForm" action="{{ placement.get_update_url }}" method="post" hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'>
  89.     <!-- hidden inputs for the form submission -->
  90.     <input type="hidden" name="id" value="{{ placement.id }}">
  91.     <input type="hidden" name="placement_data" value="{{ placement.placement_data.pk }}">
  92.     <label for="color">Color</label>
  93.     <select name="color" id="color{{ placement.pk }}" class="form-select" hx-post="{% url 'math_placements:math-placement-update' placement.pk %}" hx-target="closest #placementUpdateForm" hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'>
  94.         <option value="" {% if not placement.color %}selected{% endif %}>
  95.             Default
  96.         </option>
  97.         {% for value, display_name in placement_colors %}
  98.             <option value="{{ value }}" {% if value == placement.color %}selected{% endif %}>{{ display_name }}</option>
  99.         {% endfor %}
  100.     </select>
  101. </form>
  102.  
  103. ## mathplacement_list.html ##
  104.  
  105. {% block content %}
  106.     {% for placement in math_placements %}
  107.         {% include 'math_placements/snippets/mathplacement_update_form.html' %}
  108.     {% endfor %}
  109. {% endblock %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement