Changes between Initial Version and Version 1 of NullMediaItem


Ignore:
Timestamp:
09/25/25 16:01:18 (4 months ago)
Author:
bazzuser
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • NullMediaItem

    v1 v1  
     1**Purpose of NullMediaItem**
     2
     3The NullMediaItem class represents the absence of media (image, audio, or video) in a way that avoids special-case handling elsewhere in the code.
     4
     5It follows the Null Object Pattern
     6, which provides a default behavior for "no object" cases, eliminating the need for repetitive if media is not None or if media: checks in templates or logic.
     7
     8**Why use it?**
     9
     10Without NullMediaItem:
     11
     12
     13{{{#!python
     14if question.media:
     15    for m in question.media:
     16        if m.type == "image":
     17            ...
     18}}}
     19
     20
     21
     22With NullMediaItem:
     23
     24
     25{{{#!python
     26for m in question.media:
     27    html += m.to_html()
     28}}}
     29
     30
     31
     32→ no conditional logic needed – every MediaItem, even the null variant, understands how to behave.
     33
     34**Behavior of NullMediaItem**
     35|| Method ||    Behavior ||
     36||type() ||     Returns "none" ||
     37|| to_dict() || Returns empty {} or None ||
     38|| to_html() || Returns "" (empty string) ||
     39
     40**When is it used?**
     41
     42You can attach one or more NullMediaItem()s to a Question instead of using None, [], or omitting the field entirely.
     43
     44For example:
     45
     46
     47{{{|!python
     48question = TextQuestion(
     49    qid="q1",
     50    text="What is your name?",
     51    media=[NullMediaItem()]
     52)
     53}}}
     54
     55This way, the rendering pipeline stays uniform, regardless of whether media is present or not.
     56
     57**Benefits**
     58
     59- Simplifies templates and rendering logic
     60
     61- Eliminates null checks in UI code
     62
     63- Makes polymorphism clean and consistent
     64
     65- Encourages Tell, Don't Ask design (don’t ask “do you have media?”, just tell media to render itself)