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..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']) @@ -44,7 +60,8 @@ 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 shorten(url): diff --git a/hasjob/views/listing.py b/hasjob/views/listing.py index 62a0c3db9..7331d4bc9 100644 --- a/hasjob/views/listing.py +++ b/hasjob/views/listing.py @@ -20,7 +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 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 @@ -748,14 +748,7 @@ def confirm_email(domain, hashid, key): post.confirm() 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.", 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')