Conditions | 8 |
Total Lines | 55 |
Lines | 0 |
Ratio | 0 % |
Tests | 1 |
CRAP Score | 65.3873 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | 1 | from plugin.managers.core.base import Manager, Update |
|
103 | 1 | def from_pin(self, t_account, pin): |
|
104 | if not pin: |
||
105 | log.debug('"pin" parameter is empty, ignoring account authorization update') |
||
106 | return t_account |
||
107 | |||
108 | # Retrieve current account `OAuthCredential` |
||
109 | oauth = t_account.oauth_credentials.first() |
||
110 | |||
111 | if oauth and oauth.code == pin: |
||
112 | log.debug("PIN hasn't changed, ignoring account authorization update") |
||
113 | return t_account |
||
114 | |||
115 | if not oauth: |
||
116 | # Create new `OAuthCredential` for the account |
||
117 | oauth = TraktOAuthCredential( |
||
118 | account=t_account |
||
119 | ) |
||
120 | |||
121 | # Update `OAuthCredential` |
||
122 | if not TraktOAuthCredentialManager.update.from_pin(oauth, pin, save=False): |
||
123 | log.warn("Unable to update OAuthCredential (token exchange failed, hasn't changed, etc..)") |
||
124 | return None |
||
125 | |||
126 | # Validate the account authorization |
||
127 | with t_account.oauth_authorization(oauth).http(retry=True): |
||
128 | settings = Trakt['users/settings'].get() |
||
129 | |||
130 | if not settings: |
||
131 | log.warn('Unable to retrieve account details for authorization') |
||
132 | return None |
||
133 | |||
134 | # Update `TraktAccount` username |
||
135 | try: |
||
136 | self.update_username(t_account, settings) |
||
137 | except (apsw.ConstraintError, peewee.IntegrityError), ex: |
||
138 | log.debug('Trakt account already exists - %s', ex, exc_info=True) |
||
139 | |||
140 | raise TraktAccountExistsException('Trakt account already exists') |
||
141 | |||
142 | # Save oauth credential changes |
||
143 | oauth.save() |
||
144 | |||
145 | # Refresh `TraktAccount` |
||
146 | t_account.refresh( |
||
147 | force=True, |
||
148 | settings=settings |
||
149 | ) |
||
150 | |||
151 | # Refresh `Account` |
||
152 | t_account.account.refresh( |
||
153 | force=True |
||
154 | ) |
||
155 | |||
156 | log.info('Updated account authorization for %r', t_account) |
||
157 | return t_account |
||
158 | |||
207 |