Пример #1
0
	def TEMP(self):
		web.header('Content-type', 'text/plain')
		user_id = session.user().id
		f = web.input(file=None)['file'].split('/')
		f = f[-2] if len(f[-1]) == 0 else f[-1]
		if len(f)==0: f = None
		f = Files.get_file(f)
		return json.dumps({'name': f.name, 'body': f.body, 'id': f.id})
Пример #2
0
	def TEMP(self):
		web.header('Content-type', 'text/html')
		user_id = session.user().id
		dir_ = web.input(dir='/')['dir']
		parent_id = dir_.split('/')
		parent_id = parent_id[-2] if len(parent_id[-1]) == 0 else parent_id[-1]
		if len(parent_id)==0: parent_id = None
		dirs = Files.get_directories(user_id, parent_id)
		files = Files.get_files(user_id, parent_id)
		s = ['<ul class="jqueryFileTree" style="display: none;">']
		if dir_[-1] != '/': dir_+='/'
		for d in dirs:
			s.append('<li class="directory collapsed"><a href="#" rel="%s%d/">%s</a></li>' % (dir_, d.id, d.name))
		for f in files:
			s.append('<li class="file ext_txt"><a href="#" rel="%s%d">%s</a></li>' % (dir_, f.id, f.name))
		s.append('</ul>')
		return ' '.join(s)
Пример #3
0
	def POST(self):
		web.header('Content-type', 'text/plain')
		
		user_id = session.user().id

		sid = int(web.input(id='0')['id'], 10)
		sname = web.input(name='')['name']
		scode = web.input(code='')['code']

		return Files.save(user_id, sid, sname, scode)
Пример #4
0
def create_request(title,
                   description,
                   category,
                   tz_name,
                   agency_ein=None,
                   first_name=None,
                   last_name=None,
                   submission=DIRECT_INPUT,
                   agency_date_submitted=None,
                   email=None,
                   user_title=None,
                   organization=None,
                   phone=None,
                   fax=None,
                   address=None,
                   upload_path=None):
    """
    Creates a new FOIL Request and associated Users, UserRequests, and Events.

    :param title: request title
    :param description: detailed description of the request
    :param tz_name: client's timezone name
    :param agency_ein: agency_ein selected for the request
    :param first_name: first name of the requester
    :param last_name: last name of the requester
    :param submission: request submission method
    :param agency_date_submitted: submission date chosen by agency
    :param email: requester's email address
    :param user_title: requester's organizational title
    :param organization: requester's organization
    :param phone: requester's phone number
    :param fax: requester's fax number
    :param address: requester's mailing address
    :param upload_path: file path of the validated upload
    """
    # 1. Generate the request id
    request_id = generate_request_id(agency_ein)

    # 2a. Generate Email Notification Text for Agency
    # agency_email = generate_email_template('agency_acknowledgment.html', request_id=request_id)
    # 2b. Generate Email Notification Text for Requester

    # 3a. Send Email Notification Text for Agency
    # 3b. Send Email Notification Text for Requester

    # 4a. Calculate Request Submitted Date (Round to next business day)
    date_created = datetime.utcnow()
    date_submitted = (local_to_utc(agency_date_submitted, tz_name)
                      if current_user.is_agency else
                      get_following_date(date_created))

    # 4b. Calculate Request Due Date (month day year but time is always 5PM, 5 Days after submitted date)
    due_date = get_due_date(date_submitted, ACKNOWLEDGMENT_DAYS_DUE)

    # 5. Create Request
    request = Requests(
        id=request_id,
        title=title,
        agency_ein=agency_ein,
        category=category,
        description=description,
        date_created=date_created,
        date_submitted=date_submitted,
        due_date=due_date,
        submission=submission,
    )
    create_object(request)

    guid_for_event = current_user.guid if not current_user.is_anonymous else None
    auth_type_for_event = current_user.auth_user_type if not current_user.is_anonymous else None

    # 6. Get or Create User
    if current_user.is_public:
        user = current_user
    else:
        user = Users(guid=generate_guid(),
                     auth_user_type=ANONYMOUS_USER,
                     email=email,
                     first_name=first_name,
                     last_name=last_name,
                     title=user_title or None,
                     organization=organization or None,
                     email_validated=False,
                     terms_of_use_accepted=False,
                     phone_number=phone,
                     fax_number=fax,
                     mailing_address=address)
        create_object(user)
        # user created event
        create_object(
            Events(request_id,
                   guid_for_event,
                   auth_type_for_event,
                   event_type.USER_CREATED,
                   previous_value=None,
                   new_value=user.val_for_events,
                   response_id=None,
                   timestamp=datetime.utcnow()))

    if upload_path is not None:
        # 7. Move file to upload directory
        upload_path = _move_validated_upload(request_id, upload_path)
        # 8. Create response object
        filename = os.path.basename(upload_path)
        response = Files(request_id,
                         RELEASE_AND_PRIVATE,
                         filename,
                         filename,
                         fu.get_mime_type(upload_path),
                         fu.getsize(upload_path),
                         fu.get_hash(upload_path),
                         is_editable=False)
        create_object(obj=response)

        # 8. Create upload Event
        upload_event = Events(user_guid=user.guid,
                              auth_user_type=user.auth_user_type,
                              response_id=response.id,
                              request_id=request_id,
                              type_=event_type.FILE_ADDED,
                              timestamp=datetime.utcnow(),
                              new_value=response.val_for_events)
        create_object(upload_event)

        # Create response token if requester is anonymous
        if current_user.is_anonymous or current_user.is_agency:
            create_object(ResponseTokens(response.id))

    role_to_user = {
        role.PUBLIC_REQUESTER: user.is_public,
        role.ANONYMOUS: user.is_anonymous_requester,
    }
    role_name = [k for (k, v) in role_to_user.items() if v][0]
    # (key for "truthy" value)

    # 9. Create Event
    timestamp = datetime.utcnow()
    event = Events(user_guid=user.guid
                   if current_user.is_anonymous else current_user.guid,
                   auth_user_type=user.auth_user_type if
                   current_user.is_anonymous else current_user.auth_user_type,
                   request_id=request_id,
                   type_=event_type.REQ_CREATED,
                   timestamp=timestamp,
                   new_value=request.val_for_events)
    create_object(event)
    if current_user.is_agency:
        agency_event = Events(user_guid=current_user.guid,
                              auth_user_type=current_user.auth_user_type,
                              request_id=request.id,
                              type_=event_type.AGENCY_REQ_CREATED,
                              timestamp=timestamp)
        create_object(agency_event)

    # 10. Create UserRequest for requester
    user_request = UserRequests(
        user_guid=user.guid,
        auth_user_type=user.auth_user_type,
        request_user_type=user_type_request.REQUESTER,
        request_id=request_id,
        permissions=Roles.query.filter_by(name=role_name).first().permissions)
    create_object(user_request)
    create_object(
        Events(request_id,
               guid_for_event,
               auth_type_for_event,
               event_type.USER_ADDED,
               previous_value=None,
               new_value=user_request.val_for_events,
               response_id=None,
               timestamp=datetime.utcnow()))

    # 11. Create the elasticsearch request doc only if agency has been onboarded
    agency = Agencies.query.filter_by(ein=agency_ein).one()

    # (Now that we can associate the request with its requester.)
    if current_app.config['ELASTICSEARCH_ENABLED'] and agency.is_active:
        request.es_create()

    # 12. Add all agency administrators to the request.
    if agency.administrators:
        # b. Store all agency users objects in the UserRequests table as Agency users with Agency Administrator
        # privileges
        _create_agency_user_requests(request_id=request_id,
                                     agency_admins=agency.administrators,
                                     guid_for_event=guid_for_event,
                                     auth_type_for_event=auth_type_for_event)

    # 13. Add all parent agency administrators to the request.
    parent_agency_ein = _get_parent_ein(agency.parent_ein)
    if agency.ein != parent_agency_ein:
        parent_agency = Agencies.query.filter_by(ein=parent_agency_ein).one()
        if parent_agency.administrators:
            _create_agency_user_requests(
                request_id=request_id,
                agency_admins=parent_agency.administrators,
                guid_for_event=guid_for_event,
                auth_type_for_event=auth_type_for_event)
    return request_id
Пример #5
0
def media():
    global requestedRequests
    global i
    global directory
    global coursesList
    global videoDuration

    # Extract fields from extension request
    req = request.json
    url = req['url']
    name = req['name']
    courseName = req['courseName']
    videoDuration = req['videoDuration']
    name = name.split('\n')[0]
    courseName = re.sub('[^0-9a-zA-Z]+', '_', courseName)
    name = re.sub('[^0-9a-zA-Z]+', '_', name)
    if(courseName not in coursesList):
        c = Course(coursename=courseName)
        db.session.add(c)
        db.session.commit()
        getCourseList()

    directory = f"{path_to_directory}/{courseName}"
    video_pattern = ""
    is_video = False
    is_videoHD = re.search(video_patternHD, url)
    is_videoFHD = re.search(video_patternFHD, url)
    if(is_videoFHD):
        is_video = True
        video_pattern = video_patternFHD
    if(is_videoHD):
        is_video = True
        video_pattern = video_patternHD
    is_audio = re.search(audio_pattern, url)

    # There is only one GET request if the wideo is a mp4
    is_mp4 = re.search(mp4_pattern, url)
    if is_mp4:
        requestedRequests.add(video_pattern)
        requestedRequests.add(audio_pattern)
        fname = handle_windows_path(f"{directory}/video_{i}_{name}.mp4")
        print(f"downloading {fname}")
        urllib.request.urlretrieve(url, fname)
        print(f"downloading {fname}")
        courseID = db.session.query(Course).filter_by(coursename=courseName).first().id
        verbose_cls()
        i += 1

    # If course uses .aac and .ts we have to filter for unique requests
    if(len(requestedRequests) < 2):
        courseID = db.session.query(Course).filter_by(coursename=courseName).first().id
        if is_video and video_pattern not in requestedRequests:
            fname = handle_windows_path(f"{directory}/video_{i}_{name}.ts")
            print(f"downloading {fname}")
            #urllib.request.urlretrieve(url, fname)
            while(download(url, fname) is False):
                print(f"downloading {fname}")
                print(f"Error during downloading {fname}")
            requestedRequests.add(video_pattern)
            f = Files(filename=f"video_{i}_{name}.ts", course_id=courseID)
            #db.session.add(f)
            #db.session.commit()
            print(f"Downloaded {fname}")

        if is_audio and audio_pattern not in requestedRequests:
            fname = handle_windows_path(f"{directory}/audio_{i}_{name}.aac")
            print(f"downloading {fname}")
            #urllib.request.urlretrieve(url, fname)
            while(download(url, fname) is False):
                print(f"downloading {fname}")
                print(f"Error during downloading {fname}")
            requestedRequests.add(audio_pattern)
            f = Files(filename=f"audio_{i}_{name}.aac", course_id=courseID)
            #db.session.add(f)
            #db.session.commit()
            print(f"Downloaded {fname}")

        if len(requestedRequests) == 2:
            verbose_cls()
            i += 1
    if(automatic):
        clearRequests()
    return ""