Skip to content

Commit 280cfec

Browse files
committed
Configire email settings
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent 0d89b3f commit 280cfec

5 files changed

Lines changed: 43 additions & 12 deletions

File tree

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ install_requires =
6060
djangorestframework>=3.12.4
6161
django-filter>=2.4.0
6262
django-widget-tweaks>=1.4.8
63+
django-crispy-forms>=1.10.0
6364
django-environ>=0.8.0
6465
gunicorn>=20.1.0
6566

vulnerabilities/forms.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class Meta:
4646
"last_name",
4747
)
4848

49+
def __init__(self, *args, **kwargs):
50+
super(ApiUserCreationForm, self).__init__(*args, **kwargs)
51+
self.fields["username"].help_text = f"<ul><li>{self.fields['username'].help_text}</li></ul>"
52+
4953
def save(self, commit=True):
5054
return ApiUser.objects.create_api_user(
5155
username=self.cleaned_data["username"],
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
{% extends "base.html" %}
2+
{%load crispy_forms_tags %}
23

3-
{% block title %}
4-
VulnerableCode API key request
5-
{% endblock %}
64

75
{% block content %}
86
<section class="section pt-0">
9-
{{ form.as_p }}
7+
{% block title %}
8+
VulnerableCode API key request
9+
{% endblock %}
10+
<br/>
11+
<form action = "" method = "post">
12+
{% csrf_token %}
13+
{{form|crispy }}
14+
<br/>
15+
<input class="button is-link" type="submit" value="Submit">
16+
</form>
1017
</section>
1118
{% endblock %}

vulnerabilities/views.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
# See https://aboutcode.org for more information about nexB OSS projects.
88
#
99

10+
from django.core.mail import send_mail
1011
from django.db.models import Count
1112
from django.db.models import Q
13+
from django.http import HttpResponse
1214
from django.http.response import Http404
1315
from django.shortcuts import render
1416
from django.urls import reverse_lazy
@@ -22,6 +24,7 @@
2224
from vulnerabilities.forms import ApiUserCreationForm
2325
from vulnerabilities.forms import PackageSearchForm
2426
from vulnerabilities.forms import VulnerabilitySearchForm
27+
from vulnerablecode.settings import env
2528

2629
PAGE_SIZE = 20
2730

@@ -232,9 +235,18 @@ class ApiUserCreateView(generic.CreateView):
232235
template_name = "api_user_creation_form.html"
233236

234237
def form_valid(self, form):
235-
# TODO: send an email with the API key
236-
response = super().form_valid(form)
237-
# TODO: return http response with a simple success message that
238+
super().form_valid(form)
239+
240+
send_mail(
241+
subject="VCIO API Key",
242+
message=f"Here is your token for the VCIO API: {self.object.auth_token}",
243+
from_email=env.str("FROM_EMAIL", default=""),
244+
recipient_list=[self.object.email],
245+
)
246+
247+
return HttpResponse(
248+
f"We have mailed you the token for VCIO API on this email {self.object.email}"
249+
)
238250

239251
def get_success_url(self):
240-
return reverse_lazy("api_user_creation_success", kwargs={"uuid": self.object.pk})
252+
return reverse_lazy("home")

vulnerablecode/settings.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@
4949
# SECURITY WARNING: do not run with debug turned on in production
5050
DEBUG_UI = env.bool("VULNERABLECODE_DEBUG_UI", default=False)
5151

52+
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
53+
EMAIL_HOST = "smtp.gmail.com"
54+
EMAIL_USE_TLS = True
55+
EMAIL_PORT = 587
56+
EMAIL_HOST_USER = env.str("EMAIL_HOST_USER", default="")
57+
EMAIL_HOST_PASSWORD = env.str("EMAIL_HOST_PASSWORD", default="")
58+
5259
# Application definition
5360

5461
INSTALLED_APPS = (
@@ -68,10 +75,7 @@
6875
"rest_framework",
6976
"rest_framework.authtoken",
7077
"widget_tweaks",
71-
# for API doc
72-
"drf_spectacular",
73-
# required for Django collectstatic discovery
74-
"drf_spectacular_sidecar",
78+
"crispy_forms",
7579
)
7680

7781
MIDDLEWARE = (
@@ -184,6 +188,9 @@
184188
str(PROJECT_DIR / "static"),
185189
]
186190

191+
192+
CRISPY_TEMPLATE_PACK = "bootstrap4"
193+
187194
# Third-party apps
188195

189196
# Django restframework

0 commit comments

Comments
 (0)