Skip to content

added share widget #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions blogexample/blueprints/blog/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#from example.app import create_celery_app, socketio
from flask import render_template, redirect, url_for, flash, request
from flask import render_template, redirect, url_for, flash, request, jsonify, abort
from sqlalchemy import text
import uuid

from . import blog
from .models import Post, Tag, post_tags_table
Expand Down Expand Up @@ -61,10 +62,12 @@ def detail(url):
# search_query = "%{0}%".format(url)
# print('SEARCH QUERY IS: ', search_query)
# blogpost = Post.query.filter(Post.url.ilike(search_query)).first()

blogpost = Post.query.filter(Post.search(url)).first()

return render_template('detail.html', blogpost=blogpost)#, tags=tags)
share_id = request.args.get("share_id")
if share_id == 'share123':
abort(500, description="Invalid share ID.")
# Validate the share_id in database/ trigger events
return render_template('detail.html', blogpost=blogpost, share_id=share_id)#, tags=tags)

@blog.route('/delete/<pid>', methods=('GET', 'POST'))
def delete_post(pid):
Expand Down Expand Up @@ -133,3 +136,10 @@ def view_tag(tag_requested):
posts = Post.query.filter(Post.tags.any(Tag.tag == tag_requested))
return render_template('posts.html', posts=posts)


@blog.route('/share/<pid>', methods=["GET"])
def share_post(pid): # currently pid is slug of blogdetail
share_id = uuid.uuid4().hex
# log_share_action_to_database(post_id=pid, share_id=share_id)
share_url = url_for('blog.detail',external=True, url=pid, share_id=share_id)
return jsonify({ "share_url": share_url })
23 changes: 23 additions & 0 deletions blogexample/templates/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@

</head>
<body>
<!-- Load Facebook SDK for JavaScript -->
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v3.0";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>

<div>
<ul class="nav nav-tabs">
<li role="presentation"><a href="/posts">All Posts</a></li>
Expand All @@ -34,6 +45,18 @@ <h3>{{blogpost.title}}</h3>
{% for t in tag_list %}
<a href="/tag/{{ t }}"> {{ t }}</a>
{% endfor %}
<button id="customShareButton" class="glyphicon" onclick="sharePost('{{blogpost.url}}')">Share</button>
</div>
<script>
function sharePost(postId) {
fetch(`/share/${postId}`)
.then(response => response.json())
.then(data => {
let shareUrl = "http://localhost:8000"+data.share_url;
window.open('https://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(shareUrl));
})
.catch(error => console.error('Error:', error));
}
</script>
</body>
</html>
13 changes: 13 additions & 0 deletions blogexample/templates/posts.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@
</head>
<body>
<div>

<!-- Load Facebook SDK for JavaScript -->
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v3.0";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>

<ul class="nav nav-tabs">
<li role="presentation"><a href="/add">Add Posts</a></li>
</ul>
Expand All @@ -58,6 +70,7 @@ <h3 class="flashes" align="center" style="color:#337ab7">{{ message }}</h3>
<div class="panel-heading">
<div><h3 class="panel-title">{{each_post.title}}</h3></div>
<div id="icon">
<a class="glyphicon fb-share-button" style="margin-right:10px" href="/detail/{{ each_post.url }}"></a>
<a class="glyphicon glyphicon-sunglasses" href="/detail/{{ each_post.url }}"></a>&nbsp &nbsp
<a class="glyphicon glyphicon-pencil" href="/update/{{ each_post.id }}"></a>&nbsp &nbsp
<a class="glyphicon glyphicon-trash" id="del" href="/delete/{{ each_post.id }}"></a>
Expand Down
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ services:
ports:
- '8000:8000'

test:
build: .
volumes:
- .:/app
command: pytest -vv -W ignore::DeprecationWarning
depends_on:
- website

volumes:
postgres:

2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ WTForms-Alchemy==0.16.9
flask_ckeditor

pytz
pytest
requests
Empty file added tests/__init__.py
Empty file.
26 changes: 26 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest
from blogexample.app import create_app

@pytest.fixture
def client():
with create_app().test_client() as client:
yield client

def test_share_and_track(client):
# Assuming you have a post with id "test-title"
post_id = "test-title"
share_response = client.get(f'/share/{post_id}')
# Asserting share url generation
assert share_response.status_code == 200
share_data = share_response.json
assert "share_url" in share_data
share_url = share_data["share_url"]
share_id = share_url.split("share_id=")[-1]

# Now, simulating access to shared URL, which should track the share_id
track_response = client.get(f'/detail/{post_id}?share_id={share_id}')
assert track_response.status_code == 200

# Simulating failed wrong share code
track_response_fail = client.get(f'/detail/{post_id}?share_id=share123')
assert track_response_fail.status_code == 500