Skip to content

Commit

Permalink
Merge pull request #606 from RickWangPerth/v2.5uat
Browse files Browse the repository at this point in the history
Fix new account login issue
  • Loading branch information
RickWangPerth authored Oct 7, 2024
2 parents 3a9eed8 + ad8bcf1 commit 3687ee1
Show file tree
Hide file tree
Showing 17 changed files with 474 additions and 136 deletions.
47 changes: 34 additions & 13 deletions wamtram2/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .models import TrtPersons, TrtDataEntry, TrtTags, TrtEntryBatches, TrtPlaces, TrtPitTags, Template, TrtObservations,TrtTagStates, TrtMeasurementTypes,TrtYesNo
from django_select2.forms import ModelSelect2Widget
from django.core.validators import RegexValidator
from django.db.models import Case, When, IntegerField


tagWidget = ModelSelect2Widget(
Expand Down Expand Up @@ -198,10 +199,6 @@ class Meta:
"recapture_left_tag_barnacles_2",
"recapture_right_tag_barnacles",
"recapture_right_tag_barnacles_2",
"new_left_tag_barnacles",
"new_left_tag_barnacles_2",
"new_right_tag_barnacles",
"new_right_tag_barnacles_2",
"identifier",
"identification_type",

Expand Down Expand Up @@ -271,6 +268,7 @@ def __init__(self, *args, **kwargs):
'placeholder': 'Enter name',
})


nesting_choices = TrtYesNo.objects.filter(code__in=['N', 'P', 'Y'])
self.fields['nesting'].queryset = nesting_choices

Expand All @@ -283,20 +281,37 @@ def __init__(self, *args, **kwargs):
self.fields[field_name].queryset = filtered_measurement_types


tag_state_order = ["A1", "AE", "#"]

tag_state_order_case = Case(
*[When(tag_state=state, then=pos) for pos, state in enumerate(tag_state_order)],
default=len(tag_state_order),
output_field=IntegerField()
)

# Filter the queryset for new tag fields
new_tag_states = TrtTagStates.objects.filter(
tag_state__in=["A1", "AE", "#"]
)
tag_state__in=tag_state_order
).order_by(tag_state_order_case)

self.fields['new_left_tag_state'].queryset = new_tag_states
self.fields['new_right_tag_state'].queryset = new_tag_states
self.fields['new_left_tag_state_2'].queryset = new_tag_states
self.fields['new_right_tag_state_2'].queryset = new_tag_states

# Filter the queryset for recapture (old) tag fields
old_tag_states = TrtTagStates.objects.filter(
tag_state__in=[ "#", "R", "RC", "OO", "P", "P_OK"]
old_tag_state_order = ["P_OK","P", "RC", "RN", "OO", "R", "#"]

old_tag_state_order_case = Case(
*[When(tag_state=state, then=pos) for pos, state in enumerate(old_tag_state_order)],
default=len(old_tag_state_order),
output_field=IntegerField()
)

old_tag_states = TrtTagStates.objects.filter(
tag_state__in=old_tag_state_order
).order_by(old_tag_state_order_case)

self.fields['recapture_left_tag_state'].queryset = old_tag_states
self.fields['recapture_right_tag_state'].queryset = old_tag_states
self.fields['recapture_left_tag_state_2'].queryset = old_tag_states
Expand Down Expand Up @@ -408,10 +423,6 @@ def __init__(self, *args, **kwargs):
self.fields["recapture_left_tag_barnacles_2"].required = False
self.fields["recapture_right_tag_barnacles"].required = False
self.fields["recapture_right_tag_barnacles_2"].required = False
self.fields["new_left_tag_barnacles"].required = False
self.fields["new_left_tag_barnacles_2"].required = False
self.fields["new_right_tag_barnacles"].required = False
self.fields["new_right_tag_barnacles_2"].required = False
self.fields["identifier"].required = False
self.fields["identification_type"].required = False

Expand Down Expand Up @@ -500,6 +511,17 @@ def save(self, commit=True):
def clean(self):
cleaned_data = super().clean()
do_not_process = cleaned_data.get("do_not_process")

# tag_fields = [
# 'recapture_left_tag_id', 'recapture_left_tag_id_2', 'recapture_left_tag_id_3',
# 'recapture_right_tag_id', 'recapture_right_tag_id_2', 'recapture_right_tag_id_3',
# 'new_left_tag_id', 'new_left_tag_id_2', 'new_right_tag_id', 'new_right_tag_id_2',
# 'dud_filpper_tag', 'dud_filpper_tag_2'
# ]

# for field in tag_fields:
# if cleaned_data.get(field):
# cleaned_data[field] = cleaned_data[field].upper()

if do_not_process:
return cleaned_data
Expand Down Expand Up @@ -575,7 +597,6 @@ def clean(self):
cleaned_data['place_code'] = None

return cleaned_data


class TrtObservationsForm(forms.ModelForm):
class Meta:
Expand Down
14 changes: 9 additions & 5 deletions wamtram2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,10 +731,6 @@ class TrtDataEntry(models.Model):
recapture_left_tag_barnacles_2 = models.BooleanField(default=False, db_column='RECAPTURE_LEFT_TAG_BARNACLES_2')
recapture_right_tag_barnacles = models.BooleanField(default=False, db_column='RECAPTURE_RIGHT_TAG_BARNACLES')
recapture_right_tag_barnacles_2 = models.BooleanField(default=False, db_column='RECAPTURE_RIGHT_TAG_BARNACLES_2')
new_left_tag_barnacles = models.BooleanField(default=False, db_column='NEW_LEFT_TAG_BARNACLES')
new_left_tag_barnacles_2 = models.BooleanField(default=False, db_column='NEW_LEFT_TAG_BARNACLES_2')
new_right_tag_barnacles = models.BooleanField(default=False, db_column='NEW_RIGHT_TAG_BARNACLES')
new_right_tag_barnacles_2 = models.BooleanField(default=False, db_column='NEW_RIGHT_TAG_BARNACLES_2')

# Measurements
# Curved carapace length min
Expand Down Expand Up @@ -1362,7 +1358,7 @@ def __str__(self):

@staticmethod
def get_ordered_locations():
priority_codes = ['BW', 'CD', 'CL', 'DA', 'DH', 'LA', 'LO', 'MN', 'RI', 'TH', 'VA', 'WK', 'PH']
priority_codes = ['BW', 'CL', 'DA', 'DH', 'LO', 'MN', 'RI', 'TH', 'VA', 'WK', 'PH']

custom_order = Case(
*[When(location_code=code, then=Value(i)) for i, code in enumerate(priority_codes)],
Expand Down Expand Up @@ -2150,6 +2146,11 @@ class TrtSighting(models.Model):
class Meta:
managed = False
db_table = "TRT_SIGHTING"


class TrtSpeciesManager(models.Manager):
def get_queryset(self):
return super().get_queryset().exclude(species_code='0')


class TrtSpecies(models.Model):
Expand All @@ -2168,13 +2169,16 @@ class TrtSpecies(models.Model):
hide_dataentry = models.BooleanField(
db_column="Hide_DataEntry"
) # Field name made lowercase.

objects = TrtSpeciesManager()

class Meta:
managed = False
db_table = "TRT_SPECIES"

def __str__(self):
return f"{self.common_name}"



class TrtTags(models.Model):
Expand Down
26 changes: 22 additions & 4 deletions wamtram2/static/js/tagValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ document.addEventListener('DOMContentLoaded', function() {

function validateTag(tagInput, validationMessage, detailedMessage, type, side) {
const turtleId = turtleIdInput ? turtleIdInput.value : null;
const tagId = tagInput ? tagInput.value : null;
let tagId = tagInput ? tagInput.value.trim().toUpperCase() : null;

tagInput.value = tagId;

tagInput.classList.remove('is-valid', 'is-invalid', 'is-warning');

Expand Down Expand Up @@ -141,9 +143,13 @@ document.addEventListener('DOMContentLoaded', function() {
setValidationStatus(tagInput, validationMessage, detailedMessage, 'invalid', '✗ Invalid tag:', `Tag status - ${data.status}`);
doNotProcessField.checked = true;
} else if (data.tag_not_found) {
setValidationStatus(tagInput, validationMessage, detailedMessage, 'invalid', '✗ Invalid tag: Tag not found (Please remove it from here and add it to the comment area)');
doNotProcessField.checked = true;

if(!turtleId && (type === 'recaptured_tag' || type === 'recaptured_pit_tag')) {
setValidationStatus(tagInput, validationMessage, detailedMessage, 'invalid', '? Untagged turtle with old tag');
doNotProcessField.checked = true;
}else{
setValidationStatus(tagInput, validationMessage, detailedMessage, 'invalid', '✗ Invalid tag: Tag not found (Please remove it from here and add it to the comment area)');
doNotProcessField.checked = true;
}
} else {
setValidationStatus(tagInput, validationMessage, detailedMessage, 'invalid', '✗ Invalid tag');
doNotProcessField.checked = true;
Expand Down Expand Up @@ -190,12 +196,24 @@ document.addEventListener('DOMContentLoaded', function() {
e.preventDefault();
removeTagAndAddToComment(input);
});
} else if (status === 'invalid' && message.includes('Untagged turtle with old tag')) {
detailedMessage.innerHTML = `${detailedMessageText} <a href="#" class="remove-tag-link">Remove the tag</a>`;
const link = detailedMessage.querySelector('.remove-tag-link');
link.addEventListener('click', function(e) {
e.preventDefault();
removeTag(input);
});
} else {
detailedMessage.innerHTML = detailedMessageText;
}

}

function removeTag(input) {
input.value = '';
input.dispatchEvent(new Event('blur'));
}

function removeTagAndAddToComment(input) {
const tagValue = input.value;
const commentArea = document.getElementById('id_comments');
Expand Down
65 changes: 59 additions & 6 deletions wamtram2/templates/wamtram2/add_batches_code.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="{% url 'home' %}">Home</a></li>
<li class="breadcrumb-item"><a href="{% url 'wamtram2:batches_curation' %}">Batch Management</a></li>
<li class="breadcrumb-item"><a href="{% url 'wamtram2:batches_curation' %}">Manage Batches</a></li>
<li class="breadcrumb-item active">Edit Batch Details</li>
<li class="breadcrumb-item"><a href="{% url 'wamtram2:entry_batch_detail' batch_id %}">{{ batch_id }}</a></li>
</ol>
Expand All @@ -57,21 +57,24 @@
{% csrf_token %}
<div class="col-md-3">
<label for="location_code">Location (required)</label>
<select class="form-control" id="location_code" name="location_code">
<select class="form-control" id="location_code" name="location_code" required>
<option value="">Select Location</option>
{% for location in locations %}
<option value="{{ location.location_code }}">{{ location.location_name }}</option>
{% endfor %}
</select>
</div>
<div class="col-md-3">
<label for="place_code">Place (required)</label>
<label for="place_code">Place (optional)</label>
<select class="form-control" id="place_code" name="place_code">
<option value="">Select Place</option>
<!-- Will be populated dynamically by JavaScript -->
</select>
</div>
<div class="col-md-2">
<label for="year">Year (required)</label>
<select class="form-control" id="year" name="year">
<select class="form-control" id="year" name="year" required>
<option value="">Select Year</option>
{% for year, short_year in years.items %}
<option value="{{ short_year }}" {% if year == current_year|stringformat:"s" %}selected{% endif %}>{{ year }}</option>
{% endfor %}
Expand Down Expand Up @@ -118,14 +121,23 @@ <h5>Generated Code: <span id="generatedCode"></span></h5>
<label for="comments">{{ form.comments.label }}</label>
<textarea name="comments" id="comments" class="form-control" rows="3">{{ form.comments.value|default_if_none:'' }}</textarea>
</div>
<div class="form-group">
{% comment %} <div class="form-group">
<label for="template">{{ form.template.label }}</label>
<select name="template" id="template" class="form-control">
<option value="">---------</option>
<option value="">Select Template</option>
{% for template in templates %}
<option value="{{ template.template_id }}" {% if template.template_id == form.initial.template %}selected{% endif %}>{{ template.name }}</option>
{% endfor %}
</select>
</div> {% endcomment %}
<div class="form-group">
<label for="template">{{ form.template.label }}</label>
<div class="search-container">
<input type="text" id="templateSearch" class="search-field form-control" placeholder="Search Template" autocomplete="off" value="{{ template_name|default:'' }}">
<input type="hidden" id="templateId" name="template" value="{{ template_id|default:'' }}">

<div id="templateSearchResults" class="search-results"></div>
</div>
</div>
<button type="submit" class="btn btn-primary">Save</button>
<a href="{% url 'wamtram2:batches_curation' %}" class="btn btn-secondary">Cancel</a>
Expand Down Expand Up @@ -246,6 +258,47 @@ <h5>Generated Code: <span id="generatedCode"></span></h5>

// Initialize event handlers
$(document).ready(function() {

// Search for templates
$('#templateSearch').on('input', function() {
var searchTerm = $(this).val();
if (searchTerm.length >= 2) {
$.ajax({
url: "{% url 'wamtram2:search_templates' %}",
data: { 'q': searchTerm },
dataType: 'json',
success: function(data) {
var results = $('#templateSearchResults');
results.empty();
if (data.length > 0) {
results.show();
data.forEach(function(template) {
var div = $('<div class="search-result"></div>');
div.text(template.name);
div.on('click', function() {
$('#templateSearch').val(template.name);
$('#templateId').val(template.template_id);
results.hide();
});
results.append(div);
});
} else {
results.hide();
}
}
});
} else {
$('#templateSearchResults').hide();
}
});

$(document).on('click', function(e) {
if (!$(e.target).closest('.search-container').length) {
$('.search-results').hide();
}
});


$('#location_code').change(function() {
var locationCode = $(this).val();
loadPlaces(locationCode);
Expand Down
Loading

0 comments on commit 3687ee1

Please sign in to comment.