Hi Everyone!
I am working on a project that is supposed to allow users to transfer files 
amongst each other. A transferred file can only be viewed by the user to 
whom the file has been transferred or an is_staff account.

*My Model*
class File(models.Model):
TYPES = (
('GENERAL', 'General'),
('PERSONAL', 'Personal'),
)
type = models.CharField(max_length=10, choices=TYPES, default='Personal')
no = models.CharField(max_length=250, unique=True, null=True)
name = models.CharField(max_length=250, unique=True, null=True)
created = models.DateTimeField(auto_now_add=True, null=True)
updated = models.DateTimeField(auto_now=True)
    class Meta:
        ordering = ('-created',)

    def __str__(self):
        return f'{self.no}'

    def get_absolute_url(self):
        return reverse('outgoing_file', args=[self.pk])

class Transfer(models.Model):
    STATES = (
        ('OUTGOING', 'Outgoing'),
        ('INCOMING', 'Incoming'),
    )
    file = models.ForeignKey('File', on_delete=models.CASCADE, 
related_name='transfers')
    state = models.CharField(max_length=10, choices=STATES, 
default='Outgoing')
    file_from = models.ForeignKey(User, on_delete=models.SET_NULL, 
null=True, related_name='transfers_from')
    file_to = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, 
related_name='transfers_to')
    recipient_name = models.CharField(max_length=250)
    incoming_date = models.DateTimeField(auto_now=True)
    due_date = models.DateTimeField(default=due_date(), null=True)
    updated = models.DateTimeField(auto_now=True)
    def __str__(self):
        return f'{self.file_from} {self.file_to}'

*My Views*
@login_required
def outgoing_file_list(request):
    if request.user.is_staff:
        files = File.objects.all()
    else:
        files = File.objects.filter(transfers__file_to_id=request.user.id)

    context = {
        'files': files,
    }
*NOTE:*
When the same file is transferred to a user multiple times, I want the 
query to return the newest item only. That to me will make the file 
accessible per user. Any recommended approach to achieve this is welcome.

*My Template*
*<tr>*
*                            <th>#</th>*
*                            <th>File Type</th>*
*                            <th>File Number</th>*
*                            <th>File Name</th>*
*                            <th>Recipient Name</th>*
*                            <th>Office Number</th>*
*                            <th>Transferred To</th>*
*                            <th>Outgoing Date</th>*
*                            <th>Due Date</th>*
*                            <th>Actions</th>*
*                        </tr>*
*                        </thead>*
*                        <tbody>*
*                        {% for file in files %}*
*                            <tr>*
*                                <td>{{ forloop.counter }}</td>*
*                                <td>{{ file.type }}</td>*
*                                <td>{{ file.no }}</td>*
*                                <td>{{ file.name | title  }}</td>*
*                                <td>{{ file.transfers.recipient_name 
}}</td>*
*                                <td>{{ file.transfers.recipient_office 
}}</td>*
*                                <td>{{ file.transfer_to.get_full_name 
}}</td>*
*                                <td>{{ file.outgoing_date.date }}</td>*
*                                <td>{{ file.due_date | timeuntil }}</td>*
*                                <td>*
*                                    {% if request.user.is_staff %}*
*                                        <div class="btn-group" 
role="group" aria-label="Basic outlined example">*
*                                            <a class="btn 
btn-outline-primary btn-sm"*
*                                               href="#"*
*                                               role="button">Receive*
*                                            </a>*
*                                        </div>*
*                                    {% endif %}*

*                                    <div class="btn-group" role="group" 
aria-label="Basic outlined example">*
*                                        <a class="btn btn-outline-primary 
btn-sm"*
*                                           href="#"*
*                                           role="button">Transfer*
*                                        </a>*
*                                    </div>*
*                                </td>*
*                            </tr>*
*                        {% endfor %}*

*NOTE: All related fields in the table do not show ie.*
*file.transfers.recipient_name*
*file.transfers.recipient_office*
*file.transfer_to.get_full_name*
*and the remaining.*

Thanks.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/801238dc-3aff-4bd6-8863-726eb024a163n%40googlegroups.com.

Reply via email to