This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import date | |
from django.db import models | |
from django.contrib.auth.models import PermissionsMixin, AbstractUser | |
from django.contrib.auth.base_user import AbstractBaseUser | |
from django.utils.translation import ugettext_lazy as _ | |
from .manager import UserManager | |
class User(AbstractBaseUser, PermissionsMixin): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.auth.models import UserManager | |
class UserManager(UserManager): | |
use_in_migrations = True | |
def _create_user(self, phone_number, password, **extra_fields): | |
if not phone_number: | |
raise ValueError('The given phone number must be set') | |
user = self.model(phone_number=phone_number, **extra_fields) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RegisterView(SuccessMessageMixin, FormView): | |
template_name = 'register.html' | |
form_class = RegisterForm | |
success_message = "One-Time password sent to your registered mobile number.\ | |
The verification code is valid for 10 minutes." | |
def form_valid(self, form): | |
user = form.save() | |
username = self.request.POST['username'] | |
password = self.request.POST['password1'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf import settings | |
import requests | |
def send_verfication_code(user): | |
data = { | |
'api_key': settings.AUTHY_KEY, | |
'via': 'sms', | |
'country_code': user.country_code, | |
'phone_number': user.phone_number, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def PhoneVerificationView(request, **kwargs): | |
template_name = 'phone_confirm.html' | |
if request.method == "POST": | |
username = request.POST['username'] | |
user = User.objects.get(username=username) | |
form = PhoneVerificationForm(request.POST) | |
if form.is_valid(): | |
verification_code = request.POST['one_time_password'] | |
response = verify_sent_code(verification_code, user) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LoginView(FormView): | |
template_name = 'login.html' | |
form_class = LoginForm | |
success_url = '/dashboard' | |
def dispatch(self, request, *args, **kwargs): | |
if self.request.user.is_authenticated: | |
messages.add_message(self.request, messages.INFO, | |
"User already logged in") | |
return redirect('/dashboard') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LoginForm(forms.Form): | |
username = forms.CharField() | |
password = forms.CharField() | |
class Meta: | |
fields = ['username','password'] | |
def clean(self): | |
username = self.cleaned_data.get('username') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AUTHENTICATION_BACKENDS = ( | |
... | |
# Needed to login by username in Django admin, regardless of `allauth` | |
'django.contrib.auth.backends.ModelBackend', | |
# `allauth` specific authentication methods, such as login by e-mail | |
'allauth.account.auth_backends.AuthenticationBackend', | |
... | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def save_profile(sender, instance, **kwargs): | |
print(instance) | |
instance.user.full_name = instance.extra_data['name'] | |
uid = instance.extra_data['id'] | |
instance.user.profile_picture = instance.get_avatar_url() | |
instance.user.save() | |
post_save.connect(save_profile, sender=SocialAccount) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data_cat = ['train', 'valid'] # data categories | |
class ImageDataset(Dataset): | |
"""training dataset.""" | |
def __init__(self, df, transform=None): | |
""" | |
Args: | |
df (pd.DataFrame): a pandas DataFrame with image path and labels. | |
transform (callable, optional): Optional transform to be applied |
OlderNewer