Example #1
0
def student_sign_up(request):
    users = User.objects

    if request.method == 'POST':

        def is_used_student_id(new_id):
            try:
                users.get(username=new_id)
                return False
            except Exception:
                return True

        student_id = "A1000"
        while not is_used_student_id(student_id):
            student_id = random.choice([
                'A', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P',
                'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
            ]) + str(random.randint(1000, 9999))

        username = student_id
        password = request.POST['password']
        first_name = request.POST['first_name'].capitalize()
        last_name = request.POST['last_name'].capitalize()
        email = request.POST['email'].lower()
        gender = request.POST['gender']
        phone_number = request.POST['phone_number']
        address = request.POST['address']

        new_user = User.objects.create_user(username,
                                            email,
                                            password,
                                            first_name=first_name,
                                            last_name=last_name)
        user_id = new_user.id
        new_student = Student(user_id=user_id)
        new_student.user_id = user_id
        new_student.gender = gender
        new_student.phone_number = phone_number
        new_student.home_address = address
        try:
            new_student.photo = request.FILES['profile_photo']
        except Exception:
            new_student.photo = None
        new_student.save()
        # the next few lines before the return statement will email the Student's username to him/her
        student_login_link = 'http://logeeksatutors.com/student/sign_in/'
        email_subject = 'LOGEEKS \'A\'-TUTORS WELCOMES YOU'
        email_message = 'Your Username (Student ID) is ' + str(student_id) + '. \n Follow the link below to login into ' \
                                                                             'your Student Account. \n' \
                                                                             + str(student_login_link) + '\n\n' \
                                                                             'Thanks for choosing Logeeks! \n' \
                                                                             'Best Regards\n' \
                                                                             'Logeeks \'A\'-Tutors (Scientific).'

        # TODO: uncomment the next line to test its functionality, when there's internet connection;
        send_mail(email_subject, email_message, '*****@*****.**',
                  [str(email)])
        # new_user.email_user(subject=email_subject, message=email_message, from_email='*****@*****.**')
        context = {
            'new_student_first_name': first_name,
            'new_student_email': email
        }
        return render(request, 'student/student_sign_up_complete.html',
                      context)

    context = {}
    return render(request, 'student/student_sign_up.html', context)
Example #2
0
def create_student(strategy, details, response, user, *args, **kwargs):
    """
    Part of the Python Social Auth pipeline which creates a student upon
    signup. If student already exists, updates information from Facebook
    or Google (depending on the backend).

    Saves friends and other information to fill database.
    """
    backend_name = kwargs['backend'].name
    if Student.objects.filter(user=user).exists():
        new_student = Student.objects.get(user=user)
    else:
        new_student = Student(user=user)
        new_student.save()
    social_user = user.social_auth.filter(provider=backend_name, ).first()

    if backend_name == 'google-oauth2' and not user.social_auth.filter(
            provider='facebook').exists():
        try:
            access_token = social_user.extra_data["access_token"]
        except TypeError:
            access_token = json.loads(social_user.extra_data)["access_token"]
        response = requests.get(
            'https://www.googleapis.com/plus/v1/people/me'.format(
                social_user.uid, get_secret('GOOGLE_API_KEY')),
            params={'access_token': access_token})
        new_student.img_url = response.json()['image'].get('url', '')
        new_student.gender = response.json().get('gender', '')
        new_student.save()

    elif backend_name == 'facebook':

        try:
            access_token = social_user.extra_data["access_token"]
        except TypeError:
            access_token = json.loads(social_user.extra_data)["access_token"]

        if social_user:
            url = u'https://graph.facebook.com/{0}/' \
                  u'?fields=picture&type=large' \
                  u'&access_token={1}'.format(
                      social_user.uid,
                      access_token,
                  )
            request = urllib2.Request(url)
            data = json.loads(urllib2.urlopen(request).read())
            new_student.img_url = data['picture']['data']['url']
            url = u'https://graph.facebook.com/{0}/' \
                  u'?fields=gender' \
                  u'&access_token={1}'.format(
                      social_user.uid,
                      access_token,
                  )
            request = urllib2.Request(url)
            data = json.loads(urllib2.urlopen(request).read())
            try:
                new_student.gender = data.get('gender', '')
            except BaseException:
                pass
            new_student.fbook_uid = social_user.uid
            new_student.save()
            url = u'https://graph.facebook.com/{0}/' \
                  u'friends?fields=id' \
                  u'&access_token={1}'.format(
                      social_user.uid,
                      access_token,
                  )
            request = urllib2.Request(url)
            friends = json.loads(urllib2.urlopen(request).read()).get('data')

            for friend in friends:
                if Student.objects.filter(fbook_uid=friend['id']).exists():
                    friend_student = Student.objects.get(
                        fbook_uid=friend['id'])
                    if not new_student.friends.filter(
                            user=friend_student.user).exists():
                        new_student.friends.add(friend_student)
                        new_student.save()
                        friend_student.save()

    return kwargs