From 6d622d55df74868f82c3bb09f8f8a74500174349 Mon Sep 17 00:00:00 2001 From: sameera sy Date: Thu, 12 Jul 2018 23:25:25 +0530 Subject: [PATCH 1/3] first commit --- hasjob/models/jobpost.py | 3 +++ hasjob/templates/detail.html.jinja2 | 12 +++++++++--- hasjob/twitter.py | 9 ++++++++- hasjob/views/listing.py | 2 ++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/hasjob/models/jobpost.py b/hasjob/models/jobpost.py index d8f880fb2..517d1d28b 100644 --- a/hasjob/models/jobpost.py +++ b/hasjob/models/jobpost.py @@ -143,6 +143,9 @@ class JobPost(BaseMixin, db.Model): review_datetime = db.Column(db.DateTime, nullable=True) review_comments = db.Column(db.Unicode(250), nullable=True) + # Social media links + tweetid = db.Column(db.Unicode(30), nullable=True) + search_vector = deferred(db.Column(TSVECTOR, nullable=True)) _state = db.Column('status', db.Integer, StateManager.check_constraint('status', POST_STATE), diff --git a/hasjob/templates/detail.html.jinja2 b/hasjob/templates/detail.html.jinja2 index b5966fb50..220d19bc5 100644 --- a/hasjob/templates/detail.html.jinja2 +++ b/hasjob/templates/detail.html.jinja2 @@ -124,9 +124,15 @@    Share on WhatsApp - -    Tweet this - + {%- if post.tweetid %} + +    Retweet this + + {%- else%} + +    Tweet this + + {%- endif %}    Share on Facebook diff --git a/hasjob/twitter.py b/hasjob/twitter.py index 923bc1f42..f4cc81a30 100644 --- a/hasjob/twitter.py +++ b/hasjob/twitter.py @@ -44,7 +44,14 @@ def tweet(title, url, location=None, parsed_location=None, username=None): text = text + ' ' + locationtag if username: text = text + ' @' + username - api.update_status(text) + tweet_status = api.update_status(text) + return tweet_status.id + + +def retweet(post): + # Need to add the By-line like A job by Company has been posted on hasjob, check it out + post.headline + tweet_id = tweet(post.headline, post.url_for(_external=True)) + return tweet_id def shorten(url): diff --git a/hasjob/views/listing.py b/hasjob/views/listing.py index 62a0c3db9..c6a6434ee 100644 --- a/hasjob/views/listing.py +++ b/hasjob/views/listing.py @@ -21,6 +21,7 @@ AnonJobView, JobApplication, Campaign, CAMPAIGN_POSITION, unique_hash, viewstats_by_id_hour, viewstats_by_id_day) from hasjob.twitter import tweet +from hasjob.twitter import retweet from hasjob.tagging import tag_locations, add_to_boards, tag_jobpost from hasjob.uploads import uploaded_logos from hasjob.utils import get_word_bag, redactemail, random_long_key, common_legal_names @@ -746,6 +747,7 @@ def confirm_email(domain, hashid, key): % post.email_domain, category='info') return redirect(url_for('index')) post.confirm() + post.tweetid = retweet(post) db.session.commit() if app.config['TWITTER_ENABLED']: if post.headlineb: From 34dad239effd528c0af1c0245295bbf967d2e63e Mon Sep 17 00:00:00 2001 From: sameera sy Date: Fri, 13 Jul 2018 09:33:54 +0530 Subject: [PATCH 2/3] adding migration script for column add --- .../d8258df712fa_tweet_id_for_jobpost.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 migrations/versions/d8258df712fa_tweet_id_for_jobpost.py diff --git a/migrations/versions/d8258df712fa_tweet_id_for_jobpost.py b/migrations/versions/d8258df712fa_tweet_id_for_jobpost.py new file mode 100644 index 000000000..c7e3dcdc9 --- /dev/null +++ b/migrations/versions/d8258df712fa_tweet_id_for_jobpost.py @@ -0,0 +1,23 @@ +"""Tweet id for JobPost + +Revision ID: d8258df712fa +Revises: 625415764254 +Create Date: 2018-07-12 18:22:20.245244 + +""" + +# revision identifiers, used by Alembic. +revision = 'd8258df712fa' +down_revision = '625415764254' + +from alembic import op +import sqlalchemy as sa + + + +def upgrade(): + op.add_column('jobpost', sa.Column('tweetid', sa.Unicode(length=30), nullable=True)) + + +def downgrade(): + op.drop_column('jobpost', 'tweetid') From 9c919f0479bdf15e007e6ab34b7335838e6d7f39 Mon Sep 17 00:00:00 2001 From: sameera sy Date: Fri, 13 Jul 2018 22:35:20 +0530 Subject: [PATCH 3/3] changed according to reviews --- hasjob/twitter.py | 22 ++++++++++++++++------ hasjob/views/listing.py | 13 ++----------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/hasjob/twitter.py b/hasjob/twitter.py index f4cc81a30..82a50f118 100644 --- a/hasjob/twitter.py +++ b/hasjob/twitter.py @@ -7,9 +7,25 @@ import json import re from hasjob import app +from hasjob.models import JobPost +from hasjob.models import db @job('hasjob') +def tweet_post(post_id): + print "in tweet post" + post = JobPost.query.get(post_id) + if post.headlineb: + post.tweetid = tweet(post.headline, post.url_for(b=0, _external=True), + post.location, dict(post.parsed_location or {}), username=post.twitter) + tweet(post.headlineb, post.url_for(b=1, _external=True), + post.location, dict(post.parsed_location or {}), username=post.twitter) + else: + post.tweetid = tweet(post.headline, post.url_for(_external=True), + post.location, dict(post.parsed_location or {}), username=post.twitter) + db.session.commit() + + def tweet(title, url, location=None, parsed_location=None, username=None): auth = OAuthHandler(app.config['TWITTER_CONSUMER_KEY'], app.config['TWITTER_CONSUMER_SECRET']) auth.set_access_token(app.config['TWITTER_ACCESS_KEY'], app.config['TWITTER_ACCESS_SECRET']) @@ -48,12 +64,6 @@ def tweet(title, url, location=None, parsed_location=None, username=None): return tweet_status.id -def retweet(post): - # Need to add the By-line like A job by Company has been posted on hasjob, check it out + post.headline - tweet_id = tweet(post.headline, post.url_for(_external=True)) - return tweet_id - - def shorten(url): if app.config['BITLY_KEY']: b = bitlyapi.BitLy(app.config['BITLY_USER'], app.config['BITLY_KEY']) diff --git a/hasjob/views/listing.py b/hasjob/views/listing.py index c6a6434ee..7331d4bc9 100644 --- a/hasjob/views/listing.py +++ b/hasjob/views/listing.py @@ -20,8 +20,7 @@ PAY_TYPE, ReportCode, UserJobView, AnonJobView, JobApplication, Campaign, CAMPAIGN_POSITION, unique_hash, viewstats_by_id_hour, viewstats_by_id_day) -from hasjob.twitter import tweet -from hasjob.twitter import retweet +from hasjob.twitter import tweet_post from hasjob.tagging import tag_locations, add_to_boards, tag_jobpost from hasjob.uploads import uploaded_logos from hasjob.utils import get_word_bag, redactemail, random_long_key, common_legal_names @@ -747,17 +746,9 @@ def confirm_email(domain, hashid, key): % post.email_domain, category='info') return redirect(url_for('index')) post.confirm() - post.tweetid = retweet(post) db.session.commit() if app.config['TWITTER_ENABLED']: - if post.headlineb: - tweet.delay(post.headline, post.url_for(b=0, _external=True), - post.location, dict(post.parsed_location or {}), username=post.twitter) - tweet.delay(post.headlineb, post.url_for(b=1, _external=True), - post.location, dict(post.parsed_location or {}), username=post.twitter) - else: - tweet.delay(post.headline, post.url_for(_external=True), - post.location, dict(post.parsed_location or {}), username=post.twitter) + tweet_post.delay(post.id) add_to_boards.delay(post.id) flash("Congratulations! Your job post has been published. As a bonus for being an employer on Hasjob, " "you can now see how your post is performing relative to others. Look in the footer of any post.",