Esempio n. 1
0
def next_draft():
    current_app.logger.debug('creating next draft')

    if 'did' in request.form and 'uid' in request.form:

        did = request.form['did']
        uid = request.form['uid']

        draft = Draft.query.filter_by(did=did).first()

        if not draft or draft.uid != g.user.uid or draft.finalized:
            return jsonify(error="Bad params"), 400

        # finalize the draft
        draft.finalized = True
        db.session.add(draft)
        db.session.flush()

        # make a new draft for the writer
        new_draft = Draft.next_draft(draft)
        db.session.add(new_draft)
        db.session.commit()

        return jsonify(status='success',
            did=new_draft.did,
            version=new_draft.version,
            timestamp=new_draft.pretty_modified_date()
        )
    else:
        return jsonify(error="Bad params"), 400
Esempio n. 2
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))
    def save(self, params, request_obj=None):
        logging.info(request_obj)
        self.clear()

        controller_name = self.controller.name
        username = users.get_current_user().email()

        draft = Draft()
        draft.controller_name = controller_name
        draft.username = username

        for index, field in enumerate(params):
            #logging.info('%s ==> %s' % (field, params[field]))

            if request_obj is not None:
                check_if_array = request_obj.POST.getall(field)
                is_array = False
                if len(check_if_array) > 0:
                    is_array = True

                if is_array:
                    value = None
                    for v in check_if_array:

                        if v is None:
                            v = ''

                        if value is None:
                            value = v
                        else:
                            value = value + '::::' + v
                else:
                    value = params[field]
            else:
                value = params[field]

            logging.info('%s ==> %s' % (field, value))
            if value != '' and value is not None:
                setattr(draft, field, value)

        draft.put()
 def retrive(self, controller_name, username):
     return Draft.query(
         ndb.GenericProperty('controller_name') == controller_name,
         ndb.GenericProperty('username') == username).get()
Esempio n. 5
0
def email_a_review():
    current_app.logger.debug('emailing review for a draft')

    if 'did' in request.form and 'uid' in request.form and 'email' in request.form:

        did = request.form['did']
        uid = request.form['uid']
        email = request.form['email']

        draft = Draft.query.filter_by(did=did).first()

        if not draft or draft.uid != g.user.uid:
            return jsonify(error="Invalid draft"), 400

        if not validate_email(email, check_mx=True):
            return jsonify(error="Invalid email"), 400

        # create review pointing to the draft
        review = Review()
        review.did = draft.did
        review.email = email

        # really good code to generate a unique url hash for the review
        unique_hash = False
        gen_hash = ''
        while not unique_hash:
            gen_hash = uuid.uuid4().hex
            queried_review = Review.query.filter_by(urlhash=gen_hash).first()
            if not queried_review:
                unique_hash = True

        review.urlhash = gen_hash

        db.session.add(review)

        # if the draft was not finalized, finalize it and create a new one
        new_did = None
        new_version = None
        new_timestamp = None
        if not draft.finalized:
            draft.finalized = True
            db.session.add(draft)
            db.session.flush()

            # make a new draft for the writer
            new_draft = Draft.next_draft(draft)
            db.session.add(new_draft)
            db.session.flush()

            new_did = new_draft.did
            new_version = new_draft.version
            new_timestamp = new_draft.pretty_modified_date()
        else:
            db.session.flush()

        review_url = 'http://' + app.config.get('SERVER_HOST') + '/essays/review/' + review.urlhash

        # send emailz
        params = {
            'sender': g.user.first_name + ' ' + g.user.last_name,
            'review_url': review_url
        }
        mailer = Mailer()
        mailer.send(ReviewADraft(), params, email)


        # create notification for user receiving review if necessary
        receiving_user = User.query.filter_by(email=email).first()
        if receiving_user:
            notification = Notification()
            notification.uid = receiving_user.uid
            notification.from_uid = uid
            notification.short_template = 'requested a review.'
            notification.url = review_url
            notification.long_template = '{{ sender }} wrote an essay and wants you to <a href="' + review_url +'"> review </a> it.'
            db.session.add(notification)

        db.session.commit()

        return jsonify(status='success',
            new_did=new_did,
            new_version=new_version,
            new_timestamp=new_timestamp
        )
    else:
        return jsonify(error="Bad params"), 400
Esempio n. 6
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)