Skip to content

Commit

Permalink
Fixes gitcoinco#118 - Use ValidationError over bare exception for ema…
Browse files Browse the repository at this point in the history
…il validation
  • Loading branch information
mbeacom committed Dec 14, 2017
1 parent 9a29878 commit ac88050
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 32 deletions.
18 changes: 9 additions & 9 deletions app/dashboard/ios.py
@@ -1,6 +1,6 @@
import json
import logging

from django.core.exceptions import ValidationError
from django.core.validators import validate_email
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
Expand Down Expand Up @@ -32,16 +32,16 @@ def save(request):
direction = body.get('direction')
github_username = body.get('github_username')

#do validation
# do validation
validation_failed = False

#email
# email
try:
validate_email(email_address)
except Exception as e:
except ValidationError:
validation_failed = 'email'

#bounty
# bounty
if not Bounty.objects.filter(pk=bounty_id).exists():
validation_failed = 'bounty does not exist'

Expand All @@ -59,23 +59,23 @@ def save(request):
message = 'Validation failed: {}'.format(validation_failed)
else:
bounty = Bounty.objects.get(pk=bounty_id)
#save obj
# save obj
Match.objects.create(
bounty=bounty,
email=email_address,
direction=direction,
github_username=github_username,
)
)

#send match email
# send match email
if direction == '+':
to_emails = [email_address, bounty.bounty_owner_email]
new_match(to_emails, bounty, github_username)

# response
status = 200
message = 'Success'

response = {
'status': status,
'message': message,
Expand Down
11 changes: 3 additions & 8 deletions app/retail/views.py
Expand Up @@ -16,14 +16,13 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.validators import validate_email
from django.http import JsonResponse
from django.shortcuts import redirect
from django.template.response import TemplateResponse

from marketing.utils import get_or_save_email_subscriber, invite_to_slack
from slackclient import SlackClient


def index(request):
Expand Down Expand Up @@ -381,10 +380,6 @@ def help_dev(request):
return redirect('https://docs.google.com/document/d/1S8BLKJF7J5RbrfFw-mX0iYcy4VSc6-a1aQXtKT_ta0Y/edit')


def help_dev(request):
return redirect('https://docs.google.com/document/d/1S8BLKJF7J5RbrfFw-mX0iYcy4VSc6-a1aQXtKT_ta0Y/edit')


def help_pilot(request):
return redirect('https://docs.google.com/document/d/1R-qQKlIcW38d7l6GumehDlOhdmX1-6Ibab3gE06qotQ/edit')

Expand All @@ -407,15 +402,15 @@ def ios(request):

def slack(request):
context = {
'active': 'slack',
'active': 'slack',
}

if request.POST.get('email', False):
email = request.POST['email']
valid_email = True
try:
validate_email(request.POST.get('email', False))
except Exception as e:
except ValidationError:
valid_email = False

if valid_email:
Expand Down
29 changes: 14 additions & 15 deletions app/tdi/views.py
@@ -1,5 +1,5 @@
'''
Copyright (C) 2017 Gitcoin Core
Copyright (C) 2017 Gitcoin Core
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
Expand All @@ -23,6 +23,7 @@
from django.conf import settings
from django.contrib import messages
from django.contrib.admin.views.decorators import staff_member_required
from django.core.exceptions import ValidationError
from django.core.validators import validate_email
from django.http import HttpResponse
from django.shortcuts import redirect
Expand All @@ -44,6 +45,7 @@
def ratelimited(request, ratelimited=False):
return whitepaper_access(request, ratelimited=True)


@ratelimit(key='ip', rate='5/m', method=ratelimit.UNSAFE, block=True)
def whitepaper_new(request, ratelimited=False):

Expand All @@ -52,7 +54,7 @@ def whitepaper_new(request, ratelimited=False):
'title': 'Whitepaper',
'minihero': 'Whitepaper',
'suppress_logo': True,
}
}
if not request.POST.get('submit', False):
return TemplateResponse(request, 'whitepaper_new.html', context)

Expand All @@ -75,19 +77,19 @@ def whitepaper_new(request, ratelimited=False):
""".format(context['email'], context['role'], context['comments'], ip)
send_mail(settings.CONTACT_EMAIL, settings.CONTACT_EMAIL, "New Whitepaper Request", str(body))

war = WhitepaperAccessRequest.objects.create(
WhitepaperAccessRequest.objects.create(
email=context['email'],
role=context['role'],
comments=context['comments'],
ip=ip,
)

invite_to_slack(context['email'])

valid_email = True
try:
validate_email(request.POST.get('email', False))
except Exception as e:
except ValidationError:
valid_email = False

if not request.POST.get('email', False) or not valid_email:
Expand Down Expand Up @@ -115,7 +117,6 @@ def whitepaper_access(request, ratelimited=False):
context['msg'] = "You're ratelimited. Please contact founders@gitcoin.co"
return TemplateResponse(request, 'whitepaper_accesscode.html', context)


context['accesskey'] = request.POST.get('accesskey')
context['email'] = request.POST.get('email')
access_codes = AccessCodes.objects.filter(invitecode=request.POST.get('accesskey'))
Expand All @@ -140,7 +141,7 @@ def whitepaper_access(request, ratelimited=False):
return TemplateResponse(request, 'whitepaper_accesscode.html', context)

ip = get_ip(request)

wa = WhitepaperAccess.objects.create(
invitecode=request.POST.get('accesskey', False),
email=request.POST.get('email', False),
Expand All @@ -165,7 +166,6 @@ def whitepaper_access(request, ratelimited=False):
can.drawString(left, 7, msg)
can.save()


# middle watermark
packet2 = StringIO.StringIO()
can = canvas.Canvas(packet2, pagesize=letter)
Expand All @@ -180,9 +180,7 @@ def whitepaper_access(request, ratelimited=False):
can.drawString(320, 50, msg)
can.save()


#move to the beginning of the StringIO buffer
file_name = 'whitepaper.pdf'
# move to the beginning of the StringIO buffer
path_to_file = 'assets/other/wp.pdf'
new_pdf1 = PdfFileReader(packet1)
new_pdf2 = PdfFileReader(packet2)
Expand All @@ -205,12 +203,13 @@ def whitepaper_access(request, ratelimited=False):
output.write(outputStream)
outputStream.close()

filename = outputfile
filename = outputfile
wrapper = FileWrapper(file(filename))
response = HttpResponse(wrapper, content_type='application/pdf')
response['Content-Length'] = os.path.getsize(filename)
return response


@staff_member_required
def process_accesscode_request(request, pk):

Expand All @@ -227,17 +226,17 @@ def process_accesscode_request(request, pk):
h.update(h.hexdigest() + str(timezone.now()))
invitecode = h.hexdigest()[:29]

code = AccessCodes.objects.create(
AccessCodes.objects.create(
invitecode=invitecode,
maxuses=1,
)
)
obj.processed = True
obj.save()

from_email = settings.PERSONAL_CONTACT_EMAIL
to_email = obj.email
subject = request.POST.get('subject')
body = request.POST.get('body').replace('[code]',invitecode)
body = request.POST.get('body').replace('[code]', invitecode)
send_mail(from_email, to_email, subject, body, from_name="Kevin from Gitcoin.co")
messages.success(request, 'Invite sent')

Expand Down

0 comments on commit ac88050

Please sign in to comment.