Exemplo n.º 1
0
def new():
    try:
        users = User.query.filter(User.is_active == True).all()
        form = Form_Record_Add(request.form)
        sections = Section.query.filter(Section.is_active == True).all()
        items = Item.query.filter(Item.is_active == True).all()

        if request.method == 'POST':
            if form.validate():
                sections = Section()

                sanitize_form = {
                    'slug': form.slug.data,
                    'parent': form.parent.data,
                    'title_en_US': form.title_en_US.data,
                    'title_fr_FR': form.title_fr_FR.data,
                    'description_en_US': form.description_en_US.data,
                    'description_fr_FR': form.description_fr_FR.data,
                    'users': form.users.data,
                    'items': form.items.data,
                    'is_active': form.is_active.data
                }

                Section().create_data(sanitize_form)
                logger.info("Adding a new record.")

                if request_wants_json():
                    return jsonify(data={
                        message: "Record added successfully.",
                        form: form
                    }), 200, {
                        'Content-Type': 'application/json'
                    }
                else:
                    flash("Record added successfully.", category="success")
                    return redirect("/sections")

        form.action = url_for('sections_page.new')

        # html or Json response
        if request_wants_json():
            return jsonify(data=form), 200, {
                'Content-Type': 'application/json'
            }
        else:
            return render_template("sections/edit.html",
                                   form=form,
                                   sections=sections,
                                   items=items,
                                   users=users,
                                   title_en_US='New',
                                   app=app)
    except Exception, ex:
        print("------------ ERROR  ------------\n" + str(ex.message))
        flash(str(ex.message), category="warning")
        abort(404)
Exemplo n.º 2
0
    def post(self, request):
        response_data = {'retCode': error_constants.ERR_STATUS_SUCCESS[0],
                         'retMsg': error_constants.ERR_STATUS_SUCCESS[1]}
        try:
            road_id = int(request.POST.get('roadId', 0))
            district_id = int(request.POST.get('districtId'))
            name = request.POST.get('name')
            start_place = request.POST.get('startPlace')
            end_place = request.POST.get('endPlace')
            xy_coordinate = request.POST.get('XYCOORDINATE', '')
            channel = request.POST.get('channel', '')
            call_sign = request.POST.get('callSign', '')
            remark_1 = request.POST.get('remark1', '')
            remark_2 = request.POST.get('remark2', '')
            remark_3 = request.POST.get('remark3', '')
        except Exception as ex:
            print 'function name: ', __name__
            print Exception, ":", ex
            return generate_error_response(error_constants.ERR_INVALID_PARAMETER, status.HTTP_400_BAD_REQUEST)

        if road_id:
            cur_section = Section(name=name, start_place=start_place, end_place=end_place,
                                                 xy_coordinate=xy_coordinate, road_id=road_id, channel=channel,
                                                 call_sign=call_sign, remark1=remark_1, remark2=remark_2,
                                                 remark3=remark_3, district_id=Road.objects.get(id=road_id).district_id)

            try:
                with transaction.atomic():
                    cur_section.save()
            except Exception as ex:
                print 'function name: ', __name__
                print Exception, ":", ex
                return generate_error_response(error_constants.ERR_SAVE_INFO_FAIL,
                                               status.HTTP_500_INTERNAL_SERVER_ERROR)
            update_road_section_ids(road_id, cur_section.id, True)
        else:
            cur_section = Section(name=name, start_place=start_place,
                                                 end_place=end_place, xy_coordinate=xy_coordinate,
                                                 remark1=remark_1, channel=channel, call_sign=call_sign,
                                                 remark2=remark_2, remark3=remark_3, district_id=district_id)
            try:
                with transaction.atomic():
                    cur_section.save()
            except Exception as ex:
                print 'function name: ', __name__
                print Exception, ":", ex
                return generate_error_response(error_constants.ERR_SAVE_INFO_FAIL,
                                               status.HTTP_500_INTERNAL_SERVER_ERROR)
        return Response(response_data, status=status.HTTP_200_OK)
Exemplo n.º 3
0
def section_save(section_key_us=None):
    form = SectionFormAdmin()

    # form validation
    if not form.validate_on_submit():
        return "VALIDATION_ERROR", 400

    # get and validate parent section
    if form.parent_section.data == '/':
        parent_section_key = None
    else:
        parent_section = ndb.Key(
            urlsafe=form.parent_section.data).get() or abort(404)
        parent_section_key = parent_section.key

    # section instance
    if section_key_us:
        # edit
        # print 'admin section save EDIT'
        section = ndb.Key(urlsafe=section_key_us).get() or abort(404)
    else:
        # new
        # print 'admin section save NEW'
        section = Section()

    # update data
    g.cache.clear()
    section.parent_section = parent_section_key
    section.set_name(form.name.data, g.language)
    section.put()
    return 'ok'
Exemplo n.º 4
0
def eval(ast, document, last_create=None):
    head, *tail = ast

    if head[0] == 'section':
        section = Section(head[1].rstrip())
        document.sections.append(section)
        if tail:
            eval(tail, document, 'section')
    elif head[0] == 'subsection':
        subsection = Subsection(head[1].rstrip())
        document.sections[-1].subsections.append(subsection)
        if tail:
            eval(tail, document, 'subsection')
    elif head[0] == 'attr':
        value = tail[0][1].replace('{', "")
        value = value.replace('}', "")
        value = value.replace('\t', "")
        attr = ({head[1]: (value).rstrip()})
        if last_create == 'section':
            document.sections[-1].attrs.append(attr)
        else:
            document.sections[-1].subsections[-1].attrs.append(attr)
    else:
        eval(head, document, last_create)
        if tail:
            eval(tail, document, last_create)
Exemplo n.º 5
0
def destroy(id=1):
    try:
        sections = Section()
        section = sections.query.get_or_404(id)

        if section.children:
            # html or Json response
            if request_wants_json():
                return jsonify(data={
                    message:
                    "Unauthorized : Must delete child's section' first"
                }), 422, {
                    'Content-Type': 'application/json'
                }
            else:
                flash("Unauthorized : Must delete child's section first.",
                      category="danger")
                return redirect(url_for('sections_page.index'))

        else:
            sections.destroy_data(section.id)
            # html or Json response
            if request_wants_json():
                return jsonify(data={
                    message: "Record deleted successfully.",
                    section: m_section
                })
            else:
                flash("Record deleted successfully.", category="success")
                return redirect(url_for('sections_page.index'))

    except Exception, ex:
        print("------------ ERROR  ------------\n" + str(ex.message))
        flash(str(ex.message), category="warning")
        abort(404)
Exemplo n.º 6
0
def get_all_sections():
    """Get all sections"""
    return {
        "sections": [
            Section(id=ind, name=section, slug=slug)
            for ind, (slug, section) in enumerate(slug_to_section.items())
        ]
    }
Exemplo n.º 7
0
    def import_sections(sheet_url):
        index = read_spreadsheet(course="cs61a", url=sheet_url, sheet_name="Index")
        header = index[0][:4]
        if header != ["Day", "Start Time", "End Time", "Sheet"]:
            raise Failure("Invalid header for index sheet")
        for day, start_time, end_time, sheet, *args in index[1:]:
            sheet: List[List[Union[str, int]]] = read_spreadsheet(
                course="cs61a", url=sheet_url, sheet_name=repr(sheet)
            )
            header = sheet[0]
            name_col = header.index("Name")
            email_col = header.index("Email")

            day = list(calendar.day_name).index(day)

            start_time = datetime.strptime(start_time, "%I:%M %p")
            start_time = start_time.replace(
                year=datetime.now().year,
                month=datetime.now().month,
                day=datetime.now().day,
            )
            while start_time.weekday() != day:
                start_time += timedelta(days=1)

            end_time = datetime.strptime(end_time, "%I:%M %p")
            end_time = end_time.replace(
                year=start_time.year, month=start_time.month, day=start_time.day
            )

            pst = pytz.timezone("US/Pacific")
            start_time = pst.localize(start_time).timestamp()
            end_time = pst.localize(end_time).timestamp()

            for section in sheet[1:]:
                tutor_name = section[name_col]
                tutor_email = section[email_col]
                if not tutor_name or not tutor_email:
                    continue
                tutor_user = User.query.filter_by(email=tutor_email).one_or_none()
                if tutor_user is None:
                    tutor_user = User(email=tutor_email, name=tutor_name, is_staff=True)
                    db.session.add(tutor_user)
                tutor_user.is_staff = True

                capacity = sum(1 for col in header if "Student" in col)

                db.session.add(
                    Section(
                        start_time=start_time,
                        end_time=end_time,
                        capacity=capacity,
                        staff=tutor_user,
                    )
                )

            db.session.commit()

        return refresh_state()
Exemplo n.º 8
0
def add_section():

    if request.method == "POST":
        title = request.form['title']
        description = request.form['description']
        new_section = Section(title=title, description=description)
        db.session.add(new_section)
        db.session.commit()
        return redirect(url_for('admin_bag'))

    return render_template('add_section.html')
Exemplo n.º 9
0
def addsection():
    if not g.user:  # not logged in
        return redirect(url_for('home'))
    desc = request.form['description']
    if desc == '':
        flash('error: must enter a title')
    else:
        db.session.add(Section(desc, session['user_id'],
                               request.form['color']))
        db.session.commit()
        flash('added section')
    return redirect(url_for('sections'))
Exemplo n.º 10
0
def show(id=1):
    try:
        m_sections = Section()
        m_section = m_sections.read_data(id)
        # html or Json response
        if request_wants_json():
            return jsonify(data=m_section)
        else:
            return render_template("sections/show.html",
                                   section=m_section,
                                   app=app)

    except Exception, ex:
        print("------------ ERROR  ------------\n" + str(ex.message))
        flash(str(ex.message), category="warning")
        abort(404)
Exemplo n.º 11
0
 def post(self, request):
     response_data = {'retCode': error_constants.ERR_STATUS_SUCCESS[0],
                      'retMsg': error_constants.ERR_STATUS_SUCCESS[1]}
     try:
         section_id = int(request.POST.get('sectionId'))
         name = request.POST.get('name')
         start_place = request.POST.get('startPlace')
         end_place = request.POST.get('endPlace')
         xy_coordinate = request.POST.get('XYCOORDINATE', '')
         channel = request.POST.get('channel', '')
         call_sign = request.POST.get('callSign', '')
         remark_1 = request.POST.get('remark1', '')
         remark_2 = request.POST.get('remark2', '')
         remark_3 = request.POST.get('remark3', '')
     except Exception as ex:
         print 'function name: ', __name__
         print Exception, ":", ex
         return generate_error_response(error_constants.ERR_INVALID_PARAMETER, status.HTTP_400_BAD_REQUEST)
     cur_section = Section.objects.get(id=section_id)
     district_id = cur_section.district_id
     new_section = Section(name=name, start_place=start_place, channel=channel, call_sign=call_sign,
                                          end_place=end_place, xy_coordinate=xy_coordinate,
                                          remark1=remark_1, remark2=remark_2, remark3=remark_3,
                                          district_id=district_id)
     try:
         with transaction.atomic():
             new_section.save()
     except Exception as ex:
         print 'function name: ', __name__
         print Exception, ":", ex
         return generate_error_response(error_constants.ERR_SAVE_INFO_FAIL,
                                        status.HTTP_500_INTERNAL_SERVER_ERROR)
     chief = cur_section.chief.all()
     bureau = cur_section.exec_chief_sub_bureau.all()
     trans = cur_section.exec_chief_trans.all()
     arm_poli = cur_section.exec_chief_armed_poli.all()
     for item in chief:
         new_section.chief.add(item)
     for item in bureau:
         new_section.exec_chief_sub_bureau.add(item)
     for item in trans:
         new_section.exec_chief_trans.add(item)
     for item in arm_poli:
         new_section.exec_chief_armed_poli.add(item)
     # 复制段时将岗及人员全部复制关联
     copy_station_to_new_section(new_section, cur_section)
     return Response(response_data, status.HTTP_200_OK)
Exemplo n.º 12
0
def sections(request):
    if request.method == 'POST':
        # save new post
        heading = request.POST['heading']
        content = request.POST['content']

        section = Section(heading=heading)
        section.heading = heading
        #       section.content = cgi.escape(content).encode("ascii", "xmlcharrefreplace")
        section.content = content
        section.save()

    # Get all sections from DB
    sections = Section.objects

    return render_to_response('admin/index.html', {'Sections': sections},
                              context_instance=RequestContext(request))
Exemplo n.º 13
0
    def restore_object(self, attrs, instance=None):
        classgroup = attrs.get('classgroup')
        name = attrs.get('name')

        user = self.context['request'].user

        if instance is None:
            instance = Section(classgroup=classgroup,
                               name=alphanumeric_name(name),
                               display_name=name)
            instance.save()
        else:
            if not ClassGroupPermissions.is_teacher(classgroup, user):
                raise serializers.ValidationError(
                    "You do not have permission to modify this section.")
            instance.name = alphanumeric_name(name)
            instance.display_name = name
        return instance
Exemplo n.º 14
0
def index(page=1):
    try:
        m_sections = Section()
        list_sections = m_sections.all_data(page,
                                            app.config['LISTINGS_PER_PAGE'])
        # html or Json response
        if request_wants_json():
            return jsonify([{
                'id': d.id,
                'title_en_US': d.title_en_US,
                'description_en_US': d.description_en_US,
                'title_fr_FR': d.title_fr_FR,
                'description_fr_FR': d.description_fr_FR
            } for d in list_sections.items])
        else:
            return render_template("sections/index.html",
                                   list_sections=list_sections,
                                   app=app)

    except Exception, ex:
        print("------------ ERROR  ------------\n" + str(ex.message))
def create_sections_for_programs(programs, activities):
    for prog in programs:
        for i in range(programs[prog]['num_sections']):
            section_id = generate_random_uuid()
            section_name = prog + " section " + str(i)
            section = Section(section_id=section_id,
                              name=section_name,
                              description=section_name,
                              overview_image=section_name + ".png")
            add_and_commit(section)

            prog_section_mapping = ProgramSectionMapping(
                mapping_id=generate_random_uuid(),
                program_id=programs[prog]['program_id'],
                section_id=section_id,
                order_index=i + 1)
            add_and_commit(prog_section_mapping)

            for activity_id in activities:
                section_activity_mapping = SectionActivityMapping(
                    mapping_id=generate_random_uuid(),
                    section_id=section_id,
                    activity_id=activity_id)
Exemplo n.º 16
0
def manage_sections(request):
    """
    Render the page where instructors can go to make a new section or
    edit existing ones.
    """

    if request.method == "POST":
        # Class is being submitted
        form = NewSectionForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data["name"]
            new_section = Section(name=name)
            new_section.save()
            return HttpResponseRedirect("/managesection/")

    form = NewSectionForm()
    sections = Section.objects.all()
    context = {
        "form": form,
        "sections": sections,
        "listSize": sections.count()
    }
    return render(request, "managesection.html", context)
Exemplo n.º 17
0
    def import_sections(sheet_url):
        index = read_spreadsheet(course="cs61a",
                                 url=sheet_url,
                                 sheet_name="Index")
        header = index[0][:7]
        if header != [
                "ID",
                "Day",
                "Start Time",
                "End Time",
                "Staff Name",
                "Staff Email",
                "Label",
        ]:
            raise Failure("Invalid header for index sheet")
        for (
                id,
                day,
                start_time,
                end_time,
                staff_name,
                staff_email,
                label,
                *args,
        ) in index[1:]:
            day = list(calendar.day_name).index(day)

            start_time = datetime.strptime(start_time, "%I:%M %p")
            start_time = start_time.replace(
                year=datetime.now().year,
                month=datetime.now().month,
                day=datetime.now().day,
            )
            while start_time.weekday() != day:
                start_time += timedelta(days=1)

            end_time = datetime.strptime(end_time, "%I:%M %p")
            end_time = end_time.replace(year=start_time.year,
                                        month=start_time.month,
                                        day=start_time.day)

            pst = pytz.timezone("US/Pacific")
            start_time = pst.localize(start_time).timestamp()
            end_time = pst.localize(end_time).timestamp()

            staff_user = User.query.filter_by(email=staff_email).one_or_none()
            if staff_user is None:
                staff_user = User(email=staff_email,
                                  name=staff_name,
                                  is_staff=True)
                db.session.add(staff_user)
            staff_user.is_staff = True

            sheet: List[List[Union[str, int]]] = read_spreadsheet(
                course="cs61a", url=sheet_url, sheet_name=str(id))
            header = sheet[0]
            email_col = header.index("Email")

            students = [student[email_col] for student in sheet[1:]]
            capacity = len(students) * 2

            section = Section.query.filter_by(id=id).one_or_none()
            if not section:
                section = Section(
                    start_time=start_time,
                    end_time=end_time,
                    capacity=capacity,
                    staff=staff_user,
                    tag_string=label,
                )
                db.session.add(section)

            for student in students:
                user = User.query.filter_by(email=student).one_or_none()
                if not user:
                    user = User(email=student, name=student, is_staff=False)
                    db.session.add(user)
                user.sections = [section]
                user.is_staff = False

            db.session.commit()
        return refresh_state()
Exemplo n.º 18
0
    def naive_parser(cls, plaintext):
        """

        First categorize lines
        - detect via short words with longer spaces = chord_line
        - detect via known chord words

        Then create verses
        - detect via enumeration prefixes `1.` + `R:`
        - detect via empty line

        Then analyze lines to assing chord position.
        """
        store = LineStore.from_plaintext(plaintext)

        if store.first_line_is_empty:
            raise RuntimeError("Naive parser error - first line is empty")
        # first line has to be chords in this naive approach
        if store.first_line_is_chord:
            raise RuntimeError("Naive parser error - first line not chords")

        # check if the line categories are zigzag
        if not store.lines_are_zigzag:
            raise RuntimeError("Naive parser error - line categories not zigzag")

        print("line categories are switching per each line, yay")

        # generate verse_lines
        sections = []
        verse_lines = []
        line_pairs = store.get_line_pairs()
        for i, line_pair in enumerate(line_pairs):
            chord_line, text_line = line_pair
            verse_line = VerseLineFactory.from_plaintext(chord_line.line, text_line.line)
            verse_lines.append(verse_line)
            verse_code = text_line.verse_code

            save_section = False
            if i+1 < len(line_pairs):
                next_line = line_pairs[i+1]
                next_line_is_new_verse = next_line[1].is_leading_line
                if next_line_is_new_verse:
                    save_section = True
            else:
                # this is last line_pair
                save_section = True

            if save_section:
                section = Section(
                    category=SectionCategoryFactory.from_verse_code(verse_code),
                    verse_code=verse_code,
                    lines=verse_lines,
                )
                sections.append(section)

                # empty the list to store only verse_lines of the next verse
                verse_lines = []


        song = Song(
            format_version=SongFormatVersion.v0_1_0,
            name="noha_v_dumku.lorem",
            slug="noha_v_dumku.lorem",
            author="SCRAPER",
            language="cz",
            sections=sections
        )
        return song
Exemplo n.º 19
0
    def test_exam_endpoints(self):
        user = rand.user()
        course = Course(name='World Lit')
        section1 = Section(name='Hour 1')
        section2 = Section(name='Hour 2')
        course.sections = [section1, section2]
        exam1 = Exam(name='Exam 1', exam_format='DDD..EEDD')
        exam2 = Exam(name='Exam 2', exam_format='..FFFFFFFFFFFF')
        course.exams = [exam1, exam2]
        user.courses.append(course)
        db.session.add(course)
        db.session.add_all([user, section1, section2, exam1, exam2])
        db.session.commit()
        db.session.refresh(course)
        db.session.refresh(exam1)

        ##############################################
        # Verify that we have two exams in this course
        exams_list_response = self.client.get(
            '/api/course/{course_id}/exams'.format(course_id=course.id),
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        self.assertEqual(exams_list_response.status_code, HTTPStatus.OK)
        exam_list_data = json.loads(exams_list_response.data.decode())
        self.assertEqual(len(exam_list_data['exams']), 2)

        ###############################################
        # Verify that a specific exam has the correct name and format
        self.assertIn('Exam 1',
                      [exam['name'] for exam in exam_list_data['exams']])
        self.assertNotIn('Exam Foo',
                         [exam['name'] for exam in exam_list_data['exams']])
        self.assertIn(
            'DDD..EEDD',
            [exam['exam_format'] for exam in exam_list_data['exams']])

        ###############################################
        # Add an Exam
        new_exam = Exam(name='Exam 3', exam_format='DDDDD')
        new_exam_request_response = self.client.post(
            '/api/course/{course_id}/exam/add'.format(course_id=course.id),
            data=json.dumps(new_exam.toJSON()),
            content_type='application/json',
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        self.assertEqual(new_exam_request_response.status_code, HTTPStatus.OK)
        exams_list_response = self.client.get(
            '/api/course/{course_id}/exams'.format(course_id=course.id),
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        exam_list_data = json.loads(exams_list_response.data.decode())
        # we now have 3 exams
        self.assertEqual(len(exam_list_data['exams']), 3)
        new_exam_id = json.loads(
            new_exam_request_response.data.decode())['exam']['id']

        ###############################################
        # Edit an exam
        update_exam_response = self.client.post(
            '/api/course/{course_id}/exam/{exam_id}/update'.format(
                course_id=course.id, exam_id=new_exam_id),
            data=json.dumps({
                'name': 'Exam 4',
                'exam_format': 'CCC'
            }),
            content_type='application/json',
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        self.assertEqual(update_exam_response.status_code, HTTPStatus.OK)
        update_exam_response_data = json.loads(
            update_exam_response.data.decode())
        self.assertEqual('CCC',
                         update_exam_response_data['exam']['exam_format'])
        self.assertEqual('Exam 4', update_exam_response_data['exam']['name'])

        ###############################################
        # Delete an Exam
        delete_exam_response = self.client.delete(
            '/api/course/{course_id}/exam/{exam_id}'.format(
                course_id=course.id, exam_id=new_exam_id),
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        exams_list_response = self.client.get(
            '/api/course/{course_id}/exams'.format(course_id=course.id),
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        exam_list_data = json.loads(exams_list_response.data.decode())
        # we should be back to two exams
        self.assertEqual(len(exam_list_data['exams']), 2)

        ###############################################
        ###############################################
        # Create three student exams
        for answers in ['DDDTTEEDD', 'ABCTFDEAC', 'DCBFFEAAA']:
            create_student_exam_response = self.client.post(
                '/api/course/{course_id}/exam/{exam_id}/student_exam'.format(
                    course_id=course.id, exam_id=exam1.id),
                data=json.dumps({'answers': answers}),
                content_type='application/json',
                headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        exam1 = Exam.query.get(
            exam1.id
        )  # 'db.session.refresh' doesn't work here, as we're on to a different db session
        self.assertEqual(len(exam1.student_exams), 3)
        exam_response = self.client.get(
            '/api/course/{course_id}/exam/{exam_id}'.format(
                course_id=course.id, exam_id=exam1.id),
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        exam_response_data = json.loads(exam_response.data.decode())
        self.assertEqual(len(exam_response_data['exam']['student_exams']), 3)

        ###############################################
        # Update one of those student exams
        student_exam_id = exam_response_data['exam']['student_exams'][0]['id']
        self.client.post(
            '/api/course/{course_id}/exam/{exam_id}/student_exam/{student_exam_id}'
            .format(course_id=course.id,
                    exam_id=exam1.id,
                    student_exam_id=student_exam_id),
            data=json.dumps({'answers': 'AAAFFAAAA'}),
            content_type='application/json',
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        student_exam = StudentExam.query.get(student_exam_id)
        self.assertEqual(student_exam.answers, 'AAAFFAAAA')

        ###############################################
        # Delete a student exam
        delete_student_exam_response = self.client.delete(
            '/api/course/{course_id}/exam/{exam_id}/student_exam/{student_exam_id}'
            .format(course_id=course.id,
                    exam_id=exam1.id,
                    student_exam_id=student_exam_id),
            content_type='application/json',
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        exam1 = Exam.query.get(exam1.id)
        self.assertEqual(len(exam1.student_exams), 2)
Exemplo n.º 20
0
    for row in reader[1:]:
        name, email, time, group, is_npe = row
        is_npe = is_npe == "TRUE"
        hour, mins = time.split(":")
        hour = int(hour)
        mins = int(mins)
        start_time = pst.localize(
            datetime(year=2020, month=8, day=26, hour=hour + 12, minute=mins))
        end_time = start_time + timedelta(minutes=25)

        if email not in users:
            users[email] = User(email=email, name=name, is_staff=True)

        section = Section(
            start_time=start_time.timestamp(),
            end_time=end_time.timestamp(),
            capacity=5,
            staff=users[email],
        )

        if is_npe:
            section.tags = ["NPE"]

        lookup[time, group] = section

        db.session.add(section)

    with open("student_tutorials.csv") as f:
        reader = list(csv.reader(f))

        for row in reader[1:]:
            name, email, time, group = row
Exemplo n.º 21
0
 def section(self):
     return Section(name=self.letters())
Exemplo n.º 22
0
def submit2():
    #Checks if form1data has been submitted before accessing this page
    if not session['form1data']:
        return redirect(url_for('submit'))
    #Session variable back is decremented
    session['back'] = session['back'] - 1
    form = SubmitForm2()
    form1data = json.loads(session['form1data'])
    url_or_file = form1data['url_question']
    #Get number of sections
    sections = int(form1data['section'])
    #Error variables
    url_errors = []
    file_errors = []
    section_errors = []
    pdf_errors = []
    status_errors = []
    if form.validate_on_submit():
        #Loops through inputs of form
        for input in request.form:
            #checks if inputs are empty and adds ids to the corresponding error list
            if request.form[input] == '':
                check = input.split('_')
                if check[0] == 'url':
                    url_errors.append(int(check[1]))
                elif check[0] == 'section':
                    section_errors.append(int(check[1]))
                elif check[0] == 'file':
                    file_errors.append(int(check[1]))
            else:
                if url_or_file == 'Yes':
                    if 'url' in input:
                        #checks if url is pdf link
                        url = request.form[input]
                        try:
                            parsed_url = urlparse(url)
                            #checks if the user input has a schema
                            if not parsed_url.scheme:
                                url = "http://" + url
                            proxies = {
                                "http":
                                "http://cscisa.csc.nycnet:8080/array.dll?Get.Routing.Script",
                                "https":
                                "http://cscisa.csc.nycnet:8080/array.dll?Get.Routing.Script"
                            }
                            print "requesting: " + url
                            r = requests.get(url_fix(url),
                                             proxies=proxies,
                                             timeout=1)
                            if r.status_code == 404:
                                status_errors.append(int(input.split('_')[1]))
                            if r.headers['content-type'] != 'application/pdf':
                                pdf_errors.append(int(input.split('_')[1]))
                        #if requesting the website fails; do regular expression checking
                        except:
                            match = re.search('[\w%+\/-].pdf',
                                              request.form[input])
                            if not match:
                                pdf_errors.append(int(input.split('_')[1]))
        #loops through the files inputted
        for file_ in request.files:
            file_id = file_.split('_')[1]
            #no files inputted add to files errors list
            if request.files[file_].filename == '':
                file_errors.append(int(file_id))
            #check is the files one of the allowed files ext
            if not allowed_file(request.files[file_].filename):
                pdf_errors.append(int(file_id))
        #if errors respond with errors
        if pdf_errors or section_errors or url_errors or status_errors or file_errors:
            pass
        else:
            #Urls processing
            if url_or_file == 'Yes':
                if sections == 1:
                    url = request.form.get('url_1')
                    parsed_url = urlparse(url)
                    #check schema
                    if not parsed_url.scheme:
                        url = url_fix("http://" + url)
                    download_url = URL(url)
                    date_created = datetime.date(int(form1data['year']),
                                                 int(form1data['month']),
                                                 int(form1data['day']))
                    doc = Document(title=form1data['title'],
                                   type=form1data['type'],
                                   description=form1data['description'],
                                   dateCreated=date_created,
                                   agency=session['agency'],
                                   category=form1data['category'],
                                   doc_url=url)
                    db.session.add(doc)
                    #add document meta data to datebase
                    db.session.commit()
                    #get max common id
                    did = db.session.query(func.max(Document.id)).scalar()
                    doc = Document.query.get(did)
                    filename = str(did) + '_' + form1data['title']
                    doc.filename = filename
                    #download file into upload folder
                    try:
                        f = open(
                            os.path.join(app.config['UPLOAD_FOLDER'],
                                         filename + ".pdf"), 'wb')
                        if urlparse(url).scheme == "https://":
                            f.write(
                                download_url.download(
                                    cached=False,
                                    proxy=
                                    ("http://cscisa.csc.nycnet:8080/array.dll?Get.Routing.Script",
                                     'https')))
                        else:
                            f.write(
                                download_url.download(
                                    cached=False,
                                    proxy=
                                    ("http://cscisa.csc.nycnet:8080/array.dll?Get.Routing.Script",
                                     'http')))
                        f.close()
                        #hardcopy set to yes if downloaded
                        doc.hardcopy = "yes"
                        doc.path = os.path.join(app.config['DOC_PATH'],
                                                filename)
                    #NOTE: Script must be written for documents that have hardcopy set to no; to download file
                    except:
                        pass
                    sub = Submit(did=did, uid=session['uid'])
                    db.session.add(sub)
                    db.session.commit()
                    #if more that one sections do samething above for each section
                elif sections > 1:
                    #gets the common id to be inserted by querying the max common id + 1
                    common_id = db.session.query(func.max(
                        Document.common_id)).scalar()
                    if not common_id:
                        common_id = 1
                    else:
                        common_id = common_id + 1
                    for doc in range(1, (sections + 1)):
                        url = 'url_' + str(doc)
                        url = request.form.get(url)
                        parsed_url = urlparse(url)
                        if not parsed_url.scheme:
                            url = url_fix("http://" + url)
                        download_url = URL(url)
                        sectionid = 'section_' + str(doc)
                        section = request.form.get(sectionid)
                        date_created = datetime.date(int(form1data['year']),
                                                     int(form1data['month']),
                                                     int(form1data['day']))
                        doc = Document(title=form1data['title'],
                                       type=form1data['type'],
                                       description=form1data['description'],
                                       dateCreated=date_created,
                                       agency=session['agency'],
                                       category=form1data['category'],
                                       doc_url=url,
                                       common_id=common_id,
                                       section_id=doc)
                        db.session.add(doc)
                        db.session.commit()
                        did = db.session.query(func.max(Document.id)).scalar()
                        doc = Document.query.get(did)
                        filename = str(did) + '_' + form1data['title']
                        doc.filename = filename
                        try:
                            f = open(
                                os.path.join(app.config['UPLOAD_FOLDER'],
                                             filename + ".pdf"), 'wb')
                            if urlparse(url).scheme == "https://":
                                f.write(
                                    download_url.download(
                                        cached=False,
                                        proxy=
                                        ("http://cscisa.csc.nycnet:8080/array.dll?Get.Routing.Script",
                                         'https')))
                            else:
                                f.write(
                                    download_url.download(
                                        cached=False,
                                        proxy=
                                        ("http://cscisa.csc.nycnet:8080/array.dll?Get.Routing.Script",
                                         'http')))
                            f.close()
                            doc.hardcopy = "yes"
                            doc.path = os.path.join(app.config['DOC_PATH'],
                                                    filename)
                        except:
                            pass
                        sub = Submit(did=did, uid=session['uid'])
                        sec = Section(did=did, section=section)
                        db.session.add(sec)
                        db.session.add(sub)
                        db.session.commit()
            #if the user clicked files
            elif url_or_file == 'No':
                if sections == 1:
                    file = request.files['file_1']
                    if file:
                        date_created = datetime.date(int(form1data['year']),
                                                     int(form1data['month']),
                                                     int(form1data['day']))
                        doc = Document(title=form1data['title'],
                                       type=form1data['type'],
                                       description=form1data['description'],
                                       dateCreated=date_created,
                                       agency=session['agency'],
                                       category=form1data['category'])
                        db.session.add(doc)
                        db.session.commit()
                        did = db.session.query(func.max(Document.id)).scalar()
                        doc = Document.query.get(did)
                        filename = str(did) + '_' + doc.title + ".pdf"
                        file.save(
                            os.path.join(app.config['UPLOAD_FOLDER'],
                                         filename))
                        doc.path = os.path.join(app.config['DOC_PATH'],
                                                filename)
                        doc.filename = filename
                        doc.hardcopy = 'yes'
                        sub = Submit(did=did, uid=session['uid'])
                        db.session.add(sub)
                        db.session.commit()
                elif sections > 1:
                    for doc in range(1, (sections + 1)):
                        fileid = "file_" + str(doc)
                        file = request.files[fileid]
                        if file:
                            date_created = datetime.date(
                                int(form1data['year']),
                                int(form1data['month']), int(form1data['day']))
                            doc = Document(
                                title=form1data['title'],
                                type=form1data['type'],
                                description=form1data['description'],
                                dateCreated=date_created,
                                agency=session['agency'],
                                category=form1data['category'])
                            sectionid = 'section_' + str(doc)
                            section = request.form.get(sectionid)
                            db.session.add(doc)
                            db.session.commit()
                            did = db.session.query(func.max(
                                Document.id)).scalar()
                            doc = Document.query.get(did)
                            filename = str(did) + '_' + doc.title + ".pdf"
                            file.save(
                                os.path.join(app.config['UPLOAD_FOLDER'],
                                             filename))
                            doc.filename = filename
                            doc.hardcopy = 'yes'
                            doc.path = os.path.join(app.config['DOC_PATH'],
                                                    filename)
                            sub = Submit(did=did, uid=session['uid'])
                            sec = Section(did=did, section=section)
                            db.session.add(sub)
                            db.session.add(sec)
                            db.session.commit()
            #set session variable confirm to be true so that comfirmation page is allowed through
            session['confirm'] = True
            return redirect(url_for('confirmation'))
    return render_template('submit2.html',
                           back=session['back'],
                           form=form,
                           submit2form=request.form,
                           submit2files=request.files,
                           sections=int(sections),
                           url_or_file=url_or_file,
                           url_errors=url_errors,
                           section_errors=section_errors,
                           status_errors=status_errors,
                           pdf_errors=pdf_errors,
                           file_errors=file_errors)
Exemplo n.º 23
0
    def test_course_endpoints(self):
        """ Create a course, add some sections, delete some things, and rename some things """
        ########################################################
        # Set up a user and a course
        user = rand.user()
        course = Course(name='US History')
        section1 = Section(name='Hour 1')
        section2 = Section(name='Hour 2')
        course.sections = [section1, section2]
        user.courses.append(course)
        db.session.add_all([user, course, section1, section2])
        db.session.commit()

        token_request_json = {'email': user.email}

        ########################################################
        # Verify that we have a single course for this user
        course_list_response = self.client.get(
            '/api/course/',
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        course_list_data = json.loads(course_list_response.data.decode())
        self.assertEqual(len(course_list_data['courses']), 1)
        self.assertEqual(course_list_data['courses'][0]['name'], 'US History')

        course_id = course_list_data['courses'][0]['id']

        ########################################################
        # Create a new course and verify that we now have two
        new_course_json = {
            'name': 'World History',
            'sections': ['Hour 5', 'Hour 6', 'Hour 7', 'Study Hall']
        }

        course_add_response = self.client.post(
            '/api/course/add',
            headers={'Authorization': 'Bearer {}'.format(jwt_token)},
            content_type='application/json',
            data=json.dumps(new_course_json))

        self.assertEqual(course_add_response.status_code, HTTPStatus.OK)
        course_add_response_json = json.loads(
            course_add_response.data.decode())['course']
        new_course_id = course_add_response_json['id']
        self.assertEqual(len(course_add_response_json['sections']), 4)
        self.assertEqual(course_add_response_json['sections'][1]['name'],
                         'Hour 6')

        course_list_response = self.client.get(
            '/api/course/',
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        course_list_data = json.loads(course_list_response.data.decode())
        self.assertEqual(len(course_list_data['courses']), 2)

        ########################################################
        # Update the course we created
        update_course_json = {'name': 'Early World History'}

        course_update_response = self.client.post(
            '/api/course/update/{course_id}'.format(course_id=new_course_id),
            headers={'Authorization': 'Bearer {}'.format(jwt_token)},
            content_type='application/json',
            data=json.dumps(update_course_json))
        self.assertEqual(course_update_response.status_code, HTTPStatus.OK)

        get_updated_course_response = self.client.get(
            '/api/course/{course_id}'.format(course_id=new_course_id),
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        get_updated_course_response_json = json.loads(
            get_updated_course_response.data.decode())['course']
        self.assertEqual(get_updated_course_response.status_code, 200)
        self.assertEqual(get_updated_course_response_json['name'],
                         'Early World History')
        self.assertEqual(len(get_updated_course_response_json['sections']), 4)

        ########################################################
        # Add a new section to our newly created course
        new_section_json = {'name': 'New Section foo'}
        add_new_section_response = self.client.post(
            '/api/course/{course_id}/section/add'.format(
                course_id=new_course_id),
            headers={'Authorization': 'Bearer {}'.format(jwt_token)},
            content_type='application/json',
            data=json.dumps(new_section_json))
        self.assertEqual(add_new_section_response.status_code, HTTPStatus.OK)
        new_section_id = json.loads(
            add_new_section_response.data.decode())['section']['id']

        get_updated_course_response = self.client.get(
            '/api/course/{course_id}'.format(course_id=new_course_id),
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        get_updated_course_response_json = json.loads(
            get_updated_course_response.data.decode())['course']
        self.assertEqual(len(get_updated_course_response_json['sections']), 5)
        self.assertIn('New Section foo', [
            section['name']
            for section in get_updated_course_response_json['sections']
        ])

        ########################################################
        # Update the section we created
        update_section_json = {'name': 'Updated section name'}
        update_section_response = self.client.post(
            '/api/course/{course_id}/section/{section_id}/update'.format(
                course_id=new_course_id, section_id=new_section_id),
            headers={'Authorization': 'Bearer {}'.format(jwt_token)},
            content_type='application/json',
            data=json.dumps(update_section_json))
        self.assertEqual(add_new_section_response.status_code, HTTPStatus.OK)
        self.assertEqual(
            json.loads(update_section_response.data.decode())['section']['id'],
            new_section_id)
        get_updated_course_response = self.client.get(
            '/api/course/{course_id}'.format(course_id=new_course_id),
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        get_updated_course_response_json = json.loads(
            get_updated_course_response.data.decode())['course']
        self.assertIn('Updated section name', [
            section['name']
            for section in get_updated_course_response_json['sections']
        ])

        ########################################################
        # Delete the section we created
        delete_section_response = self.client.delete(
            '/api/course/{course_id}/section/{section_id}'.format(
                course_id=new_course_id, section_id=new_section_id),
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        self.assertEqual(delete_section_response.status_code, HTTPStatus.OK)
        get_updated_course_response = self.client.get(
            '/api/course/{course_id}'.format(course_id=new_course_id),
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        get_updated_course_response_json = json.loads(
            get_updated_course_response.data.decode())['course']
        self.assertEqual(len(get_updated_course_response_json['sections']), 4)
        self.assertNotIn('Updated section name', [
            section['name']
            for section in get_updated_course_response_json['sections']
        ])

        ########################################################
        # Delete our new course, assert we're back to one course
        delete_course_response = self.client.delete(
            '/api/course/{course_id}'.format(course_id=new_course_id),
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        self.assertEqual(delete_course_response.status_code, HTTPStatus.OK)
        course_list_response = self.client.get(
            '/api/course/',
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        course_list_data = json.loads(course_list_response.data.decode())
        self.assertEqual(len(course_list_data['courses']), 1)
        self.assertNotIn(
            'Early World History',
            [course['name'] for course in course_list_data['courses']])
Exemplo n.º 24
0
        hour = int(hour_str[:-2])
        if hour_str[-2:] == "PM" and hour != 12:
            hour += 12

        start_time = pst.localize(
            datetime(year=2021, month=1, day=day, hour=hour, minute=10))
        end_time = start_time + timedelta(minutes=50)

        if email not in users:
            users[email] = User(email=email, name=name, is_staff=True)

        section = Section(
            id=group,
            start_time=start_time.timestamp(),
            end_time=end_time.timestamp(),
            capacity=9,
            staff=users[email],
        )

        if is_npe:
            section.tags = ["NPE"]

        lookup[group] = section

        db.session.add(section)

    for row in student_reader[1:]:
        name, email, time, group, npe = row
        lookup[group].students.append(
            User(email=email, name=name, is_staff=False))
Exemplo n.º 25
0
    print([len(sections) for sections in sections_by_npe])

    for row in reader:
        if not row:
            break
        if row[0] != "FALSE":
            continue
        assert len(row) == 11
        npe = row[-2] == "TRUE"
        if row[-1] == "TRUE":
            print("Reassigning empty section to tutor: {}, NPE: {}".format(row[1], npe))
            section = sections_by_npe[npe].pop()
            section.staff = get_user(row[2], row[1])

        else:
            print("Creating new section with tutor: {}, NPE: {}".format(row[1], npe))

            section = Section(
                start_time=start_time.timestamp(),
                end_time=end_time.timestamp(),
                capacity=5,
                staff=get_user(row[2], row[1]),
            )

            if npe:
                section.tags = ["NPE"]

            db.session.add(section)

    db.session.commit()