-
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
base: main
Are you sure you want to change the base?
Changes from all 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, | ||
|
Contributor
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)
Contributor
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,84 @@ | ||
| "use client"; | ||
|
|
||
| import { | ||
| Dialog, | ||
| DialogClose, | ||
| DialogContent, | ||
| DialogFooter, | ||
| DialogHeader, | ||
| DialogTitle, | ||
| DialogTrigger | ||
| } from "../ui/dialog"; | ||
| import { toast } from "sonner"; | ||
| import { Button } from "../ui/button"; | ||
| import { deleteFile } from "@/api/files"; | ||
| import { useRouter } from "next/navigation"; | ||
| import { Trash2 } from "lucide-react"; | ||
| import { useState } from "react"; | ||
|
|
||
| interface DeleteFileDialogProps { | ||
| fileId: number; | ||
| scanReportId: number; | ||
| fileName: string; | ||
| isOpen?: boolean; | ||
| setOpen?: (isOpen: boolean) => void; | ||
| needTrigger?: boolean; | ||
| } | ||
|
|
||
| const DeleteFileDialog = ({ | ||
| fileId, | ||
| scanReportId, | ||
| fileName, | ||
| isOpen, | ||
| setOpen, | ||
| needTrigger = false | ||
| }: DeleteFileDialogProps) => { | ||
| const router = useRouter(); | ||
| const [internalOpen, setInternalOpen] = useState(false); | ||
| const dialogOpen = isOpen !== undefined ? isOpen : internalOpen; | ||
| const setDialogOpen = setOpen || setInternalOpen; | ||
|
|
||
| const handleDelete = async () => { | ||
| const response = await deleteFile(scanReportId, fileId); | ||
| if (response.success) { | ||
| toast.success(`File "${fileName}" deleted successfully`); | ||
| router.refresh(); | ||
| setDialogOpen(false); | ||
| } else { | ||
| toast.error( | ||
| `Failed to delete file: ${response.errorMessage || "Unknown error"}` | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <Dialog open={dialogOpen} onOpenChange={setDialogOpen}> | ||
| {needTrigger && ( | ||
| <DialogTrigger asChild> | ||
| <Button variant="destructive" size="sm"> | ||
| <Trash2 className="h-4 w-4" /> | ||
| </Button> | ||
| </DialogTrigger> | ||
| )} | ||
| <DialogContent> | ||
| <DialogHeader className="text-start"> | ||
| <DialogTitle>Delete File</DialogTitle> | ||
| </DialogHeader> | ||
| <p className="text-sm"> | ||
| Are you sure you want to delete "{fileName}"? This action cannot be | ||
| undone and will permanently remove the file from storage. | ||
| </p> | ||
| <DialogFooter className="flex-col space-y-2 sm:space-y-0 sm:space-x-2"> | ||
| <Button variant="destructive" onClick={handleDelete}> | ||
| Delete | ||
| </Button> | ||
| <DialogClose asChild> | ||
| <Button variant="outline">Cancel</Button> | ||
| </DialogClose> | ||
| </DialogFooter> | ||
| </DialogContent> | ||
| </Dialog> | ||
| ); | ||
| }; | ||
|
|
||
| export default DeleteFileDialog; |
Uh oh!
There was an error while loading. Please reload this page.