Esempio n. 1
0
def create_essay():

    # construct a new essay and redirect to it
    essay = Essay()
    essay.uid = g.user.uid
    essay.upload_id = None
    db.session.add(essay)
    db.session.flush()

    now = datetime.now()

    # construct an initial draft for the essay
    draft = Draft()
    draft.eid = essay.eid
    draft.uid = g.user.uid
    draft.created_date = now
    draft.modified_date = now

    draft.body = Draft.default_body()
    draft.title = ""
    db.session.add(draft)
    db.session.commit()

    return redirect(url_for('essays.edit_essay', essayid=essay.eid))
Esempio n. 2
0
def upload_essay():

    if request.method == 'POST' and 'paper' in request.files:
        f = request.files['paper']
        title = request.form.get('title', None)
        if not title:
            title = f.filename

        file_name, file_extension = os.path.splitext(f.filename)
        file_extension = file_extension[1::]
        applicable_parsers = filter(lambda p: p.accepts_extension(file_extension), parsers)
        if len(applicable_parsers) == 0:
            flash('Invalid file format provided')
            return render_upload(parsers)

        # Read the entire file into memory.
        contents = f.read()
        f.seek(0)

        # Parse the document with the appropriate parser
        doc_parser = applicable_parsers[0]
        parsed_contents = doc_parser.parse_file(f.stream)

        # Create an entry in the upload table for the upload
        new_upload = Upload()
        new_upload.uid = g.user.uid
        new_upload.size = len(contents)
        new_upload.mimetype = f.mimetype
        new_upload.filename = f.filename
        db.session.add(new_upload)
        db.session.flush()

        # Save the file to s3
        conn = S3Connection(current_app.config['AWS_ACCESS_KEY'],
                            current_app.config['AWS_SECRET_KEY'])
        bucket = conn.get_bucket(current_app.config['UPLOADS_S3_BUCKET'])
        k = Key(bucket)
        k.key = str(new_upload.upload_id) + '-' + f.filename
        k.set_contents_from_string(contents)

        # Create the essay entry
        new_essay = Essay()
        new_essay.uid = g.user.uid
        new_essay.upload_id = new_upload.upload_id
        db.session.add(new_essay)
        db.session.flush()

        now = datetime.now()

        # Create draft associated with essay
        draft = Draft()
        draft.eid = new_essay.eid
        draft.uid = g.user.uid
        draft.created_date = now
        draft.modified_date = now
        draft.title = parsed_contents.title if title in parsed_contents else title
        paragraphs = parsed_contents['text'].split('\n')
        body = {'blockid': 1, 'type': 'container', 'children': []}
        blockid = 1
        for p in paragraphs:
            body['children'].append({'blockid': blockid + 1, 'type': 'text', 'text': p, 'modifiers': []})
            blockid = blockid + 1
        body['max_blockid'] = blockid
        draft.body = json.dumps(body)
        db.session.add(draft)
        db.session.commit()

        return redirect(url_for('essays.edit_essay', essayid=new_essay.eid))

    return render_upload(parsers)