-
Notifications
You must be signed in to change notification settings - Fork 6
Complete Allow the user to delete the rules file from the downloads #1219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
7d24e3f
665e652
6523768
c440c12
19739d9
b3e0d70
8ce55f2
f20ff17
67f17fb
cdd8229
16686a0
924432e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # Generated by Django 5.2.1 on 2025-10-09 12:25 | ||
|
|
||
| import django.db.models.deletion | ||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("files", "0001_initial"), | ||
| migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name="filedownload", | ||
| name="deleted_at", | ||
| field=models.DateTimeField(blank=True, null=True), | ||
| ), | ||
| migrations.AddField( | ||
| model_name="filedownload", | ||
| name="deleted_by", | ||
| field=models.ForeignKey( | ||
| blank=True, | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| related_name="deleted_files", | ||
| to=settings.AUTH_USER_MODEL, | ||
| ), | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="filedownload", | ||
| name="id", | ||
| field=models.BigAutoField( | ||
| auto_created=True, primary_key=True, serialize=False, verbose_name="ID" | ||
| ), | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="filetype", | ||
| name="id", | ||
| field=models.BigAutoField( | ||
| auto_created=True, primary_key=True, serialize=False, verbose_name="ID" | ||
| ), | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,8 @@ class FileDownload(BaseModel): | |
| user (User, optional): The user who generated the file. Defaults to None. | ||
| file_type (FileType): The type of the file. | ||
| file_url (str, optional): The URL for downloading the file. Defaults to None. | ||
| deleted_at (datetime, optional): Timestamp when the file was deleted. Defaults to None. | ||
| deleted_by (User, optional): The user who deleted the file. Defaults to None. | ||
|
|
||
| Returns: | ||
| str: The name of the file. | ||
|
|
@@ -55,6 +57,14 @@ class FileDownload(BaseModel): | |
| ) | ||
| file_type = models.ForeignKey(FileType, on_delete=models.CASCADE) | ||
| file_url = models.CharField(max_length=500, null=True, blank=True) | ||
| deleted_at = models.DateTimeField(null=True, blank=True) | ||
| deleted_by = models.ForeignKey( | ||
| settings.AUTH_USER_MODEL, | ||
| on_delete=models.SET_NULL, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to decide the behaviour when the associated user is deleted: either keeping the file for record (as you did here) or deleting the associated files as well (as @AndyRae did in line 54)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think the above is about the case when the associated users are deleted. Anyway, we can come back to this later when Andy comes back (I don't think this is a blocker for other PRs), or you can decide as the PR author. I just want the deleting behaviour to be consistent. |
||
| blank=True, | ||
| null=True, | ||
| related_name="deleted_files", | ||
| ) | ||
|
|
||
| def __str__(self): | ||
| return str(self.name) | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| "use client"; | ||
|
|
||
| import { useState, useMemo } from "react"; | ||
| import { ColumnDef } from "@tanstack/react-table"; | ||
| import { DataTable } from "@/components/data-table"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { Eye, EyeOff } from "lucide-react"; | ||
| import { FILE_RETENTION_DAYS } from "@/constants/config"; | ||
|
|
||
| interface FileDownloadsTableProps { | ||
| data: FileDownload[]; | ||
| count: number; | ||
| columns: ColumnDef<FileDownload>[]; | ||
| Filter?: JSX.Element; | ||
| defaultPageSize: 10 | 20 | 30 | 40 | 50; | ||
| } | ||
|
|
||
| export function FileDownloadsTable({ | ||
| data, | ||
| count, | ||
| columns, | ||
| Filter, | ||
| defaultPageSize | ||
| }: FileDownloadsTableProps) { | ||
| const [showOldFiles, setShowOldFiles] = useState(false); | ||
|
|
||
| const cutoffDate = useMemo(() => { | ||
| const millisecondsPerDay = 24 * 60 * 60 * 1000; | ||
| return new Date(Date.now() - FILE_RETENTION_DAYS * millisecondsPerDay); | ||
| }, []); | ||
|
|
||
| const { recentFiles, oldFiles } = useMemo(() => { | ||
| const recent: FileDownload[] = []; | ||
| const old: FileDownload[] = []; | ||
|
|
||
| data.forEach((file) => { | ||
| const fileDate = new Date(file.created_at); | ||
| if (fileDate >= cutoffDate) { | ||
| recent.push(file); | ||
| } else { | ||
| old.push(file); | ||
| } | ||
| }); | ||
|
|
||
| return { recentFiles: recent, oldFiles: old }; | ||
| }, [data, cutoffDate]); | ||
|
|
||
| const displayedFiles = showOldFiles ? data : recentFiles; | ||
| const displayedCount = showOldFiles ? count : recentFiles.length; | ||
|
|
||
| const filterComponent = ( | ||
| <div className="flex gap-2 items-center"> | ||
| {Filter} | ||
| {oldFiles.length > 0 && ( | ||
| <Button | ||
| variant="outline" | ||
| size="sm" | ||
| onClick={() => setShowOldFiles(!showOldFiles)} | ||
| className="ml-auto" | ||
| > | ||
| {showOldFiles ? ( | ||
| <> | ||
| <EyeOff className="mr-2 h-4 w-4" /> | ||
| Hide older files | ||
| </> | ||
| ) : ( | ||
| <> | ||
| <Eye className="mr-2 h-4 w-4" /> | ||
| Show {oldFiles.length} older file | ||
| {oldFiles.length !== 1 ? "s" : ""} | ||
| </> | ||
| )} | ||
| </Button> | ||
| )} | ||
| </div> | ||
| ); | ||
|
|
||
| return ( | ||
| <DataTable | ||
| columns={columns} | ||
| data={displayedFiles} | ||
| count={displayedCount} | ||
| Filter={filterComponent} | ||
| defaultPageSize={defaultPageSize} | ||
| /> | ||
| ); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.