Skip to content
Snippets Groups Projects
Commit 65e02d2c authored by Odin Johan Vatne's avatar Odin Johan Vatne
Browse files

Fix duplicate comments on import

parent 60d5b983
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,8 @@ import csv
import os
from distutils.util import strtobool
from contextlib import contextmanager
from django.db import IntegrityError
from pasapp.models import ProjectTag, TagCategory, Tag, UserTag, Project, Application, Comment
from pasapp.forms import NewUserForm
from django.contrib.auth.models import User
......@@ -317,6 +319,7 @@ def read_csv(input_folder):
project_id = get_or_throw(project_map, row[col_map['f_project']]) #Project.objects.get(id=row[col_map['f_project']])
student = User.objects.get(username=row[col_map['f_student']])
params = get_params(col_map.items(), row)
try:
retrieved, created = Application.objects.get_or_create(project_id=project_id, student=student, **params)
if retrieved:
if ref_id:
......@@ -328,6 +331,13 @@ def read_csv(input_folder):
else:
logging.warning(f"Could not create Application by {row[col_map['f_student']]} on project with refID '{row[col_map['f_project']]}'. Found at row {row_num} of applications.csv.")
failed_cnt += 1
except IntegrityError as e:
retrieved = Application.objects.get(project_id=project_id, student=student)
if ref_id:
application_map[ref_id] = retrieved.pk
existing_cnt +=1
except Exception as e:
raise e
if created_cnt > 0: stats_add('Applications created', created_cnt)
if existing_cnt > 0: stats_add('Applications already existing', existing_cnt)
if failed_cnt > 0: stats_add('Applications failed', failed_cnt+error_cnt)
......@@ -351,7 +361,7 @@ def read_csv(input_folder):
if row_num == 0:
for i, val in enumerate(row):
col_map[val] = i
for key in ('f_application', 'f_sender'):
for key in ('f_application', 'f_sender', 'message'):
if key not in col_map:
logging.error(f"'comments.csv' is missing necessary column '{key}'")
return
......@@ -361,6 +371,11 @@ def read_csv(input_folder):
application_id = get_or_throw(application_map, row[col_map['f_application']])
sender = User.objects.get(username=row[col_map['f_sender']])
params = get_params(col_map.items(), row)
existing = Comment.objects.filter(application_id=application_id, sender=sender, message=params['message']) # ignores duplicates at different datetimes
if existing:
retrieved = existing.last()
created = False
else:
retrieved, created = Comment.objects.get_or_create(application_id=application_id, sender=sender, **params)
if retrieved:
if ref_id:
......
......@@ -2,7 +2,7 @@ ref_id,f_application,f_sender,message,d_date_created
,emileSepsis,damiand,Do you have any experience working with sensitive or classified data?,n-4d
,emileSepsis,emile,I have interned with the census office for one summer.,n-3d
,emileSepsis,damiand,It sounds like you have the exact right skills for this project! I will send you an offer.,n-2d
,emileSepsis,emile,Thank you so much! I look forward to working with you.,n-1d
,emileSepsis,emile,Thank you so much! I look forward to working with you.,
,bobboSepsis,damiand,"Thank you for your interest! Unfortunately, I need a student with more prior experience in this area.",n-4d
,ingridiSepsis,ingridi,"Also, my friend Justine would like to join me as a group for this project. Is that possible?",n-3d
,ingridiSepsis,damiand,"Hi Ingrid, I'm sorry but this project is only open to single applicants. It does sound like you have relevant skills however, so you're welcome to keep your application as a single applicant.",n-2d
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment