Skip to content

Commit 9c7dcd0

Browse files
committed
Everything is Ok, need to do logical tests against the results
1 parent cd73271 commit 9c7dcd0

18 files changed

+583
-244
lines changed

forms.md

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Two Forms with Shared Fields (Bootstrap)</title>
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
8+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
9+
</head>
10+
<body>
11+
12+
<div class="container mt-3">
13+
<h2>Shared Fields</h2>
14+
<div id="shared-fields" class="mb-3">
15+
<div class="form-group mb-3">
16+
<label for="sharedField1" class="form-label">Shared Field 1:</label>
17+
<input type="text" class="form-control" id="sharedField1" name="sharedField1" required>
18+
</div>
19+
<div class="form-group mb-3">
20+
<label for="sharedField2" class="form-label">Shared Field 2:</label>
21+
<input type="text" class="form-control" id="sharedField2" name="sharedField2" required>
22+
</div>
23+
</div>
24+
25+
<h2>Form 1</h2>
26+
<form id="form1" action="central_submission_url" method="post">
27+
<div id="form1-fields">
28+
<input type="hidden" name="form_type" value="form1">
29+
<div class="form-group mb-3">
30+
<label for="form1Field" class="form-label">Form 1 Specific Field:</label>
31+
<input type="text" class="form-control form1-required" id="form1Field" name="form1Field">
32+
</div>
33+
<button type="submit" class="btn btn-primary" name="submit_form" value="Submit Form 1">Submit Form 1</button>
34+
</div>
35+
</form>
36+
37+
<h2>Form 2</h2>
38+
<form id="form2" action="central_submission_url" method="post">
39+
<div id="form2-fields">
40+
<input type="hidden" name="form_type" value="form2">
41+
<div class="form-group mb-3">
42+
<label for="form2Field" class="form-label">Form 2 Specific Field:</label>
43+
<input type="text" class="form-control form2-required" id="form2Field" name="form2Field">
44+
</div>
45+
<button type="submit" class="btn btn-primary" name="submit_form" value="Submit Form 2">Submit Form 2</button>
46+
</div>
47+
</form>
48+
</div>
49+
50+
<script>
51+
document.querySelectorAll('#form1-fields input.form1-required').forEach(input => {
52+
input.removeAttribute('required');
53+
});
54+
document.querySelectorAll('#form2-fields input.form2-required').forEach(input => {
55+
input.removeAttribute('required');
56+
});
57+
58+
document.querySelector('#form1-fields button[type="submit"]').addEventListener('click', function() {
59+
document.querySelectorAll('#form1-fields input.form1-required').forEach(input => {
60+
input.setAttribute('required', 'required');
61+
});
62+
document.querySelectorAll('#form2-fields input.form2-required').forEach(input => {
63+
input.removeAttribute('required');
64+
});
65+
});
66+
67+
document.querySelector('#form2-fields button[type="submit"]').addEventListener('click', function() {
68+
document.querySelectorAll('#form2-fields input.form2-required').forEach(input => {
69+
input.setAttribute('required', 'required');
70+
});
71+
document.querySelectorAll('#form1-fields input.form1-required').forEach(input => {
72+
input.removeAttribute('required');
73+
});
74+
});
75+
76+
const forms = document.querySelectorAll('form');
77+
forms.forEach(form => {
78+
form.addEventListener('submit', function(event) {
79+
event.preventDefault();
80+
81+
const formType = this.querySelector('input[name="form_type"]').value;
82+
if (!validateForm(formType)) {
83+
return;
84+
}
85+
86+
const formData = new FormData(this);
87+
addSharedFieldsData(formData);
88+
submitFormData('central_submission_url', formData);
89+
});
90+
});
91+
92+
93+
function validateForm(formType) {
94+
let isValid = true;
95+
let formSpecificFields;
96+
let formTitle;
97+
98+
if (formType === 'form1') {
99+
formSpecificFields = document.querySelectorAll('#form1-fields input.form1-required');
100+
formTitle = 'Form 1';
101+
} else if (formType === 'form2') {
102+
formSpecificFields = document.querySelectorAll('#form2-fields input.form2-required');
103+
formTitle = 'Form 2';
104+
} else {
105+
return false; // Unknown form type
106+
}
107+
108+
formSpecificFields.forEach(element => {
109+
if (!element.value.trim() && element.hasAttribute('required')) {
110+
isValid = false;
111+
alert('Please fill in all required fields in ' + formTitle);
112+
return false;
113+
}
114+
});
115+
if (!isValid) return false;
116+
117+
118+
const sharedFields = document.getElementById('shared-fields').querySelectorAll('input[required]');
119+
sharedFields.forEach(element => {
120+
if (!element.value.trim()) {
121+
isValid = false;
122+
alert('Please fill in all Shared Fields.');
123+
return false;
124+
}
125+
});
126+
127+
return isValid;
128+
}
129+
130+
131+
function addSharedFieldsData(formData) {
132+
const sharedFieldsContainer = document.getElementById('shared-fields');
133+
const sharedInputs = sharedFieldsContainer.querySelectorAll('input');
134+
sharedInputs.forEach(input => {
135+
formData.append(input.name, input.value);
136+
});
137+
}
138+
139+
function submitFormData(url, formData) {
140+
fetch(url, {
141+
method: 'POST',
142+
body: formData
143+
})
144+
.then(response => {
145+
if (response.ok) {
146+
alert('Form submitted successfully to ' + url);
147+
// Optionally: Redirect or handle success response
148+
} else {
149+
alert('Form submission failed.');
150+
// Optionally: Handle error response
151+
}
152+
})
153+
.catch(error => {
154+
console.error('Error:', error);
155+
alert('Network error occurred during form submission.');
156+
});
157+
}
158+
</script>
159+
160+
</body>
161+
</html>

src/risk_manager/static/admin/css/forms.css

+4
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ form .aligned select + div.help {
169169
padding-left: 10px;
170170
}
171171

172+
form .aligned select option:checked {
173+
background-color: var(--selected-row);
174+
}
175+
172176
form .aligned ul li {
173177
list-style: none;
174178
}

src/risk_manager/static/trade/bootstrap.bundle.min.js

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/risk_manager/static/trade/bootstrap.min.css

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Generated by Django 5.1.4 on 2025-02-26 06:41
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("trade", "0017_trade_risk_reward_trade_stop_percent_and_more"),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name="trade",
15+
name="amount",
16+
field=models.DecimalField(
17+
blank=True, decimal_places=4, max_digits=25, null=True
18+
),
19+
),
20+
migrations.AlterField(
21+
model_name="trade",
22+
name="entry",
23+
field=models.DecimalField(
24+
blank=True, decimal_places=4, max_digits=25, null=True
25+
),
26+
),
27+
migrations.AlterField(
28+
model_name="trade",
29+
name="stop",
30+
field=models.DecimalField(
31+
blank=True, decimal_places=4, max_digits=25, null=True
32+
),
33+
),
34+
migrations.AlterField(
35+
model_name="trade",
36+
name="target",
37+
field=models.DecimalField(
38+
blank=True, decimal_places=4, max_digits=25, null=True
39+
),
40+
),
41+
]

src/risk_manager/trade/models.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,9 @@ class Trade(models.Model):
207207
symbol: Symbol = models.ForeignKey(Symbol, on_delete=models.CASCADE)
208208
date = models.DateTimeField(auto_now_add=True)
209209
date_ended = models.DateTimeField(auto_now=True, null=True, blank=True)
210-
stop = models.DecimalField(max_digits=25, decimal_places=4)
211-
entry = models.DecimalField(max_digits=25, decimal_places=4)
212-
target = models.DecimalField(max_digits=25, decimal_places=4)
210+
stop = models.DecimalField(max_digits=25, decimal_places=4, null=True, blank=True)
211+
entry = models.DecimalField(max_digits=25, decimal_places=4, null=True, blank=True)
212+
target = models.DecimalField(max_digits=25, decimal_places=4, null=True, blank=True)
213213
# This will be a computational property
214214
# riskReward = models.DecimalField(max_digits=25,
215215
# decimal_places=4,
@@ -218,7 +218,7 @@ class Trade(models.Model):
218218
result_amount = models.DecimalField(
219219
null=True, blank=True, max_digits=25, decimal_places=4, default=None
220220
)
221-
amount = models.DecimalField(max_digits=25, decimal_places=4)
221+
amount = models.DecimalField(max_digits=25, decimal_places=4, null=True, blank=True)
222222
picture = models.CharField(max_length=500, null=True, blank=True, default="")
223223
comment = models.TextField(null=True, blank=True, default="")
224224
isPositionChanged = models.BooleanField(default=False)

src/risk_manager/trade/signals.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ def check_balance_is_changed(sender: Trade, instance: Trade, **kwargs):
6060
or (before_edit.ub.reserve != instance.reserve)
6161
or (before_edit.balance != instance.balance)
6262
):
63-
ic("BEFORE:", instance.ub.balance, instance.ub.reserve, instance.amount)
63+
ic(
64+
"BEFORE:",
65+
before_edit.ub.balance,
66+
before_edit.ub.reserve,
67+
before_edit.amount,
68+
)
6469
tsm = TradeStateManager(instance, before_edit, sender)
6570
with transaction.atomic():
6671
instance.ub.save()

src/risk_manager/trade/templates/trade/add_balance.html

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
{% extends 'trade/layout.html' %} <!-- we want to use a layout -->
22
{% load static %} <!-- this will let us to use static -->
33
{% load i18n %} <!-- to use {% trans 'something' %} -->
4+
{% block js_head %}
5+
<script>
6+
7+
</script>
8+
{% endblock %}
49
{% comment %} the top sectoin of the position box {% endcomment %}
510
{% block trade_top_position %}
611
<script>
@@ -108,12 +113,12 @@
108113
</div>
109114
</div>
110115
</form>
111-
{% endblock trade_top_position %}
116+
{% endblock %}
112117

113118
{% comment %} Positoin box {% endcomment %}
114119
{% block trade_position_content %}
115120

116-
{% endblock trade_position_content %}
121+
{% endblock %}
117122

118123
{% comment %} the bottom sectoin of the postion box {% endcomment %}
119124
{% block trade_bot_position %}
@@ -126,7 +131,6 @@
126131
{% endif %}
127132
</div>
128133
</div>
129-
{% endblock trade_bot_position %}
134+
{% endblock %}
135+
130136

131-
{% block trade_new_content %}
132-
{% endblock trade_new_content %}

src/risk_manager/trade/templates/trade/layout.html

+14-17
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@
1414
<script type="module">
1515
import Decimal from "{% static 'trade/decimal.mjs' %}";
1616
</script>
17-
{% if viewname == "new" %}
18-
<script>
19-
/* Running */
20-
$(document).ready(function(){
21-
$("#form2 input").keyup(compute_amount)
22-
});
23-
</script>
24-
{% endif %}
17+
{% comment %}
18+
{% if viewname == "new" %}
19+
{% endif %}
20+
{% endcomment %}
21+
{% block js_head %}{% endblock %}
2522
</head>
2623

2724
<body class="container-fluid d-flex flex-column min-vh-100 opacity-75">
@@ -44,27 +41,25 @@
4441
<div class="flex-grow-1 d-flex h-100"> <!-- Added this wrapper -->
4542
<div class="row w-100 mx-auto">
4643
<article id="mainArticle" class="col-3 d-flex align-self-start">{% trans 'Calculator' %}</article>
47-
<nav id="mainNav" class="col-6 align-self-center card">
44+
<nav id="mainNav" class="col-9 align-self-center card">
4845

4946
<div class="card-header text-center">
5047
{{ title }}
5148
</div>
52-
{% block trade_top_position %}{% endblock trade_top_position %}
53-
{% block trade_position_content %}{% endblock trade_position_content %}
49+
{% block trade_top_position %}{% endblock %}
50+
{% block trade_position_content %}{% endblock %}
5451

5552
<div class="card-footer text-body-secondary">
5653

57-
{% block trade_bot_position %}{% endblock trade_bot_position %}
54+
{% block trade_bot_position %}{% endblock %}
5855
</div>
5956
</nav>
60-
<div id="siteAds" class="col-3 d-flex align-self-start justify-content-end text-right">{% trans 'Calendar' %}</div>
6157
</div>
6258
<hr class="mt-2 align-self-end">
6359
</div>
6460

6561

66-
{% block trade_index_content %}{% endblock trade_index_content %}
67-
{% block trade_new_content %}{% endblock trade_new_content %}
62+
{% block trade_index_content %}{% endblock %}
6863
<hr class="mt-2">
6964
<footer id="pageFooter" class="mt-auto">{% trans 'Footer' %}</footer>
7065
<script>
@@ -77,7 +72,7 @@
7772
inputs.forEach(function(input) {
7873
// If the input is empty, set its value to the placeholder
7974
if (!input.value) {
80-
console.log(input.value)
75+
// console.log(input.value)
8176
input.value = input.placeholder;
8277
}
8378
});
@@ -106,7 +101,9 @@
106101
})()
107102
</script>
108103
<script src="{% static 'trade/bootstrap.bundle.min.js' %}"></script>
109-
<script src="{% static 'trade/trade.js' %}"></script>
104+
{% comment %}
105+
<script src="{% static 'trade/trade.js' %}"></script>
106+
{% endcomment %}
110107
</body>
111108

112109
</html>

0 commit comments

Comments
 (0)