split download.DownloadAttempt.successful into .email_sent and .code_valid

master
Tait Hoyem 4 years ago
parent 0d5578dc3d
commit cc485fa628

@ -0,0 +1,27 @@
# Generated by Django 3.1.2 on 2020-11-09 21:06
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('download', '0011_downloadattempt_geolocation'),
]
operations = [
migrations.RemoveField(
model_name='downloadattempt',
name='successful',
),
migrations.AddField(
model_name='downloadattempt',
name='code_correct',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='downloadattempt',
name='email_sent',
field=models.BooleanField(default=False),
),
]

@ -30,7 +30,8 @@ class EmailSent(models.Model):
class DownloadAttempt(models.Model):
timestamp = models.DateTimeField(auto_now_add=True, editable=False, null=False, blank=False)
user = models.ForeignKey(QuoteUser, on_delete=models.CASCADE, related_name='attempts')
successful = models.BooleanField(default=False)
email_sent = models.BooleanField(default=False)
code_correct = models.BooleanField(default=False)
pdf = models.ForeignKey(PDF, on_delete=models.CASCADE, related_name='attempts')
ip = models.GenericIPAddressField()
geolocation = models.CharField(max_length=64)

@ -3,7 +3,7 @@ from . import views
urlpatterns = [
path('', views.starter, name='download'),
path('d/<pdfid>/', views.download, name='download_f'),
path('dp/<pdfid>/', views.download_preload, name='download_f_plus'),
path('d/<addrid>/', views.download, name='download_f'),
path('dp/<addrid>/', views.download_preload, name='download_f_plus'),
path('search/<addr>/', views.search, name='search'),
]

@ -5,6 +5,7 @@ from .models import Address, PDF, EmailSent, DownloadAttempt
from .forms import CodeForm
from core.models import QuoteUser
from django.template.loader import render_to_string
from django.conf import settings
import ipinfo
import json
import datetime
@ -23,7 +24,14 @@ def get_client_ip(request):
def get_client_ip_info(request):
realip = get_client_ip(request)
return IPINFO_HANDLER.getDetails(realip)
handler = ipinfo.getHandler()
details = handler.getDetails(realip)
# needed for testing
if settings.DEBUG and (details.ip == '127.0.0.1' or details.ip == '::1'):
details.city = 'Local'
details.region = 'Local'
details.country = 'Local'
return details
# Create your views here.
def starter(request):
@ -61,31 +69,27 @@ def save_email(user, addr, pdf, dt):
ref_code=hashlib.sha256(dt.strftime("%Y%m%d%H%M%S").encode()).hexdigest()
)
def download(request, pdfid):
def download(request, addrid):
if request.method == 'POST':
form = CodeForm(request.POST)
if form.is_valid():
addr = Address.objects.filter(id=pdfid)
# get addr by id
addr = Address.objects.filter(id=addrid)
if len(addr) == 0:
return render(request, 'common/not-found.html')
# only get first addr
addr = addr[0]
# TODO: If same address + different code, the old file is still visible if the old code is still known
pdf = PDF.objects.filter(address=addr, code=form.cleaned_data['code']).order_by('upload_date').reverse()
if len(pdf) == 0:
return render(request, 'common/password-incorrect.html')
pdf = pdf[0]
# create user
user, created = QuoteUser.objects.get_or_create(username=form.cleaned_data['email'], email=form.cleaned_data['email'])
# disallow login for new user
# disallow login for new users
if created:
user.set_unusable_password()
user.save()
# TODO: fail gracefuly
# TODO: fail gracefully
ip = get_client_ip_info(request)
# create download attempt
dla = DownloadAttempt.objects.create(
user=user,
pdf=pdf,
pdf=PDF.objects.filter(address=addr).order_by('-upload_date')[0],
ip=ip.ip,
geolocation="{0}, {1}, {2}".format(
ip.city,
@ -93,34 +97,41 @@ def download(request, pdfid):
ip.country
)
)
# TODO: If same address + different code, the old file is still visible if the old code is still known
pdf = PDF.objects.filter(address=addr, code=form.cleaned_data['code']).order_by('upload_date').reverse()
if len(pdf) == 0:
return render(request, 'common/password-incorrect.html')
pdf = pdf[0]
dla.code_correct = True
dla.save()
# create timestamps
dt_date = datetime.datetime.now()
try:
send_email(form.cleaned_data['email'], addr, pdf, dt_date)
except:
return render(request, 'download/email-not-sent.html', {
'id': pdfid,
'id': addrid,
'code': form.cleaned_data['code']
})
# only saves email if it sent
save_email(user, addr, pdf, dt_date)
# only makes successful if email is sent
dla.successful = True
dla.email_sent = True
dla.save()
return render(request, 'download/email-confirm.html')
else:
form = CodeForm()
return render(request, 'download/code-form.html', {
'form': form,
'id': pdfid
'id': addrid
})
def download_preload(request, pdfid):
def download_preload(request, addid):
if request.method == 'POST':
code = request.POST.get('code')
email = request.POST.get('email')
form = CodeForm(initial={'code': code, 'email': email})
return render(request, 'download/code-form.html', {
'form': form,
'id': pdfid,
'id': addrid,
})

@ -18,6 +18,8 @@
<th>IP</th>
<th>Geolocation</th>
<th>Timestamp</th>
<th>Valid Code</th>
<th>Email Sent</th>
</tr>
{% for req in requests %}
<tr>
@ -26,6 +28,8 @@
<td>{{ req.ip }}</td>
<td>{{ req.geolocation }}</td>
<td>{{ req.timestamp }}</td>
<td>{{ req.code_correct }}</td>
<td>{{ req.email_sent }}</td>
</tr>
{% endfor %}
</table>

Loading…
Cancel
Save