You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
5.2 KiB
144 lines
5.2 KiB
from uuid import uuid4
|
|
|
|
from facepy import GraphAPI
|
|
from facepy.exceptions import FacepyError
|
|
|
|
from django.contrib.auth import get_user_model, logout, login, views
|
|
from django.contrib.auth.forms import AuthenticationForm
|
|
from django.http import JsonResponse
|
|
from django.urls import reverse_lazy
|
|
from django.utils.decorators import method_decorator
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
from django.views.generic import FormView, View
|
|
from django.views.generic.edit import BaseFormView
|
|
|
|
from apps.notification.utils import send_email
|
|
from apps.user.models import LilcityUserSettings, LilcityUserProxy
|
|
from .forms import LearnerRegistrationForm
|
|
from .tokens import verification_email_token
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
class LearnerRegistrationView(FormView):
|
|
form_class = LearnerRegistrationForm
|
|
template_name = "auth/registration-learner.html"
|
|
|
|
def form_valid(self, form):
|
|
first_name = form.cleaned_data['first_name']
|
|
last_name = form.cleaned_data['last_name']
|
|
email = form.cleaned_data['email']
|
|
password = form.cleaned_data['password']
|
|
|
|
user = User.objects.create_user(username=email, email=email, first_name=first_name, last_name=last_name, password=password)
|
|
login(self.request, user)
|
|
|
|
# fixme: change email text
|
|
# fixme: async send email
|
|
token = verification_email_token.make_token(user)
|
|
send_email('Verification Email', email, "notification/email/verification_email.html", token=token)
|
|
|
|
return JsonResponse({"success": True}, status=201)
|
|
|
|
def form_invalid(self, form):
|
|
return JsonResponse(form.errors.get_json_data(escape_html=True), status=400)
|
|
|
|
|
|
class LogoutView(View):
|
|
def post(self, request, *args, **kwargs):
|
|
logout(request)
|
|
return JsonResponse({"success": True})
|
|
|
|
|
|
class LoginView(FormView):
|
|
form_class = AuthenticationForm
|
|
template_name = "auth/login.html"
|
|
|
|
def form_valid(self, form):
|
|
login(self.request, form.get_user())
|
|
return JsonResponse({"success": True})
|
|
|
|
def form_invalid(self, form):
|
|
return JsonResponse({"success": False, "errors": form.errors.get_json_data(escape_html=True)}, status=400)
|
|
|
|
|
|
class VerificationEmailView(View):
|
|
def get(self, request, *args, **kwargs):
|
|
is_valid_token = verification_email_token.check_token(request.user, kwargs.get('token'))
|
|
|
|
if is_valid_token:
|
|
lilcity_user_settings = request.user.lilcity_user_settings
|
|
lilcity_user_settings.is_verification_email = True
|
|
lilcity_user_settings.save()
|
|
return JsonResponse({"success": True})
|
|
else:
|
|
return JsonResponse({"success": False}, status=400)
|
|
|
|
|
|
class PasswordResetView(views.PasswordContextMixin, BaseFormView):
|
|
email_template_name = "auth/password_reset.html"
|
|
subject_template_name = "auth/password_reset_subject.txt"
|
|
form_class = views.PasswordResetForm
|
|
|
|
extra_email_context = None
|
|
from_email = None
|
|
html_email_template_name = None
|
|
title = 'Password reset'
|
|
token_generator = views.default_token_generator
|
|
|
|
def form_valid(self, form):
|
|
opts = {
|
|
'use_https': self.request.is_secure(),
|
|
'token_generator': self.token_generator,
|
|
'from_email': self.from_email,
|
|
'email_template_name': self.email_template_name,
|
|
'subject_template_name': self.subject_template_name,
|
|
'request': self.request,
|
|
'html_email_template_name': self.html_email_template_name,
|
|
'extra_email_context': self.extra_email_context,
|
|
}
|
|
form.save(**opts)
|
|
return JsonResponse({"success": True})
|
|
|
|
|
|
class PasswordResetConfirmView(views.PasswordResetConfirmView):
|
|
template_name = "auth/password_reset_confirm.html"
|
|
success_url = reverse_lazy('lilcity:password_reset_complete')
|
|
|
|
|
|
class PasswordResetComplete(views.PasswordResetCompleteView):
|
|
pass
|
|
|
|
|
|
@method_decorator(csrf_exempt, name="dispatch")
|
|
class FacebookLoginOrRegistration(View):
|
|
def post(self, requests, *args, **kwargs):
|
|
access_token = requests.POST.get('access_token')
|
|
graph = GraphAPI(access_token)
|
|
try:
|
|
data = graph.get('/me?fields=email, first_name, last_name')
|
|
except FacepyError:
|
|
return JsonResponse({"success": False})
|
|
|
|
facebook_id = data.get('id')
|
|
|
|
lilcity_user_settings = LilcityUserSettings.objects.filter(facebook_id=facebook_id)
|
|
if lilcity_user_settings.count():
|
|
login(requests, user=lilcity_user_settings[0].user)
|
|
return JsonResponse({"success": True})
|
|
|
|
email = requests.POST.get('email') or data.get('email')
|
|
if not email:
|
|
return JsonResponse({"success": False,
|
|
"errors": {"email": 'is field required'}
|
|
})
|
|
else:
|
|
first_name = data.get('first_name', '')
|
|
last_name = data.get('last_name', '')
|
|
|
|
user = LilcityUserProxy.objects.create_user(username=email, email=email, first_name=first_name, last_name=last_name, password=uuid4().hex)
|
|
|
|
user.lilcity_user_settings.is_verification_email = True
|
|
user.lilcity_user_settings.facebook_id = facebook_id
|
|
user.lilcity_user_settings.save()
|
|
return JsonResponse({"success": True})
|
|
|