| Conditions | 13 |
| Total Lines | 78 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 1 | Features | 0 |
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:
Complex classes like ServiceWallabag.save_data() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # coding: utf-8 |
||
| 84 | def save_data(self, trigger_id, **data): |
||
| 85 | """ |
||
| 86 | let's save the data |
||
| 87 | |||
| 88 | :param trigger_id: trigger ID from which to save data |
||
| 89 | :param data: the data to check to be used and save |
||
| 90 | :type trigger_id: int |
||
| 91 | :type data: dict |
||
| 92 | :return: the status of the save statement |
||
| 93 | :rtype: boolean |
||
| 94 | """ |
||
| 95 | from th_wallabag.models import Wallabag |
||
| 96 | |||
| 97 | status = False |
||
| 98 | |||
| 99 | if self.token and 'link' in data and data['link'] is not None\ |
||
| 100 | and len(data['link']) > 0: |
||
| 101 | # get the data of this trigger |
||
| 102 | trigger = Wallabag.objects.get(trigger_id=trigger_id) |
||
| 103 | |||
| 104 | title = self.set_title(data) |
||
| 105 | # convert htmlentities |
||
| 106 | title = HtmlEntities(title).html_entity_decode |
||
| 107 | |||
| 108 | if self.us_not_found: |
||
| 109 | us = UserService.objects.get(id=data['userservice_id']) |
||
| 110 | self.service_host = us.host |
||
| 111 | self.service_client_secret = us.client_secret |
||
| 112 | self.service_client_id = us.client_id |
||
| 113 | self.token = us.token |
||
| 114 | |||
| 115 | self.wall = Wall(host=self.service_host, |
||
| 116 | client_secret=self.service_client_secret, |
||
| 117 | client_id=self.service_client_id, |
||
| 118 | token=self.token) |
||
| 119 | |||
| 120 | try: |
||
| 121 | self.wall.post_entries(url=data['link'], |
||
| 122 | title=title, |
||
| 123 | tags=(trigger.tag.lower())) |
||
| 124 | |||
| 125 | sentence = str('wallabag {} created').format(data['link']) |
||
| 126 | logger.debug(sentence) |
||
| 127 | status = True |
||
| 128 | except Exception as e: |
||
| 129 | if e.errno == 401: |
||
| 130 | new_token = self.refresh_token() |
||
| 131 | UserService.objects.filter( |
||
| 132 | id=data['userservice_id']).update( |
||
| 133 | token=new_token) |
||
| 134 | |||
| 135 | # new wallabag session with new token |
||
| 136 | new_wall = Wall(host=self.service_host, |
||
| 137 | client_secret=self.service_client_secret, |
||
| 138 | client_id=self.service_client_id, |
||
| 139 | token=new_token) |
||
| 140 | try: |
||
| 141 | return new_wall.post_entries(url=data['link'], |
||
| 142 | title=title, |
||
| 143 | tags=(trigger.tag.lower()) |
||
| 144 | ) |
||
| 145 | except Exception as e: |
||
| 146 | logger.critical(e.errno, e.strerror) |
||
| 147 | status = False |
||
| 148 | else: |
||
| 149 | logger.critical(e.errno, e.strerror) |
||
| 150 | status = False |
||
| 151 | |||
| 152 | elif self.token and 'link' in data and data['link'] is not None\ |
||
| 153 | and len(data['link']) == 0: |
||
| 154 | logger.warning( |
||
| 155 | "no link provided for trigger ID {}, so we ignore it".format(trigger_id)) |
||
| 156 | status = True |
||
| 157 | else: |
||
| 158 | logger.critical( |
||
| 159 | "no token provided for trigger ID {}".format(trigger_id)) |
||
| 160 | status = False |
||
| 161 | return status |
||
| 162 | |||
| 210 |