from .apps import OsfOauth2AdapterConfig
from allauth.socialaccount import providers
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider



class OSFAccount(ProviderAccount):
    def to_str(self):
        # default ... reserved word?
        dflt = super(OSFAccount, self).to_str()
        return next(
            value
            for value in (
                # try the name first, then the id, then the super value
                '{} {}'.format(
                    self.account.extra_data.get('first_name', None),
                    self.account.extra_data.get('last_name', None)
                ),
                self.account.extra_data.get('id', None),
                dflt
            )
            if value is not None
        )

class OSFProvider(OAuth2Provider):
    id = 'osf'
    name = 'Open Science Framework'
    account_class = OSFAccount

    def extract_common_fields(self, data):
        attributes = data.get('data').get('attributes')
        return dict(
            # we could put more fields here later
            # the api has much more available, just not sure how much we need right now
            username=data.get('id'),
            first_name=attributes.get('given_name'),
            last_name=attributes.get('family_name'),
        )

    def extract_uid(self, data):
        return str(data.get('data').get('id'))

    def get_default_scope(self):
        return OsfOauth2AdapterConfig.default_scopes

providers.registry.register(OSFProvider)
from .apps import OsfOauth2AdapterConfig
from allauth.socialaccount import providers
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider



class OSFAccount(ProviderAccount):
    def to_str(self):
        # default ... reserved word?
        dflt = super(OSFAccount, self).to_str()
        return next(
            value
            for value in (
                # try the name first, then the id, then the super value
                '{} {}'.format(
                    self.account.extra_data.get('first_name', None),
                    self.account.extra_data.get('last_name', None)
                ),
                self.account.extra_data.get('id', None),
                dflt
            )
            if value is not None
        )

class OSFProvider(OAuth2Provider):
    id = 'osf'
    name = 'Open Science Framework'
    account_class = OSFAccount

    def extract_common_fields(self, data):
        attributes = data.get('data').get('attributes')
        return dict(
            # we could put more fields here later
            # the api has much more available, just not sure how much we need right now
            username=self.extract_uid(data),
            first_name=attributes.get('given_name'),
            last_name=attributes.get('family_name'),
        )

    def extract_uid(self, data):
        return str(data.get('data').get('id'))

    def get_default_scope(self):
        return OsfOauth2AdapterConfig.default_scopes

providers.registry.register(OSFProvider)
Change username to osf uid
Change username to osf uid

  from .apps import OsfOauth2AdapterConfig
  from allauth.socialaccount import providers
  from allauth.socialaccount.providers.base import ProviderAccount
  from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
  
  
  
  class OSFAccount(ProviderAccount):
      def to_str(self):
          # default ... reserved word?
          dflt = super(OSFAccount, self).to_str()
          return next(
              value
              for value in (
                  # try the name first, then the id, then the super value
                  '{} {}'.format(
                      self.account.extra_data.get('first_name', None),
                      self.account.extra_data.get('last_name', None)
                  ),
                  self.account.extra_data.get('id', None),
                  dflt
              )
              if value is not None
          )
  
  class OSFProvider(OAuth2Provider):
      id = 'osf'
      name = 'Open Science Framework'
      account_class = OSFAccount
  
      def extract_common_fields(self, data):
          attributes = data.get('data').get('attributes')
          return dict(
              # we could put more fields here later
              # the api has much more available, just not sure how much we need right now
-             username=data.get('id'),
+             username=self.extract_uid(data),
              first_name=attributes.get('given_name'),
              last_name=attributes.get('family_name'),
          )
  
      def extract_uid(self, data):
          return str(data.get('data').get('id'))
  
      def get_default_scope(self):
          return OsfOauth2AdapterConfig.default_scopes
  
  providers.registry.register(OSFProvider)
Change username to osf uid
## Code Before:
from .apps import OsfOauth2AdapterConfig
from allauth.socialaccount import providers
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider



class OSFAccount(ProviderAccount):
    def to_str(self):
        # default ... reserved word?
        dflt = super(OSFAccount, self).to_str()
        return next(
            value
            for value in (
                # try the name first, then the id, then the super value
                '{} {}'.format(
                    self.account.extra_data.get('first_name', None),
                    self.account.extra_data.get('last_name', None)
                ),
                self.account.extra_data.get('id', None),
                dflt
            )
            if value is not None
        )

class OSFProvider(OAuth2Provider):
    id = 'osf'
    name = 'Open Science Framework'
    account_class = OSFAccount

    def extract_common_fields(self, data):
        attributes = data.get('data').get('attributes')
        return dict(
            # we could put more fields here later
            # the api has much more available, just not sure how much we need right now
            username=data.get('id'),
            first_name=attributes.get('given_name'),
            last_name=attributes.get('family_name'),
        )

    def extract_uid(self, data):
        return str(data.get('data').get('id'))

    def get_default_scope(self):
        return OsfOauth2AdapterConfig.default_scopes

providers.registry.register(OSFProvider)
## Instruction:
Change username to osf uid
## Code After:
from .apps import OsfOauth2AdapterConfig
from allauth.socialaccount import providers
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider



class OSFAccount(ProviderAccount):
    def to_str(self):
        # default ... reserved word?
        dflt = super(OSFAccount, self).to_str()
        return next(
            value
            for value in (
                # try the name first, then the id, then the super value
                '{} {}'.format(
                    self.account.extra_data.get('first_name', None),
                    self.account.extra_data.get('last_name', None)
                ),
                self.account.extra_data.get('id', None),
                dflt
            )
            if value is not None
        )

class OSFProvider(OAuth2Provider):
    id = 'osf'
    name = 'Open Science Framework'
    account_class = OSFAccount

    def extract_common_fields(self, data):
        attributes = data.get('data').get('attributes')
        return dict(
            # we could put more fields here later
            # the api has much more available, just not sure how much we need right now
            username=self.extract_uid(data),
            first_name=attributes.get('given_name'),
            last_name=attributes.get('family_name'),
        )

    def extract_uid(self, data):
        return str(data.get('data').get('id'))

    def get_default_scope(self):
        return OsfOauth2AdapterConfig.default_scopes

providers.registry.register(OSFProvider)