Self hosted FLOSS fitness/workout, nutrition and weight tracker
Check for duplicate weight entries on import
Yuyu Aung968c2a8808eb
diff --git a/wger/weight/helpers.py b/wger/weight/helpers.pyindex d8e71b90c..10d1a5108 100644--- a/wger/weight/helpers.py+++ b/wger/weight/helpers.py@@ -43,21 +43,36 @@ def parse_weight_csv(request, cleaned_data):# csv.reader expects a file-like object, so use StringIOparsed_csv = csv.reader(six.StringIO(cleaned_data['csv_input']),dialect)-+ distinct_weight_entries = []weight_list = []error_list = []++ # Process the CSV items firstfor row in parsed_csv:try:parsed_date = datetime.datetime.strptime(row[0], cleaned_data['date_format'])parsed_weight = decimal.Decimal(row[1].replace(',', '.'))-- weight_list.append(WeightEntry(date=parsed_date,- weight=parsed_weight,- user=request.user))+ duplicate_date_in_db = WeightEntry.objects.filter(date=parsed_date,+ user=request.user)+ # within the list there are no duplicates+ unique_among_csv = (parsed_date, parsed_weight) not in distinct_weight_entries+ # there is no existing weight entry in the database for that date+ unique_in_db = not duplicate_date_in_db++ if unique_among_csv and unique_in_db:+ distinct_weight_entries.append((parsed_date, parsed_weight))+ else:+ error_list.append(row)except (ValueError, IndexError, decimal.InvalidOperation):error_list.append(row)+ # Create the valid weight entries+ for date, weight in distinct_weight_entries:+ weight_list.append(WeightEntry(date=date,+ weight=weight,+ user=request.user))+return (weight_list, error_list)