Ejemplo n.º 1
0
def journal_add_view(context, request):

    if IJournalEntry.providedBy(context):
        entry = context
        project = context.__parent__.__parent__
        add_form = False
    else:
        entry = JournalEntry()
        project = context
        add_form = True

    errors = {}
    defaults = {}

    if 'form.submitted' in request.POST:
        try:
            # FormEncode validation
            defaults = dict(request.POST)
            defaults['indicators'] = request.POST.get('indicators')
            form_result = entry_schema.to_python(request.POST)
        except formencode.validators.Invalid, why:
            errors = why.error_dict
        else:

            session = DBSession()

            # Handle image upload
            if form_result['image'] is not None:
                entry.image = File('image.jpg', form_result['image'].read())

            elif form_result['image_action'] == 'delete' and entry.image:
                session.delete(entry.image)

            entry.date = datetime.now()
            entry.text = form_result['text']
            entry.user = authenticated_user(request)

            # Check whether indicator belongs to this project.
            indicator_query = session.query(Indicator)
            indicator_query = indicator_query.filter(Project.id == project.id)
            indicator_query = indicator_query.join(Project.objectives)
            indicator_query = indicator_query.join(Objective.competences)
            indicator_query = indicator_query.join(Competence.indicator_sets)
            indicator_query = indicator_query.join(IndicatorSet.indicators)
            if form_result['indicators']:
                indicator_query = indicator_query.filter(
                    Indicator.id.in_(form_result['indicators']))
                indicators = indicator_query.all()
                entry.indicators = indicators

            if add_form:
                project.journal_entries.append(entry)

            if ITeacher.providedBy(authenticated_user(request)):
                return HTTPFound(location=model_url(
                    get_root(request)['projects'][project.id], request))
            return HTTPFound(
                location=model_url(authenticated_user(request), request))
Ejemplo n.º 2
0
def members_view(context, request):

    session = DBSession()
    all_students = session.query(Student).all()
    all_students = [
        student for student in all_students if not student in context.students
    ]
    all_teachers = session.query(Teacher).all()
    all_teachers = [
        teacher for teacher in all_teachers if not teacher in context.teachers
    ]

    if 'form.submitted' in request.POST:
        student_id = request.POST.get('student_id', None)
        if student_id:
            student = session.query(Student).filter_by(id=student_id).first()
            if student:
                context.students.append(student)
        teacher_id = request.POST.get('teacher_id', None)
        if teacher_id:
            teacher = session.query(Teacher).filter_by(id=teacher_id).first()
            if teacher:
                context.teachers.append(teacher)

        return HTTPFound(location=model_url(context, request, 'members.html'))
    # This should be a post request, but it has to be finished today ...
    elif 'remove_student' in request.GET:
        student_id = request.GET.get('remove_student', None)
        if student_id:
            student = context.students.filter_by(id=student_id).first()
            if student:
                context.students.remove(student)
        return HTTPFound(location=model_url(context, request, 'members.html'))
    elif 'remove_teacher' in request.GET:
        teacher_id = request.GET.get('remove_teacher', None)
        if teacher_id:
            teacher = context.teachers.filter_by(id=teacher_id).first()
            if teacher:
                context.teachers.remove(teacher)
        return HTTPFound(location=model_url(context, request, 'members.html'))

    root = get_root(request)
    students = []
    for student in context.students:
        students.append(root['users'][student.id])

    teachers = []
    for teacher in context.teachers:
        teachers.append(root['users'][teacher.id])

    return dict(api=TemplateAPI(request),
                context=context,
                students=students,
                all_students=all_students,
                teachers=teachers,
                all_teachers=all_teachers)
Ejemplo n.º 3
0
def journal_add_view(context, request):
    
    if IJournalEntry.providedBy(context):
        entry = context
        project = context.__parent__.__parent__
        add_form = False
    else:
        entry = JournalEntry()
        project = context
        add_form = True
        
    errors = {}
    defaults = {}
    
    if 'form.submitted' in request.POST:
        try:
            # FormEncode validation
            defaults = dict(request.POST)
            defaults['indicators'] = request.POST.get('indicators')
            form_result = entry_schema.to_python(request.POST)
        except formencode.validators.Invalid, why:
            errors=why.error_dict
        else:
            
            session = DBSession()
            
            # Handle image upload
            if form_result['image'] is not None:
                entry.image = File('image.jpg', form_result['image'].read())

            elif form_result['image_action'] == 'delete' and entry.image:
                session.delete(entry.image)
            
            entry.date = datetime.now()
            entry.text = form_result['text']
            entry.user = authenticated_user(request)
            
            # Check whether indicator belongs to this project.
            indicator_query = session.query(Indicator)
            indicator_query = indicator_query.filter(Project.id == project.id)
            indicator_query = indicator_query.join(Project.objectives)
            indicator_query = indicator_query.join(Objective.competences)
            indicator_query = indicator_query.join(Competence.indicator_sets)
            indicator_query = indicator_query.join(IndicatorSet.indicators)
            if form_result['indicators']:
                indicator_query = indicator_query.filter(Indicator.id.in_(form_result['indicators']))
                indicators = indicator_query.all()
                entry.indicators = indicators
            
            if add_form:
                project.journal_entries.append(entry)
                
            if ITeacher.providedBy(authenticated_user(request)):
                return HTTPFound(location = model_url(get_root(request)['projects'][project.id], request))
            return HTTPFound(location = model_url(authenticated_user(request), request))
Ejemplo n.º 4
0
def members_view(context, request):
    
    session = DBSession()
    all_students = session.query(Student).all()
    all_students = [student for student in all_students if not student in context.students]
    all_teachers = session.query(Teacher).all()
    all_teachers = [teacher for teacher in all_teachers if not teacher in context.teachers]
    
    if 'form.submitted' in request.POST:
        student_id = request.POST.get('student_id', None)
        if student_id:
            student = session.query(Student).filter_by(id=student_id).first()
            if student:
                context.students.append(student)
        teacher_id = request.POST.get('teacher_id', None)
        if teacher_id:
            teacher = session.query(Teacher).filter_by(id=teacher_id).first()
            if teacher:
                context.teachers.append(teacher)
        
        return HTTPFound(location = model_url(context, request, 'members.html'))
    # This should be a post request, but it has to be finished today ...
    elif 'remove_student' in request.GET:
        student_id = request.GET.get('remove_student', None)
        if student_id:
            student = context.students.filter_by(id=student_id).first()
            if student:
                context.students.remove(student)
        return HTTPFound(location = model_url(context, request, 'members.html'))
    elif 'remove_teacher' in request.GET:
        teacher_id = request.GET.get('remove_teacher', None)
        if teacher_id:
            teacher = context.teachers.filter_by(id=teacher_id).first()
            if teacher:
                context.teachers.remove(teacher)
        return HTTPFound(location = model_url(context, request, 'members.html'))
        
    root = get_root(request)
    students = []
    for student in context.students:
        students.append(root['users'][student.id])
        
    teachers = []
    for teacher in context.teachers:
        teachers.append(root['users'][teacher.id])
    
    return dict(api=TemplateAPI(request),
                context=context,
                students=students,
                all_students=all_students,
                teachers=teachers,
                all_teachers=all_teachers)
    
Ejemplo n.º 5
0
def teacher_edit_view(context, request):

    if ITeacher.providedBy(context):
        teacher = context
        context = teacher.__parent__
        add_form = False
    else:
        teacher = Teacher(id=uuid.uuid4())
        add_form = True

    errors = {}
    defaults = {}

    if "form.submitted" in request.POST:
        try:
            # FormEncode validation
            defaults = dict(request.POST)
            state = FormencodeState()
            state.user_id = teacher.user_name
            if add_form:
                form_result = teacher_add_schema.to_python(request.POST, state)
            else:
                form_result = teacher_schema.to_python(request.POST, state)
        except formencode.validators.Invalid, why:
            errors = why.error_dict
        else:
            changed = False

            # Convert password to SHA hash
            if form_result.get("password", None):
                form_result["password"] = "******" % sha.new(form_result["password"]).hexdigest()
                changed = True

            # Handle portrait upload
            if form_result["portrait"] is not None:

                # Scale image and convert to JPEG
                im = Image.open(form_result["portrait"].file)
                im.thumbnail((128, 128), Image.ANTIALIAS)
                # Convert to RGB if neccessary
                if im.mode != "RGB":
                    im = im.convert("RGB")
                outfile = StringIO()
                im.save(outfile, "JPEG")
                outfile.seek(0)

                teacher.portrait = File("portrait.jpg", outfile.read())
                changed = True

            del form_result["portrait"]

            # Apply schema fields to the student object
            field_names = [p.key for p in class_mapper(Teacher).iterate_properties]
            for field_name in field_names:
                if field_name in form_result.keys():
                    if form_result[field_name] != getattr(teacher, field_name):
                        setattr(teacher, field_name, form_result[field_name])
                        changed = True

            # Add student if this is the add form
            if add_form:
                session = DBSession()
                session.add(teacher)

                if not form_result["password"]:
                    reset_url = model_url(get_root(request), request, "retrieve_password.html")
                    teacher.send_password_reset(reset_url)

            return HTTPFound(location=model_url(context, request))
Ejemplo n.º 6
0
         indicator_query = indicator_query.join(IndicatorSet.indicators)
         if form_result['indicators']:
             indicator_query = indicator_query.filter(Indicator.id.in_(form_result['indicators']))
             indicators = indicator_query.all()
             entry.indicators = indicators
         
         if add_form:
             project.journal_entries.append(entry)
             
         if ITeacher.providedBy(authenticated_user(request)):
             return HTTPFound(location = model_url(get_root(request)['projects'][project.id], request))
         return HTTPFound(location = model_url(authenticated_user(request), request))
         
 elif 'form.cancel' in request.POST:
     if ITeacher.providedBy(authenticated_user(request)):
         return HTTPFound(location = model_url(get_root(request)['projects'][project.id], request))
     return HTTPFound(location = model_url(authenticated_user(request), request))
     
 else:
     if not add_form:
         for field_name in entry_schema.fields.keys():
             if hasattr(entry, field_name):
                 defaults[field_name] = getattr(entry, field_name)
             
         defaults['indicators'] = [indicator.id for indicator in entry.indicators]
         if entry.image:
             defaults['image_action'] = 'nochange'
         defaults['image'] = '' 
 
 session = DBSession()
 user = authenticated_user(request)
Ejemplo n.º 7
0
def teacher_edit_view(context, request):
    
    if ITeacher.providedBy(context):
        teacher = context
        context = teacher.__parent__
        add_form = False
    else:
        teacher = Teacher(id=uuid.uuid4())
        add_form = True
    
    errors = {}
    defaults = {}
    
    if 'form.submitted' in request.POST:
        try:
            # FormEncode validation
            defaults = dict(request.POST)
            state = FormencodeState()
            state.user_id = teacher.user_name
            if add_form:
                form_result = teacher_add_schema.to_python(request.POST, state)
            else:
                form_result = teacher_schema.to_python(request.POST, state)
        except formencode.validators.Invalid, why:
            errors=why.error_dict
        else:
            changed = False
            
            # Convert password to SHA hash
            if form_result.get('password', None):
                form_result['password'] = '******' % sha.new(form_result['password']).hexdigest()
                changed = True
            
            # Handle portrait upload
            if form_result['portrait'] is not None:
                
                # Scale image and convert to JPEG
                im = Image.open(form_result['portrait'].file)
                im.thumbnail((128, 128),Image.ANTIALIAS)
                # Convert to RGB if neccessary
                if im.mode != "RGB":
                    im = im.convert("RGB")
                outfile = StringIO()
                im.save(outfile, "JPEG")
                outfile.seek(0)

                teacher.portrait = File('portrait.jpg', outfile.read())
                changed = True
                
            del form_result['portrait']
            
            # Apply schema fields to the student object
            field_names = [ p.key for p in class_mapper(Teacher).iterate_properties ]
            for field_name in field_names:
                if field_name in form_result.keys():
                    if form_result[field_name] != getattr(teacher, field_name):
                        setattr(teacher, field_name, form_result[field_name])
                        changed = True
            
            # Add student if this is the add form
            if add_form:
                session = DBSession()
                session.add(teacher)
                
                if not form_result['password']:
                    reset_url = model_url(get_root(request), request, 'retrieve_password.html')
                    teacher.send_password_reset(reset_url)
                
            return HTTPFound(location = model_url(context, request))
Ejemplo n.º 8
0
    if 'form.submitted' in request.params:
        try:
            # FormEncode validation
            schema = PWResetRequestSchema()
            form_result = schema.to_python(request.params)
        except formencode.validators.Invalid, why:
            form = render_template(
                'templates/password_retrieve.pt',
                request=request,
                api=TemplateAPI(request),
            )
            # FormEncode fills template with error messages
            form = htmlfill.render(form,
                                   defaults=request.params,
                                   errors=why.error_dict)
            return Response(form)
        else:
            session = DBSession()
            user = session.query(User).filter_by(
                email=form_result['email']).one()
            reset_url = model_url(get_root(request), request,
                                  'retrieve_password.html')
            user.send_password_reset(reset_url)

            statusmessage.show(request, u'Password retrieval e-mail sent.')
            return HTTPFound(location=model_url(context, request))

    return render_template_to_response('templates/password_retrieve.pt',
                                       request=request,
                                       api=TemplateAPI(request))
Ejemplo n.º 9
0
                        identity = {'repoze.who.userid': form_result['email']}
                        headers = identifier.remember(request.environ, identity)
                    request.environ['repoze.who.userid'] = form_result['email']
                    return HTTPFound(location = model_url(context, request), headers=headers)
                else:
                    statusmessage.show(request, u"Retrieve request not valid.", u"error")
        
        return render_template_to_response('templates/password_reset.pt', request=request, api=TemplateAPI(request))
    
    # First step: Create and send reset url
    if 'form.submitted' in request.params:
        try:
            # FormEncode validation
            schema = PWResetRequestSchema()
            form_result = schema.to_python(request.params)
        except formencode.validators.Invalid, why:
            form = render_template('templates/password_retrieve.pt', request = request, api=TemplateAPI(request),)
            # FormEncode fills template with error messages
            form = htmlfill.render(form, defaults=request.params, errors=why.error_dict)
            return Response(form)
        else:
            session = DBSession()
            user = session.query(User).filter_by(email=form_result['email']).one()
            reset_url = model_url(get_root(request), request, 'retrieve_password.html')
            user.send_password_reset(reset_url)
                       
            statusmessage.show(request, u'Password retrieval e-mail sent.')
            return HTTPFound(location = model_url(context, request))
    
    return render_template_to_response('templates/password_retrieve.pt', request=request, api=TemplateAPI(request))
Ejemplo n.º 10
0
                indicators = indicator_query.all()
                entry.indicators = indicators

            if add_form:
                project.journal_entries.append(entry)

            if ITeacher.providedBy(authenticated_user(request)):
                return HTTPFound(location=model_url(
                    get_root(request)['projects'][project.id], request))
            return HTTPFound(
                location=model_url(authenticated_user(request), request))

    elif 'form.cancel' in request.POST:
        if ITeacher.providedBy(authenticated_user(request)):
            return HTTPFound(location=model_url(
                get_root(request)['projects'][project.id], request))
        return HTTPFound(
            location=model_url(authenticated_user(request), request))

    else:
        if not add_form:
            for field_name in entry_schema.fields.keys():
                if hasattr(entry, field_name):
                    defaults[field_name] = getattr(entry, field_name)

            defaults['indicators'] = [
                indicator.id for indicator in entry.indicators
            ]
            if entry.image:
                defaults['image_action'] = 'nochange'
            defaults['image'] = ''