can someone help me figure out... I would want to automatically trigger in database if the entries that was posted by a different user is a possible duplicate. if entries like date, time and location are the same... how can I do this? Thank you
So far I have below code, where can I insert possible duplicate in the database after the user submits the form
Model
class UserReport(models.Model):
PENDING = 1
APPROVED = 2
REJECTED = 3
STATUS = (
(PENDING, 'Pending'),
(APPROVED, 'Approved'),
(REJECTED, 'Rejected')
)
IF_DUPLICATE = (
('Duplicate', 'Duplicate'),
('Possible Duplicate', 'Possible Duplicate'),
('Not Duplicate', 'Not Duplicate')
)
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
userid = models.CharField(max_length=250, unique=True, null=True, blank=True)
# barangay = models.ForeignKey(Barangay_district, on_delete=models.CASCADE, null=True)
description = models.TextField(max_length=250, blank=True)
address = models.CharField(max_length=250)
country = models.CharField(max_length=50, blank=True, null=True)
state = models.CharField(max_length=250, blank=True, null=True)
city = models.CharField(max_length=50, blank=True, null=True)
pin_code = models.CharField(max_length=6, blank=True, null=True)
latitude = models.FloatField(max_length=20, blank=True, null=True)
longitude = models.FloatField(max_length=20, blank=True, null=True)
geo_location = gismodels.PointField(blank=True, null=True, srid=4326) # New field
upload_photovideo = models.FileField(upload_to='incident_report/image', blank=True, null=True)
date = models.DateField(auto_now_add=False, auto_now=False, blank=True, null=True)
time = models.TimeField(auto_now_add=False, auto_now=False, blank=True, null=True)
status = models.PositiveSmallIntegerField(choices=STATUS, blank=True, null=True)
duplicate = models.CharField(choices=IF_DUPLICATE,max_length=250, blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def get_status(self):
if self.status == 1:
incident_status = 'Pending'
elif self.status == 2:
incident_status = 'Approved'
elif self.status == 3:
incident_status = 'Rejected'
return incident_status
# New method to generate geo_location from lat, lng
def save(self, *args, **kwargs):
super(UserReport, self).save(*args, **kwargs)
if self.upload_photovideo:
if ".jpg" in self.upload_photovideo.url or ".png" in self.upload_photovideo.url:
#check if image exists before resize
img = Image.open(self.upload_photovideo.path)
if img.height > 1080 or img.width > 1920:
new_height = 720
new_width = int(new_height / img.height * img.width)
img = img.resize((new_width, new_height))
img.save(self.upload_photovideo.path)
if self.latitude and self.longitude:
self.geo_location = Point(float(self.longitude), float(self.latitude))
return super(UserReport, self).save(*args, **kwargs)
return super(UserReport, self).save(*args, **kwargs)
Views
def sa_incidentreports(request):
profile = get_object_or_404(UserProfile, user=request.user)
if request.method == 'POST':
form = UserReportForm(request.POST or None, request.FILES or None)
form_general = IncidentGeneralForm(request.POST or None, request.FILES or None)
# form_people = IncidentRemarksForm(request.POST or None, request.FILES or None)
form_media = IncidentRemarksForm(request.POST or None, request.FILES or None)
form_remarks = IncidentRemarksForm(request.POST or None, request.FILES or None)
try:
if form.is_valid() and form_general.is_valid() and form_remarks.is_valid():
date=request.POST.get("date")
time=request.POST.get("time")
address=request.POST.get("address")
city=request.POST.get("city")
pin_code=request.POST.get("pin_code")
latitude=request.POST.get("latitude")
longitude=request.POST.get("longitude")
description=request.POST.get("description")
accident_factor1 = request.POST.get("accident_factor")
accident_factor = AccidentCausation.objects.get(pk=accident_factor1)
collision_type1 = request.POST.get("collision_type")
collision_type = CollisionType.objects.get(pk=collision_type1)
crash_type1 = request.POST.get("crash_type")
crash_type = CrashType.objects.get(pk=crash_type1)
weather = request.POST.get("weather")
light = request.POST.get("light")
severity = request.POST.get("severity")
movement_code = request.POST.get("movement_code")
desc=request.POST.getlist("desc[]")
images=request.FILES.getlist("file[]")
responder = request.POST.get("responder")
action_taken = request.POST.get("action_taken")
form.user = request.user
user_report=UserReport(user=request.user,date=date,time=time,address=address,city=city,pin_code=pin_code,latitude=latitude,longitude=longitude,description=description)
user_report.status = 2
user_report.save()
incident_general=IncidentGeneral(user_report=user_report,accident_factor=accident_factor,
collision_type=collision_type,
crash_type=crash_type,
weather=weather,light=light,severity=severity,movement_code=movement_code)
incident_general.save()
incident_remarks = IncidentRemark(incident_general=incident_general,responder=responder,action_taken=action_taken)
incident_remarks.save()
messages.success(request,"Data Save Successfully")
request.session['latest__id'] = incident_general.id
return redirect('sa_incidentreports_additional')
except Exception as e:
print('invalid form')
messages.error(request, str(e))
else:
print('invalid formd')
print(form.errors)
print(form_general.errors)
print(form_remarks.errors)
else:
form = UserReportForm()
form_general = IncidentGeneralForm()
form_remarks = IncidentRemarksForm()
context = {
'form': form,
'form_general': form_general,
'form_remarks': form_remarks,
'profile':profile
}
return render(request,"pages/super/sa_incident_report.html", context)
CodePudding user response:
In your models.py you can add unique together
class UserReport(models.Model): class Meta: unique_together = ('date','time','geo_location')
then migrate. This will restrict duplication.