Good practice in using an external api in django

user9357277 Source

I wonder where I should put the code for api twitter. I have two applications: "accounts" and "twitter".

in the "accounts" application I have a code that allows you to login using twitter ('social_django', 'widget_tweaks',).

in the "twitter" application, I want to use api twitter.

My question is, is it good practice to log in and use the api in the "accounts" application? Can you log in to api in the "twitter" application in the views?

accounts/views.py

@login_required
def settings(request):
    user = request.user

    try:
        twitter_login = user.social_auth.get(provider='twitter')
    except UserSocialAuth.DoesNotExist:
        twitter_login = None

    can_disconnect = (user.social_auth.count() > 1 or user.has_usable_password())

    return render(request, 'accounts/settings.html', {
        'twitter_login': twitter_login,
        'can_disconnect': can_disconnect,
    })

twitter/views.py

from django.views import generic
from .models import Tweet
from .forms import TweetForm


class AppTwitterListView(generic.ListView):
    model = Tweet

    def get_queryset(self):
        return Tweet.objects.filter(user=self.request.user)


class AppTwitterDetailView(generic.DetailView):
    model = Tweet
    context_object_name = 'tweet_item'


class AppTwitterCreateView(generic.CreateView):
    model = Tweet
    form_class = TweetForm
    template_name = "apptwitter/create.html"
pythondjangoapiviewdry

Answers

comments powered by Disqus