Пример #1
0
 def get_or_create_user(self, request = None, username = None, password = None):
     '''
     Retrieves the user from the Django DB. If the user is not
     found in the DB, then it tries to retrieve him from
     dionysos.teilar.gr
     '''
     try:
         student = Cronos(username, password)
         '''
         Try to pull the user from the Django DB
         '''
         user = User.objects.get(username = username)
         '''
         skip dionysos authentication if the user is one of the listed ADMINS
         '''
         if user.username in get_admins_usernames():
             if user.check_password(password):
                 return user
             else:
                 return
         '''
         If the user is found in the DB, try to login with those
         credentials in dionysos.teilar.gr
         '''
         try:
             student.dionysos_auth_login(request)
         except LoginError:
             '''
             Authentication failed
             '''
             return
         except CronosError:
             '''
             Connection issue with dionysos.teilar.gr. Try to authenticate
             with the password stored in the DB instead
             '''
             if password != decrypt_password(user.get_profile().dionysos_password):
                 return
     except User.DoesNotExist:
         '''
         If the user is not in the DB, try to log in with his
         dionysos.teilar.gr account
         '''
         try:
             student.dionysos_auth_login(request, personal_data = True, declaration = True, grades = True)
             '''
             The credentials worked, try to create a user based on those credentials
             '''
             student.get_dionysos_account(request)
             try:
                 '''
                 Relate the webscraped school name with the one stored in the DB
                 '''
                 student.dionysos_school = Departments.objects.get(name = student.dionysos_school)
             except Exception as error:
                 logger_syslog.error(error, extra = log_extra_data(request))
                 logger_mail.exception(error)
                 raise CronosError(u'Αδυναμία ανάκτησης της σχολής')
             user = self.add_student_to_db(request, student)
         except CronosError:
             raise
         except LoginError:
             '''
             Authentication failed
             '''
             return
     return user
Пример #2
0
def settings_accounts(request):
    '''
    The user's accounts settings webpage
    '''
    notification = {}
    eclass_credentials_form = EclassCredentialsForm()
    webmail_form = WebmailForm()
    declaration_form = DeclarationForm()
    grades_form = GradesForm()
    eclass_lessons_form = EclassLessonsForm()
    if request.user.get_profile().dionysos_password:
        raw_dionysos_password = decrypt_password(request.user.get_profile().dionysos_password),
    else:
        raw_dionysos_password = None
    student = Cronos(
        request.user.get_profile().dionysos_username,
        raw_dionysos_password,
    )
    if request.user.get_profile().eclass_username:
        student.eclass_username = request.user.get_profile().eclass_username
        student.eclass_password = decrypt_password(request.user.get_profile().eclass_password)
    if request.method == 'POST':
        if request.POST.get('eclass_password'):
            '''
            Update eclass credentials
            '''
            try:
                eclass_credentials_form = EclassCredentialsForm(request.POST)
                student.eclass_password = request.POST.get('eclass_password')
                if not request.user.get_profile().eclass_username:
                    if eclass_credentials_form.is_valid():
                        student.eclass_username = request.POST.get('eclass_username')
                    else:
                        raise LoginError
                try:
                    '''
                    Check if the e-class.teilar.gr credentials already exist in the DB,
                    but belong to another student's account
                    '''
                    user = User.objects.get(userprofile__eclass_username = student.eclass_username)
                    if user.username != request.user.username:
                        raise CronosError(u'Τα στοιχεία e-class.teilar.gr ανήκουν ήδη σε κάποιον άλλο λογαριασμό')
                except User.DoesNotExist:
                    pass
                '''
                Try to login with those credentials, and update eclass lessons afterwards
                '''
                student.get_eclass_lessons(request)
                '''
                Login succeeded, save eclass credentials to the user's profile
                '''
                request.user.get_profile().eclass_username = student.eclass_username
                request.user.get_profile().eclass_password = encrypt_password(student.eclass_password)
                request.user.get_profile().save()
                notification['success'] = u'Η ανανέωση των στοιχείων openclass.teilar.gr ήταν επιτυχής'
            except (CronosError, LoginError) as error:
                notification['error'] = error.value
        elif request.POST.get('webmail_password'):
            '''
            Update webmail credentials
            '''
            try:
                webmail_form = WebmailForm(request.POST)
                webmail_password = request.POST.get('webmail_password')
                if request.user.get_profile().webmail_username:
                    webmail_username = request.user.get_profile().webmail_username
                else:
                    if not webmail_form.is_valid():
                        raise LoginError
                    webmail_username = request.POST.get('webmail_username')
                try:
                    '''
                    Check if the myweb.teilar.gr credentials already exist in the DB,
                    but belong to another student's account
                    '''
                    user = User.objects.get(userprofile__webmail_username = webmail_username)
                    if user.username != request.user.username:
                        raise CronosError(u'Τα στοιχεία webmail.teilar.gr ανήκουν ήδη σε κάποιον άλλο λογαριασμό')
                except User.DoesNotExist:
                    user = None
                if not user:
                    user = UserProfile.objects.get(pk=request.user.id)
                user.webmail_username = webmail_username
                user.webmail_password = encrypt_password(webmail_password)
                '''
                Login was successful, add in DB
                '''
                user.save()
                notification['success'] = u'Η ανανέωση των στοιχείων myweb.teilar.gr ήταν επιτυχής'
            except (CronosError, LoginError) as error:
                notification['error'] = error.value
        elif request.POST.get('declaration'):
            '''
            Update the declaration
            '''
            try:
                student.get_dionysos_declaration(request)
                request.user.get_profile().declaration = student.dionysos_declaration
                request.user.get_profile().save()
                notification['success'] = u'Η ανανέωση της δήλωσης ήταν επιτυχής'
            except (CronosError, LoginError) as error:
                notification['error'] = error.value
        elif request.POST.get('grades'):
            '''
            Update the grades
            '''
            try:
                student.get_dionysos_grades(request)
                request.user.get_profile().grades = student.dionysos_grades
                request.user.get_profile().save()
                notification['success'] = u'Η ανανέωση της βαθμολογίας ήταν επιτυχής'
            except (CronosError, LoginError) as error:
                notification['error'] = error.value
        elif request.POST.get('eclass_lessons'):
            '''
            Update the eclass lessons
            '''
            try:
                student.get_eclass_lessons(request)
                notification['success'] = u'Η ανανέωση των μαθημάτων e-class ήταν επιτυχής'
            except (CronosError, LoginError) as error:
                notification['error'] = error.value
    return render_to_response('settings_accounts.html',{
        'notification': notification,
        'eclass_credentials_form': eclass_credentials_form,
        'webmail_form': webmail_form,
        'eclass_lessons_form': eclass_lessons_form,
        'declaration_form': declaration_form,
        'grades_form': grades_form,
       }, context_instance = RequestContext(request))