Skip to content

Commit

Permalink
generate share link
Browse files Browse the repository at this point in the history
  • Loading branch information
emillon committed Dec 17, 2014
1 parent 1b04ac9 commit 69c857e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
15 changes: 13 additions & 2 deletions app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from flask.ext.uploads import UploadNotAllowed
from flask.ext.wtf import Form
from flask_wtf.file import FileField
from itsdangerous import URLSafeSerializer
from werkzeug.exceptions import BadRequest
from wtforms import HiddenField
from wtforms import TextAreaField
Expand Down Expand Up @@ -133,13 +134,15 @@ def view_doc(id):
doc = Document.query.get_or_404(id)
form_comm = CommentForm(docid=id)
form_up = UploadForm()
form_share = ShareForm()
comments = Comment.query.filter_by(doc=id)
annotations = Annotation.query.filter_by(doc=id)
readOnly = not current_user.is_authenticated()
return render_template('view.html',
doc=doc,
form_comm=form_comm,
form_up=form_up,
form_share=form_share,
comments=comments,
annotations=annotations,
readOnly=readOnly,
Expand Down Expand Up @@ -337,6 +340,14 @@ def delete_doc(id):
return redirect(url_for('.view_doc', id=id))


@bp.route('/view/<id>/share')
class ShareForm(Form):
pass


@bp.route('/view/<id>/share', methods=['POST'])
def share_doc(id):
return render_template('share.html')
salt = 'share-link'
s = URLSafeSerializer(current_app.secret_key, salt=salt)
data = {'doc': id}
h = s.dumps(data)
return jsonify(data=h)
13 changes: 11 additions & 2 deletions static/coffee/view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ view_init = (docid, filetype, readOnly) ->
view_init_image docid, readOnly
when "audio"
view_init_audio docid, readOnly
view_init_common()
view_init_common docid

view_init_audio = (docid, readOnly) ->
$pv = $('#docview')
Expand Down Expand Up @@ -59,9 +59,18 @@ view_init_pdf = (docid, readOnly) ->
.then null, ->
$pv.text "Error loading the document."

view_init_common = ->
view_init_common = (docid) ->
form_init '#upload_dialog', '#upload_link'
form_init '#share_dialog', '#share_link'
$('#share_form').submit (e) ->
e.preventDefault()
$.ajax
type: 'POST'
url: "/view/#{docid}/share"
success: (data) ->
share_url = "#{window.location.origin}/view/#{docid}/shared/#{data['data']}"
input = $('<input>').attr('type', 'text').val(share_url)
$('#share_form > button[type=submit]').replaceWith input

$('#post_comment_form').submit (e) ->
e.preventDefault()
Expand Down
9 changes: 6 additions & 3 deletions templates/macros.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@
</form>
{% endmacro %}

{% macro share_form() %}
{% macro render_share_form(form) %}
<p>
By clicking on this button you will generate link to this document.
By clicking on this button you will generate a link to this document.
</p>

<p>
The person you will send this link to will be able to make comments on it
without having to register.
</p>

<button class="btn btn-primary">Generate</button>
<form method="post" enctype="multipart/form-data" id="share_form">
{{ form.hidden_tag() }}
<button type="submit" class="btn btn-primary">Generate</button>
</form>
{% endmacro %}

{% macro annotation_state(ann) %}
Expand Down
4 changes: 2 additions & 2 deletions templates/view.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% from 'macros.html' import glyphicon %}
{% from 'macros.html' import js_bool %}
{% from 'macros.html' import upload_form %}
{% from 'macros.html' import share_form %}
{% from 'macros.html' import render_share_form %}
{% extends "base.html" %}
{% block title %}Document{% endblock %}
{% block content %}
Expand Down Expand Up @@ -64,7 +64,7 @@ <h3>{{ glyphicon('pencil') }} History</h3>
{{ upload_form (form_up, url_for('.upload', revises=doc.id)) }}
</div>
<div id="share_dialog" title="Generate a review link">
{{ share_form () }}
{{ render_share_form(form_share) }}
</div>

<h3>{{ glyphicon('comment') }} Comments</h3>
Expand Down

0 comments on commit 69c857e

Please sign in to comment.