• 
      

    Define the local site field for file attachments on creation.

    Review Request #15164 — Created July 11, 2026 and submitted

    Information

    Review Board
    release-8.x

    Reviewers

    Local sites for file attachments have a messy history. Currently, the
    local_site foreign key is only present for user file attachments
    (attachments that were uploaded into a comment box or from the user file
    attachments API), and for ones linked to file diffs (although our docs
    erroneously do not mention this). Attachments that are uploaded directly to
    a review request do not have this relation populated. Instead we grab the
    local site from their related review request. We have a get_local_site()
    method, which was originally added to help grab the right local site
    depending on the type of file attachment.

    We recently ran into a problem where we needed to grab a file attachment's
    local site before it has been linked to its review request. But we currently
    have no way of doing so.

    This change improves things by always setting the local_site field on
    file attachment creation. For existing pre-RB8.1 file attachments, calling
    get_local_site() will now set the local_site field for ones that
    don't have it set. We have a __defined_local_site key in the file
    attachment's extra data that lets us know that the local_site field value
    is authoritative. We need that because a local_site=None value is ambiguous
    on its own, it can mean that a file attachment doesn't have a local site, or
    that it's a pre-RB8.1 file attachment where we haven't resolved and set the
    local site from its review request yet.

    Did the following tests on my local RB 8 dev server and my local
    RBCommons dev server:
    - Ran unit tests.
    - Created a review request and uploaded a pdf file to it.
    - Created a user file attachment by dragging an image into a comment.
    - rbt posted a change containing binary files.
    - Viewed existing file attachments.

    Also used in upcoming changes that add local site support to
    document review.

    Summary ID
    Define the local site field for file attachments on creation.
    Local sites for file attachments have a messy history. Currently, the `local_site` foreign key is only present for user file attachments (attachments that were uploaded into a comment box or from the user file attachments API), and for ones linked to file diffs (although our docs erroneously do not mention this). Attachments that are uploaded directly to a review request do not have this relation populated. Instead we grab the local site from their related review request. We have a `get_local_site()` method, which was originally added to help grab the right local site depending on the type of file attachment. We recently ran into a problem where we needed to grab a file attachment's local site before it has been linked to its review request. But we currently have no way of doing so. This change improves things by always setting the `local_site` field on file attachment creation. For existing pre-RB8.1 file attachments, calling `get_local_site()` will now set the `local_site` field for ones that don't have it set. We have a `_defined_local_site` key in the file attachment's extra data that lets us know that the `local_site` field value is authoritative. We need that because a `local_site=None` value is ambiguous on its own, it can mean that a file attachment doesn't have a local site, or that it's a pre-RB8.1 file attachment where we haven't resolved and set the local site from its review request yet.
    2a39ed82d63a61463c047e98b950f1c01bd7ab1a
    Description From Last Updated

    'reviewboard.attachments.models.FileAttachment' imported but unused Column: 1 Error code: F401

    reviewbot reviewbot

    We probably should copy the dict before mutating it.

    david david

    There's an extra blank line here.

    david david

    Can we add a "Version Changed" detailing the new behavior here?

    david david

    I believe if you call this on a FileAttachment that hasn't yet been created, it will error out. We should …

    david david

    We probably should copy the dict before mutating it.

    david david

    We probably should copy the dict before mutating it.

    david david

    We use this literal across four different files. Can we define a constant somewhere?

    david david

    If we use a double __ prefix, that will filter it out of API responses and prevent API clients from …

    chipx86 chipx86

    If we check self.user_id instead, we'll save a query in the event that it's not already in the queryset cache.

    chipx86 chipx86

    We're a bit inconsistent in some places on how we capitalize, but I think we should be using "Local Site" …

    chipx86 chipx86

    Same note about double underscore.

    chipx86 chipx86

    There are a few additional uses of self.review_request in this method that can use this new variable instead.

    david david
    Checks run (1 failed, 1 succeeded)
    flake8 failed.
    JSHint passed.

    flake8

    maubin
    david
    1. 
        
    2. reviewboard/attachments/managers.py (Diff revision 2)
       
       
       
       
       
       
       
       
       
      Show all issues

      We probably should copy the dict before mutating it.

    3. reviewboard/attachments/managers.py (Diff revision 2)
       
       
      Show all issues

      There's an extra blank line here.

    4. reviewboard/attachments/models.py (Diff revision 2)
       
       
      Show all issues

      Can we add a "Version Changed" detailing the new behavior here?

    5. reviewboard/attachments/models.py (Diff revision 2)
       
       
      Show all issues

      I believe if you call this on a FileAttachment that hasn't yet been created, it will error out. We should probably check if the model is new, and if so, just hold off on saving.

    6. reviewboard/testing/testcase.py (Diff revision 2)
       
       
       
      Show all issues

      We probably should copy the dict before mutating it.

      1. Since this is the kwargs dict that gets built when the function is called and the caller doesn't have any reference to it, we don't need to copy it.

    7. reviewboard/testing/testcase.py (Diff revision 2)
       
       
       
       
       
      Show all issues

      We probably should copy the dict before mutating it.

    8. 
        
    maubin
    david
    1. Ship It!
    2. 
        
    chipx86
    1. 
        
    2. reviewboard/attachments/forms.py (Diff revision 3)
       
       
      Show all issues

      If we use a double __ prefix, that will filter it out of API responses and prevent API clients from overriding it.

    3. reviewboard/attachments/models.py (Diff revision 3)
       
       
      Show all issues

      If we check self.user_id instead, we'll save a query in the event that it's not already in the queryset cache.

    4. reviewboard/attachments/models.py (Diff revision 3)
       
       
       
       
      Show all issues

      We're a bit inconsistent in some places on how we capitalize, but I think we should be using "Local Site" for anything public in order to make it clear this is a Term and not, like, "your local website" or something.

    5. reviewboard/testing/testcase.py (Diff revision 3)
       
       
      Show all issues

      Same note about double underscore.

    6. 
        
    maubin
    david
    1. 
        
    2. reviewboard/attachments/forms.py (Diff revisions 3 - 4)
       
       
      Show all issues

      We use this literal across four different files. Can we define a constant somewhere?

    3. reviewboard/attachments/forms.py (Diff revision 4)
       
       
      Show all issues

      There are a few additional uses of self.review_request in this method that can use this new variable instead.

    4. 
        
    maubin
    david
    1. Ship It!
    2. 
        
    maubin
    Review request changed
    Status:
    Completed
    Change Summary:
    Pushed to release-8.x (2144f8b)