mirror of
https://github.com/Alexander-D-Karpov/akarpov
synced 2025-02-19 22:40:31 +03:00
added file notification on view, minor fixes
This commit is contained in:
parent
8583885960
commit
08198e0535
|
@ -6,7 +6,7 @@
|
|||
class FileForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = File
|
||||
fields = ["name", "private", "description"]
|
||||
fields = ["name", "private", "notify_user_on_view", "description"]
|
||||
|
||||
|
||||
class FolderForm(forms.ModelForm):
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
# Generated by Django 4.2.5 on 2023-09-24 16:47
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("files", "0024_alter_file_options_alter_filereport_options_and_more"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="file",
|
||||
name="notify_user_on_view",
|
||||
field=models.BooleanField(
|
||||
default=False, verbose_name="Receive notifications on file view"
|
||||
),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="basefileitem",
|
||||
name="parent",
|
||||
field=models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="children",
|
||||
to="files.folder",
|
||||
verbose_name="Folder",
|
||||
),
|
||||
),
|
||||
]
|
|
@ -26,6 +26,7 @@
|
|||
|
||||
class BaseFileItem(PolymorphicModel):
|
||||
parent = ForeignKey(
|
||||
verbose_name="Folder",
|
||||
to="files.Folder",
|
||||
null=True,
|
||||
blank=True,
|
||||
|
@ -74,6 +75,11 @@ class File(BaseFileItem, TimeStampedModel, ShortLinkModel, UserHistoryModel):
|
|||
description = TextField(blank=True, null=True)
|
||||
file_type = CharField(max_length=255, null=True, blank=True)
|
||||
|
||||
# extra settings
|
||||
notify_user_on_view = BooleanField(
|
||||
"Receive notifications on file view", default=False
|
||||
)
|
||||
|
||||
@property
|
||||
def file_name(self):
|
||||
return self.file.path.split("/")[-1]
|
||||
|
|
|
@ -1 +1,3 @@
|
|||
from . import doc, docx, json, odt, pdf, zip # noqa
|
||||
|
||||
# TODO: add gzip, xlsx
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
from akarpov.files.services.folders import delete_folder
|
||||
from akarpov.files.services.preview import get_base_meta
|
||||
from akarpov.files.tables import FileTable
|
||||
from akarpov.notifications.services import send_notification
|
||||
|
||||
logger = structlog.get_logger(__name__)
|
||||
|
||||
|
@ -148,6 +149,16 @@ def dispatch(self, request, *args, **kwargs):
|
|||
if "bot" in useragent:
|
||||
if file.file_type and file.file_type.split("/")[0] == "image":
|
||||
return HttpResponseRedirect(file.file.url)
|
||||
else:
|
||||
if file.notify_user_on_view:
|
||||
if self.request.user != file.user:
|
||||
send_notification(
|
||||
"File view",
|
||||
f"File {file.name} was opened",
|
||||
"site",
|
||||
user_id=file.user.id,
|
||||
conformation=True,
|
||||
)
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
|
|
|
@ -140,5 +140,42 @@
|
|||
|
||||
{% block inline_javascript %}
|
||||
{% endblock inline_javascript %}
|
||||
{% if request.user.is_authenticated %}
|
||||
<script>
|
||||
{# TODO: add automatic socket host retrieve #}
|
||||
let socket = new WebSocket(`ws://127.0.0.1:8000/ws/notifications/`);
|
||||
|
||||
function sleep(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
let fn = async function(event) {
|
||||
let data = JSON.parse(event.data)
|
||||
console.log(data)
|
||||
alert(data.body)
|
||||
{# TODO add pretty pop up #}
|
||||
}
|
||||
|
||||
socket.onmessage = fn
|
||||
socket.onclose = async function(event) {
|
||||
console.log("Notifications socket disconnected, reconnecting...")
|
||||
let socketClosed = true;
|
||||
await sleep(5000)
|
||||
while (socketClosed) {
|
||||
{# TODO: reconnect socket here #}
|
||||
try {
|
||||
let cl = socket.onclose
|
||||
socket = new WebSocket(`ws://127.0.0.1:8000/ws/notifications/`);
|
||||
socket.onmessage = fn
|
||||
socket.onclose = cl
|
||||
socketClosed = false
|
||||
} catch (e) {
|
||||
console.log("Can't connect to socket, reconnecting...")
|
||||
await sleep(1000)
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
{% endif %}
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -56,7 +56,7 @@
|
|||
</nav>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% if request.user.is_authenticated and is_folder_owner %}
|
||||
{% if request.user.is_authenticated and is_folder_owner and not folder_slug %}
|
||||
<div class="d-flex justify-content-end me-5 col">
|
||||
<a class="me-5" href="{% url 'files:table' %}">table view</a>
|
||||
</div>
|
||||
|
|
|
@ -26,11 +26,11 @@
|
|||
|
||||
{% block inline_javascript %}
|
||||
<script type="text/javascript">
|
||||
var md5 = "",
|
||||
let md5 = "",
|
||||
csrf = $("input[name='csrfmiddlewaretoken']")[0].value,
|
||||
form_data = [{"name": "csrfmiddlewaretoken", "value": csrf}];
|
||||
function calculate_md5(file, chunk_size) {
|
||||
var slice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
|
||||
let slice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
|
||||
chunks = chunks = Math.ceil(file.size / chunk_size),
|
||||
current_chunk = 0,
|
||||
spark = new SparkMD5.ArrayBuffer();
|
||||
|
@ -44,9 +44,9 @@
|
|||
}
|
||||
};
|
||||
function read_next_chunk() {
|
||||
var reader = new FileReader();
|
||||
let reader = new FileReader();
|
||||
reader.onload = onload;
|
||||
var start = current_chunk * chunk_size,
|
||||
let start = current_chunk * chunk_size,
|
||||
end = Math.min(start + chunk_size, file.size);
|
||||
reader.readAsArrayBuffer(slice.call(file, start, end));
|
||||
};
|
||||
|
@ -71,7 +71,7 @@
|
|||
{"name": "upload_id", "value": data.result.upload_id}
|
||||
);
|
||||
}
|
||||
var progress = parseInt(data.loaded / data.total * 100.0, 10);
|
||||
let progress = parseInt(data.loaded / data.total * 100.0, 10);
|
||||
$("#progress").text(Array(progress).join("=") + "> " + progress + "%");
|
||||
},
|
||||
done: function (e, data) { // Called when the file has completely uploaded
|
||||
|
|
Loading…
Reference in New Issue
Block a user