Esempio 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))
Esempio n. 2
0
def application_view(context, request):

    user = authenticated_user(request)
    if user:
        if ITeacher.providedBy(user):
            return HTTPFound(location=model_url(context, request, "dashboard.html"))
        return HTTPFound(location=model_url(user, request))

    return HTTPFound(location=model_url(context, request, "login.html"))
Esempio n. 3
0
def application_view(context, request):

    user = authenticated_user(request)
    if user:
        if ITeacher.providedBy(user):
            return HTTPFound(
                location=model_url(context, request, 'dashboard.html'))
        return HTTPFound(location=model_url(user, request))

    return HTTPFound(location=model_url(context, request, 'login.html'))
Esempio n. 4
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))
Esempio n. 5
0
def project_edit_view(context, request):

    if IProject.providedBy(context):
        project = context
        context = project.__parent__
        add_form = False
    else:
        project = Project()
        add_form = True

    errors = {}
    defaults = {}

    if 'form.submitted' in request.POST:
        try:
            # FormEncode validation
            defaults = dict(request.POST)
            form_result = project_schema.to_python(request.POST)
        except formencode.validators.Invalid, why:
            errors = why.error_dict
        else:
            # Apply schema fields to the project object
            changed = False
            for field_name in project_schema.fields.keys():
                if form_result[field_name] != getattr(project, field_name):
                    setattr(project, field_name, form_result[field_name])
                    changed = True
            # Add project if this is the add form
            if add_form:
                session = DBSession()
                # Add the teacher that created the project to the project.
                user = authenticated_user(request)
                if ITeacher.providedBy(user):
                    project.teachers.append(user)
                session.add(project)
            return HTTPFound(location=model_url(context, request))
Esempio n. 6
0
def project_edit_view(context, request):
    
    if IProject.providedBy(context):
        project = context
        context = project.__parent__
        add_form = False
    else:
        project = Project()
        add_form = True
    
    errors = {}
    defaults = {}
    
    if 'form.submitted' in request.POST:
        try:
            # FormEncode validation
            defaults = dict(request.POST)
            form_result = project_schema.to_python(request.POST)
        except formencode.validators.Invalid, why:
            errors=why.error_dict
        else:
            # Apply schema fields to the project object
            changed = False
            for field_name in project_schema.fields.keys():
                if form_result[field_name] != getattr(project, field_name):
                    setattr(project, field_name, form_result[field_name])
                    changed = True
            # Add project if this is the add form
            if add_form:
                session = DBSession()
                # Add the teacher that created the project to the project.
                user = authenticated_user(request)
                if ITeacher.providedBy(user):
                    project.teachers.append(user)
                session.add(project)
            return HTTPFound(location = model_url(context, request))
Esempio 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))
Esempio n. 8
0
         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))
         
 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()
Esempio n. 9
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))
Esempio n. 10
0
                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: