refactor: meal duplicate and admin validation

This commit is contained in:
bobbyvish
2024-04-19 15:50:08 +05:30
parent 61313ad7d2
commit b93c207ee6
4 changed files with 93 additions and 19 deletions

View File

@@ -633,10 +633,12 @@ class MealDuplicateAPIView(APIView):
model = MealRecord
def post(self, request):
meal_id = request.POST.get("id")
meal_time = request.POST.get("time")
meal_id = request.data.get("id")
meal_time = request.data.get("time")
print(f"meal_id is {meal_id}, and time is {meal_time}, and type of {type(meal_time)}")
try:
original_meal = self.model.objects.get(pk=meal_id)
original_meal = self.model.objects.get(pk=meal_id, principal=request.user)
except Exception as e:
return ApiResponse.error(message="Meal not found", errors=str(e))
@@ -646,6 +648,7 @@ class MealDuplicateAPIView(APIView):
time=meal_time,
meal_type=original_meal.meal_type
)
print(f"record id is {original_meal}")
instance.save()

View File

@@ -45,7 +45,7 @@
</a>
<ul class="collapse submenu list-unstyled show" id="iam" data-bs-parent="#accordionExample">
<li class="{% if page_name == iam_constants_context.RESOURCE_IAM_PRINCIPAL_GROUP %}active{% endif %}">
<a href="{% url 'module_iam:principal_group_link' %}"> IAM Principal</a>
<a href="{% url 'module_iam:principal_group_link' %}"> Manage Admin</a>
</li>
<!-- <li class="{% if page_name == iam_constants_context.RESOURCE_IAM_GROUP %}active{% endif %}">
<a href="{% url 'module_iam:principal_group' %}"> IAM Group </a>

View File

@@ -13,14 +13,12 @@
<div class="col-lg-12">
<div class="row mb-2">
<div class="col">
<h3>{{operation}} Principal Group Link</h3>
<a href="{% url 'module_iam:principal_group_link'%}" style="height: fit-content;width: fit-content;display: inline-block;">
<h3 class="card-title m-2 d-flex align-items-center gap-2" style="width: fit-content;"><span class="fw-bold material-symbols-outlined">
arrow_back
</span><span>{{operation}} Principal Group Link</span></h3>
</a>
</div>
<div class="col text-end">
<button class="btn btn-dark mb-2 me-4" onclick="history.back()">
<i class="fa fa-arrow-left"></i>
Back
</button>
</div>
</div>
<div class="row layout-spacing">
<div class="col-lg-12">

View File

@@ -13,21 +13,19 @@
<div class="col-lg-12">
<div class="row mb-2">
<div class="col">
<h3>{{operation}} Principal Group Link</h3>
<a href="{% url 'module_iam:principal_group_link'%}" style="height: fit-content;width: fit-content;display: inline-block;">
<h3 class="card-title m-2 d-flex align-items-center gap-2" style="width: fit-content;"><span class="fw-bold material-symbols-outlined">
arrow_back
</span><span>{{operation}} Principal Group Link</span></h3>
</a>
</div>
<div class="col text-end">
<button class="btn btn-dark mb-2 me-4" onclick="history.back()">
<i class="fa fa-arrow-left"></i>
Back
</button>
</div>
</div>
<div class="row layout-spacing">
<div class="col-lg-12">
<div class="statbox widget box box-shadow">
<div class="widget-content widget-content-area">
<form method="post" autocomplete="off">
<form method="post" autocomplete="off" id="principalForm">
{% csrf_token %}
{% include 'base_structure/includes/dynamic_template_form.html' with form=form %}
<div class="mt-4 mb-0">
@@ -57,6 +55,81 @@
tokenSeparators: [',', ' '], // Customize token separators
closeOnSelect: false // Keep the dropdown open after selection
});
// Add custom validation method for email
$.validator.addMethod("validEmail", function(value, element) {
return /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/.test(value);
}, "Please enter a valid email address.");
// Add custom validation method to check for special characters
$.validator.addMethod("noSpecialChars", function(value, element) {
return /^[a-zA-Z\s]*$/.test(value); // Allow only letters and whitespace
}, "Please enter only letters and spaces.");
// Add custom validation method to check for starting with a letter
$.validator.addMethod("startsWithLetter", function(value, element) {
return /^[a-zA-Z]/.test(value); // Check if the value starts with a letter
}, "Please start with a letter.");
// Initialize form validation
$("#principalForm").validate({
rules: {
email: {
required: true,
validEmail: true
},
first_name: {
required: true,
minlength: 2,
maxlength: 20,
noSpecialChars: true,
startsWithLetter: true
},
last_name: {
required: true,
minlength: 2,
maxlength: 20,
noSpecialChars: true,
startsWithLetter: true
}
},
messages: {
email: {
required: "Please enter your email address.",
validEmail: "Please enter a valid email address."
},
first_name: {
required: "Please enter your first name.",
minlength: "First name must be at least 2 characters.",
maxlength: "First name must not exceed 20 characters.",
noSpecialChars: "Please enter only letters and spaces.",
startsWithLetter: "First name must start with a letter."
},
last_name: {
required: "Please enter your last name.",
minlength: "Last name must be at least 2 characters.",
maxlength: "Last name must not exceed 20 characters.",
noSpecialChars: "Please enter only letters and spaces.",
startsWithLetter: "Last name must start with a letter."
}
},
errorElement: 'div',
errorPlacement: function(error, element) {
error.addClass('invalid-feedback');
$(element).closest('.form-group').append(error);
},
highlight: function(element, errorClass, validClass) {
$(element).addClass('is-invalid').removeClass('is-valid');
},
unhighlight: function(element, errorClass, validClass) {
$(element).removeClass('is-invalid').addClass('is-valid');
},
submitHandler: function(form) {
// Disable the submit button to prevent multiple submissions
$('button[type="submit"]').prop('disabled', true);
}
});
});
</script>