-
Notifications
You must be signed in to change notification settings - Fork 6
add AssetTemplate #400
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?
add AssetTemplate #400
Changes from 1 commit
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,70 @@ | ||
| # Generated by Django 4.2.4 on 2025-10-21 11:43 | ||
|
|
||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
| import django.db.models.deletion | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
| ("projects", "0025_connectionport"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="AssetTemplate", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.AutoField( | ||
| auto_created=True, | ||
| primary_key=True, | ||
| serialize=False, | ||
| verbose_name="ID", | ||
| ), | ||
| ), | ||
| ("name", models.CharField(max_length=255)), | ||
| ("desc", models.TextField(blank=True)), | ||
| ( | ||
| "visibility", | ||
| models.CharField( | ||
| choices=[ | ||
| ("project", "Project"), | ||
| ("account", "Account"), | ||
| ("global", "Everyone"), | ||
| ], | ||
| max_length=8, | ||
| ), | ||
| ), | ||
| ("created_ts", models.DateTimeField(auto_now_add=True)), | ||
| ("parameters", models.JSONField()), | ||
| ( | ||
| "asset_type", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| to="projects.assettype", | ||
| ), | ||
| ), | ||
| ( | ||
| "created_by", | ||
| models.ForeignKey( | ||
| blank=True, | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| to=settings.AUTH_USER_MODEL, | ||
| ), | ||
| ), | ||
| ( | ||
| "project", | ||
| models.ForeignKey( | ||
| blank=True, | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| to="projects.project", | ||
| ), | ||
| ), | ||
| ], | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -369,7 +369,7 @@ function submitForm() { | |
| postUrl += "/" + nodesToDB.get(topologyNodeId).uid; | ||
|
|
||
| // send the form of the asset to be saved in database (projects/views.py::asset_create_or_update) | ||
| fetch(postUrl, { | ||
| return fetch(postUrl, { | ||
| method: 'POST', | ||
| headers: {'X-CSRFToken': csrfToken}, | ||
| body: formData, | ||
|
|
@@ -390,11 +390,13 @@ function submitForm() { | |
| if(copCollapseDOM){ | ||
| copCollapse.hide(); | ||
| } | ||
| return Promise.resolve(); | ||
| } else { | ||
| // assign the content of the form to the form tag of the modal | ||
| guiModalDOM.querySelector('form .modal-body').innerHTML = jsonRes.form_html; | ||
| // make certain to show form | ||
| guiModal.show(); | ||
| return Promise.reject(); | ||
| } | ||
| }).catch(error => { | ||
| console.error(error); | ||
|
|
@@ -747,3 +749,77 @@ function computeCOP(event){ | |
| alert(error.message); | ||
| }); | ||
| } | ||
|
|
||
| /* templates */ | ||
| function load_template(e) { | ||
| e.preventDefault(); | ||
| let template_select = document.getElementById('template-select'); | ||
| if (!template_select?.value) { | ||
| alert("No template selected"); | ||
| return; | ||
| } | ||
| let url = templateUrl + '?id=' + template_select.value; | ||
| const form = document.getElementById("assetForm"); | ||
| if (!form) { | ||
| console.error("Form not found"); | ||
| return; | ||
| } | ||
| fetch(url).then(response => response.json()).then(data => { | ||
| for (let [key, value] of Object.entries(data)) { | ||
| let inp = form[key]; | ||
| if (inp) | ||
| inp.value = value; | ||
| else | ||
| console.log("Skip input field '" + key + "' (not found)"); | ||
| } | ||
| updateInputTimeseries(); | ||
| alert("Template '" + template_select.selectedOptions[0].textContent + "' loaded"); | ||
| }).catch(e => { | ||
| console.error("Error fetching template", e); | ||
| }); | ||
| } | ||
|
|
||
| function save_template(e) { | ||
| e.preventDefault(); | ||
| const assetFormData = new FormData(e.target.form); | ||
| let name = assetFormData.get("template-name"); | ||
| if (!name) { | ||
| alert("Need template name"); | ||
| return; | ||
| } | ||
| let desc = assetFormData.get("template-desc"); | ||
| let data = {}; | ||
| const ignoreKeys = [ | ||
| "name", "csrfmiddlewaretoken", | ||
| "template-name", "template-desc", "template-request_global", | ||
| ]; | ||
| assetFormData.forEach((value, key) => { | ||
| if (ignoreKeys.includes(key)) | ||
| return; | ||
| // only save string values, not objects (like files) | ||
| if (typeof value === "string") | ||
| data[key] = value; | ||
| }); | ||
| const templateFormData = new FormData(); | ||
|
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. Like here, for example, it seems like it would be cleaner to already have the form data instead of creating a new object here and populating it. |
||
| templateFormData.append('name', name); | ||
| templateFormData.append('desc', desc); | ||
| // visibility type from button name: template-project or template-account | ||
| templateFormData.append('visibility', e.target.name.substring(9)); | ||
| templateFormData.append('request_global', document.getElementById("chk-global")?.checked); | ||
| templateFormData.append('asset_type', guiModalDOM.getAttribute("data-node-type")); | ||
| templateFormData.append('data', JSON.stringify(data)); | ||
| submitForm().then(_ => { | ||
| fetch(templateUrl, { | ||
| method: 'POST', | ||
| headers: {'X-CSRFToken': csrfToken}, | ||
| body: templateFormData, | ||
| }).then(response => { | ||
| if (response.status != 201) | ||
| return Promise.reject(response.statusText); | ||
| // successfully created | ||
| alert("Template '" + name + "' saved"); | ||
| }).catch(e => { | ||
| console.error("Error saving template", e); | ||
| }); | ||
| }); | ||
| } | ||
|
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. Is there a reason you are adding the template fields in the raw html instead of implementing it through a
Author
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 wanted the template code to be explicit and readable and did not intend to use a Django form to send template data back when saving. If it were part of AssetCreateForm, the fields would be rendered alongside the other fields, so I would have needed some more custom filters. If it were not (making this its own form), it's even more complicated, since the HTML is replaced and part of another form. And I would have needed to send that other form data for each request. |
Uh oh!
There was an error while loading. Please reload this page.