work + report draft
This commit is contained in:
parent
43fe9aea31
commit
7a70163e76
42 changed files with 228 additions and 19 deletions
91
core/templates/write.html
Executable file → Normal file
91
core/templates/write.html
Executable file → Normal file
|
@ -19,13 +19,25 @@
|
|||
</div>
|
||||
|
||||
<form method="post" class="space-y-4">
|
||||
|
||||
|
||||
{% load set_content %}
|
||||
{% load access %}
|
||||
|
||||
{% for field in form %}
|
||||
<div class="mb-4">
|
||||
<label for="{{ field.id_for_label }}">
|
||||
{{ field.label|safe }}
|
||||
</label>
|
||||
{{ field }}
|
||||
|
||||
{% if field.id_for_label == "id_department" %}
|
||||
|
||||
{{ field|set_content:draft.department}}
|
||||
|
||||
{%else%}
|
||||
{% with content=draft.content|access:field.id_for_label %}
|
||||
{{ field|set_content:content }}
|
||||
{% endwith %}
|
||||
{%endif%}
|
||||
{% if field.errors %}
|
||||
<p class="text-red-500 text-sm">{{ field.errors|join:", " }}</p>
|
||||
{% endif %}
|
||||
|
@ -39,6 +51,81 @@
|
|||
Submit
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<script>
|
||||
function updateDraft() {
|
||||
const formData = {};
|
||||
|
||||
{% for field in form %}
|
||||
|
||||
var field = document.getElementById("{{ field.id_for_label }}");
|
||||
formData["{{ field.id_for_label }}"] = field.value;
|
||||
|
||||
{% endfor%}
|
||||
|
||||
fetch("/draft", {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRFToken': getCSRFToken(),
|
||||
},
|
||||
body: JSON.stringify(formData),
|
||||
})
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
console.log('Updated Draft:', data);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
});
|
||||
}
|
||||
|
||||
function debounce(func, delay) {
|
||||
let timeout;
|
||||
return function (...args) {
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(() => func.apply(this, args), delay);
|
||||
};
|
||||
}
|
||||
|
||||
function getCSRFToken() {
|
||||
const name = 'csrftoken';
|
||||
const cookies = document.cookie.split(';');
|
||||
for (let cookie of cookies) {
|
||||
cookie = cookie.trim();
|
||||
if (cookie.startsWith(name + '=')) {
|
||||
return cookie.substring(name.length + 1);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function setupListeners() {
|
||||
const textareas = document.querySelectorAll('textarea');
|
||||
const inputs = document.querySelectorAll('input');
|
||||
|
||||
const debouncedSend = debounce(() => updateDraft(), 500);
|
||||
|
||||
textareas.forEach(textarea => {
|
||||
textarea.addEventListener('input', () => debouncedSend());
|
||||
});
|
||||
|
||||
inputs.forEach(input => {
|
||||
if (input.type === 'text' || input.type === 'email' || input.type === 'password' || input.type === 'hidden') {
|
||||
input.addEventListener('input', () => debouncedSend());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
setupListeners();
|
||||
|
||||
</script>
|
||||
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue