Esempio n. 1
0
def userUpdateSettings():
    if request.method == 'POST':
        form = UserSettingsForm(request.form)
        if form.validate():
            user = current_user
            user.firstName = bleach.clean(form.firstName.data)
            user.lastName = bleach.clean(form.lastName.data)
            if form.email.data == "None":
                user.email = None
            else:
                user.email = bleach.clean(form.email.data)

            f = request.files.getlist('photo')[0]
            if len(f.filename) > 0:
                if user.photoName != None:
                    #Remove the existing photo
                    os.remove(getUserPhotoPath(user))
                #We have to upload a new photo
                photoName = secure_filename(f.filename)
                name, extension = os.path.splitext(photoName)
                ensurePathExists(getUserPhotoDir())
                f.save(
                    os.path.join(getUserPhotoDir(),
                                 str(g.user.id) + extension))
                user.photoName = str(g.user.id) + extension

            user.save()
            flash("Updated user information", "success")
            return redirect(url_for('userSettings'))
Esempio n. 2
0
def userUpdateSettings():
  if request.method == 'POST':
    form = UserSettingsForm(request.form)
    if form.validate():
      user = current_user
      user.firstName = bleach.clean(form.firstName.data)
      user.lastName = bleach.clean(form.lastName.data)
      if form.email.data == "None":
        user.email = None
      else:
        user.email = bleach.clean(form.email.data)

      f = request.files.getlist('photo')[0]
      if len(f.filename) > 0:
        if user.photoName != None:
          #Remove the existing photo
          os.remove(getUserPhotoPath(user))
        #We have to upload a new photo
        photoName = secure_filename(f.filename)
        name, extension = os.path.splitext(photoName)
        ensurePathExists(getUserPhotoDir())
        f.save(os.path.join(getUserPhotoDir(), str(g.user.id)+extension))
        user.photoName = str(g.user.id)+extension

      user.save()
      flash("Updated user information", "success")
      return redirect(url_for('userSettings'))
Esempio n. 3
0
def adminCourses():
  '''
  Function Type: View Function
  Template: admin/courses.html
  Purpose: Display all courses in the system and facilitate the creation of new
  courses.

  Inputs: None

  Template Parameters:
    active_page: A string for highlighting the active page in the nav-bar.
    form: A CreateCourseForm that is used to allow a user to input new course
    information.

  Forms Handled:
    CreateCourseForm: Validates the form and creates a new course with the
    specified name and semester.
  '''
  #even though we require login if someone gets here and is not admin
  #send them away. This is done in all methods for the admin panel
  if not g.user.isAdmin:
    return redirect(url_for('index'))

  if request.method == "POST":
    form = CreateCourseForm(request.form)
    if form.validate():
      #create a new course
      #TODO: Validate that a course with this name and semester doesn't already
      #exist
      try:
        c = Course.objects.get(name=form.name.data, semester=form.semester.data)
        flash("A course with this name and semester already exists", "warning")
      except Course.DoesNotExist:
        c = Course()
        c.name = form.name.data
        c.semester = form.semester.data
        c.gradeBook = GradeBook()
        c.save()

        page = Page()
        page.initializePerms()
        page.perm['anyView'] = True
        page.title = "Home"
        page.course = c
        page.save()

        c.homepage = url_for('viewPage', pgid=page.id)
        c.save()

        #Create the file backing
        ensurePathExists(getCoursePath(c))
        for admin in User.objects.filter(isAdmin=True):
          admin.courseInstructor.append(c)
          admin.save()
      return redirect(url_for('adminCourses'))
  return render_template('admin/courses.html', form=CreateCourseForm(), active_page="courses", courses=Course.objects)
def submitComment():
  if request.method == 'POST':
    form = FeedbackForm(request.form)
    if form.validate():
      ensurePathExists(getCommentPath())
      time = datetime.datetime.utcnow()
      filename = hashlib.md5(current_user.username).hexdigest()\
                  + time.isoformat() + ".txt"
      with open(os.path.join(getCommentPath(), filename), 'w') as f:
        if form.useName.data:
          f.write("User: "******"\n")
        f.write("Time: " + time.isoformat() + "\n")
        f.write("Comment:\n" + form.comment.data + "\n")

      flash("Your comment has been submitted", "success")

  return redirect(url_for('writeComment'))
Esempio n. 5
0
def uploadImage(pgid):
  try:
    page = Page.objects.get(id=pgid)
    if request.method == 'POST':
      form = PageImageForm(request.form)
      if form.validate():
        file = request.files.getlist('photo')[0]
        ensurePathExists(getPagePhotoDir(page))
        photoName = secure_filename(file.filename)
        file.save(os.path.join(getPagePhotoPath(page, photoName)))
        if not photoName in page.images:
          page.images.append(photoName)
          page.save()
    return redirect(url_for('editPage', pgid=pgid))

  except Page.DoesNotExist:
    abort(404)
Esempio n. 6
0
def submitComment():
    if request.method == 'POST':
        form = FeedbackForm(request.form)
        if form.validate():
            ensurePathExists(getCommentPath())
            time = datetime.datetime.utcnow()
            filename = hashlib.md5(current_user.username).hexdigest()\
                        + time.isoformat() + ".txt"
            with open(os.path.join(getCommentPath(), filename), 'w') as f:
                if form.useName.data:
                    f.write("User: "******"\n")
                f.write("Time: " + time.isoformat() + "\n")
                f.write("Comment:\n" + form.comment.data + "\n")

            flash("Your comment has been submitted", "success")

    return redirect(url_for('writeComment'))
Esempio n. 7
0
def adminCourses():
    '''
  Function Type: View Function
  Template: admin/courses.html
  Purpose: Display all courses in the system and facilitate the creation of new
  courses.

  Inputs: None

  Template Parameters:
    active_page: A string for highlighting the active page in the nav-bar.
    form: A CreateCourseForm that is used to allow a user to input new course
    information.

  Forms Handled:
    CreateCourseForm: Validates the form and creates a new course with the
    specified name and semester.
  '''
    #even though we require login if someone gets here and is not admin
    #send them away. This is done in all methods for the admin panel
    if not g.user.isAdmin:
        return redirect(url_for('index'))

    if request.method == "POST":
        form = CreateCourseForm(request.form)
        if form.validate():
            #create a new course
            #TODO: Validate that a course with this name and semester doesn't already
            #exist
            try:
                c = Course.objects.get(name=form.name.data,
                                       semester=form.semester.data)
                flash("A course with this name and semester already exists",
                      "warning")
            except Course.DoesNotExist:
                c = Course()
                c.name = form.name.data
                c.semester = form.semester.data
                c.gradeBook = GradeBook()
                c.save()

                page = Page()
                page.initializePerms()
                page.perm['anyView'] = True
                page.title = "Home"
                page.course = c
                page.save()

                # c.homepage = url_for('viewPage', pgid=page.id)
                c.save()

                #Create the file backing
                ensurePathExists(getCoursePath(c))
                for admin in User.objects.filter(isAdmin=True):
                    admin.courseInstructor.append(c)
                    admin.save()
            return redirect(url_for('adminCourses'))
    return render_template('admin/courses.html',
                           form=CreateCourseForm(),
                           active_page="courses",
                           courses=Course.objects)