Skip to content

Instantly share code, notes, and snippets.

View pyaf's full-sized avatar
😎
learning everyday

Rishabh Agrahari pyaf

😎
learning everyday
View GitHub Profile
@pyaf
pyaf / models.py
Last active March 24, 2017 10:19
models.py
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):
@pyaf
pyaf / manager.py
Created March 15, 2017 14:41
UserManager class
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)
@pyaf
pyaf / views.py
Last active March 24, 2017 11:12
RegisterView
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']
@pyaf
pyaf / authy.py
Created March 15, 2017 15:15
Authy API functions
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,
}
@pyaf
pyaf / views.py
Last active March 24, 2017 11:22
PhoneVerificationView
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)
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')
class LoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField()
class Meta:
fields = ['username','password']
def clean(self):
username = self.cleaned_data.get('username')
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',
...
)
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)
@pyaf
pyaf / datapipeline.py
Created February 2, 2018 15:22
Data pipeline and data augmentation on MURA dataset
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