def get_context_data(self, **kwargs): from gim.core.tasks.githubuser import FetchAvailableRepositoriesJob context = super(ChooseRepositoryView, self).get_context_data(**kwargs) if [j for j in FetchAvailableRepositoriesJob.collection( identifier=self.request.user.id, queued=1).instances() if j.status.hget() != STATUSES.DELAYED]: context['still_fetching'] = True context['tabs'] = [] for tab in self.tabs: tab_view = tab() tab_view.inherit_from_view(self) context['tabs'].append({ 'class': tab, 'count': tab_view.count(), 'part': tab_view.render_for_start(), }) return context
def complete_auth(self): # check state attended_state = self.request.session.get('github-auth-state', None) if not attended_state: return False, "Unexpected request, please retry" del self.request.session['github-auth-state'] state = self.request.GET.get('state', None) if state.strip() != attended_state.strip(): return False, "Unexpected request, please retry" # get code code = self.request.GET.get('code', None) if not code: return False, "Authentication denied, please retry" # get the token for the given code try: gh = self.get_github_connection() token = gh.get_access_token(code) except: token = None if not token: return False, "Authentication failed, please retry" # do we have a user for this token ? try: user_with_token = GithubUser.objects.get(token=token) except GithubUser.DoesNotExist: user_with_token = None else: user_with_token.token = None user_with_token.save(update_fields=['token']) # get informations about this user try: user_infos = Connection(access_token=token).user.get() except: user_infos = None if not user_infos: return False, "Cannot get user informations, please retry" # create/update and get a user with the given infos and token try: user = GithubUser.objects.create_or_update_from_dict( data=user_infos, defaults={'simple': {'token': token}}) except: return False, "Cannot save user informations, please retry" # reject banned users if not user.is_active: return False, "This account has been deactivated" # authenticate the user (needed to call login later) user = authenticate(username=user.username, token=user.token) if not user: return False, "Final authentication failed, please retry" # and finally login login(self.request, user) # set its username to the token user.token_object.username.hset(user.username) # remove other tokens for this username from gim.core.limpyd_models import Token for user_token in list(Token.collection(username=user.username).instances()): if user_token.token.hget() != token: user_token.delete() # add a job to fetch available repositories job = FetchAvailableRepositoriesJob.add_job(user.id, inform_user=1) if job.status == STATUSES.DELAYED: return True, "Authentication successful, welcome back!" else: return True, "Authentication successful, we are currently fetching repositories you can subscribe to (ones you own, collaborate to, or in your organizations)"