在我的 helpers 文件夹中,我在__init__.py
中使用以下方法自动生成链接:
def autogenerate_click_to_tweet_if_needed(production):
"""
# We check if the click to tweet associated with this production
# needs to be auto-generated. We only do this if it is NULL or empty
# so that if user entered it manually it prevales.
:param production:
"""
if not production:
return
# 1. We check if the production meets the requirements for a click to tweet.
# If it does not we delete all click to tweet associated with the production.
# Criteria is to be Published.
meets_criteria = production.status() == 'Published'
if not meets_criteria:
ClickToTweetProductionTweet.objects.filter(production=production).delete()
return
# 2. We get the first and only click to tweet for this production.
click_to_tweet = ClickToTweetProductionTweet.objects.filter(production=production).first()
# If we have a valid text in the tweet, we just return as we will not
# modify an existing one.
if click_to_tweet and click_to_tweet.tweet:
return
# 3. We get the format for the tweet from the Podcast.
# if we have no valid format then do nothing
tweet_format = production.podcast.podcast_click_to_tweet_format
if not tweet_format:
return
# 4. If we do not have a click to tweet we create one.
if not click_to_tweet:
click_to_tweet = ClickToTweetProductionTweet(production=production)
# 5. We will make the necessary an possible substitutions on the format
# to compose the tweet text.
# Valid placeholders are:
# PODCAST_TWITTER_NAME = podcast_twitter_name (in podcast)
# PODCAST_GUEST_TWITTER_NAME = podcast_guest_twitter_name (in podcast)
# WORDPRESS_URL = wordpress_url (in production)
tweet_format = replace_placeholder_in_text_format_with_parameter(tweet_format, 'PODCAST_TWITTER_NAME',
production.podcast.podcast_twitter_name)
tweet_format = replace_placeholder_in_text_format_with_parameter(tweet_format, 'PODCAST_GUEST_TWITTER_NAME',
production.podcast.podcast_guest_twitter_name)
wordpress_url = production.wordpress_short_url if production.wordpress_short_url else production.wordpress_url
tweet_format = replace_placeholder_in_text_format_with_parameter(tweet_format, 'WORDPRESS_URL', wordpress_url)
tweet_format = smart_text(tweet_format)
# 6. Update and save
click_to_tweet.tweet = tweet_format
click_to_tweet.save()
在我ProductionTweet
的{{1}}课程中,我有以下方法可以获取链接:
models.py
以下是def click_to_tweet_link(self):
base_url = settings.CLICK_TO_TWEET_BASE_URL
if not base_url:
return None
return base_url + self.slug
中推文部分的视图:
views.py
其形式:
class EpisodeClickToTweet(ProductionRequiredMixin, View):
form_class = EpisodeClickToTweetForm
template_name = 'fc/forms_clicktotweet.html'
def get(self, request, *args, **kwargs):
initial_values = {}
# See if we already have a click to tweet for this production, if so get id
click_to_tweet = ClickToTweetProductionTweet.objects.filter(production_id=self.production.id).first()
initial_values['production_id'] = self.production.id
initial_values['id'] = click_to_tweet.id if click_to_tweet else None
initial_values['tweet'] = click_to_tweet.tweet if click_to_tweet else None
form = self.form_class(initial=initial_values)
return render(request, self.template_name, {'form': form})
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
if form.is_valid():
# lets get the data
production_id = form.cleaned_data.get('production_id')
id = form.cleaned_data.get('id')
tweet = form.cleaned_data.get('tweet')
#A production must exist, othewise 404
production = get_object_or_404(Production, id=production_id)
if id:
click_to_tweet = ClickToTweetProductionTweet.objects.get(id=id)
else:
click_to_tweet = ClickToTweetProductionTweet(production=production)
click_to_tweet.tweet = tweet
click_to_tweet.save()
return HttpResponseRedirect(reverse('fc:episodeschedule', kwargs={'production_id':production_id}))
return render(request, self.template_name, {'form': form})
模板:
from django import forms
class EpisodeClickToTweetForm(forms.Form):
production_id = forms.IntegerField(widget=forms.Field.hidden_widget, required=False)
id = forms.IntegerField(widget=forms.Field.hidden_widget, required=False)
tweet = forms.CharField(max_length=80, required=False, widget=forms.TextInput(attrs={'placeholder': 'Add Tweet Text', }))
所以回顾一下:
对此最好的方法是什么?
非常感谢任何帮助或建议:)