| Conditions | 12 |
| Total Lines | 61 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| 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 |
||
| 78 | def save_data(self, trigger_id, **data): |
||
| 79 | """ |
||
| 80 | let's save the data |
||
| 81 | |||
| 82 | :param trigger_id: trigger ID from which to save data |
||
| 83 | :param data: the data to check to be used and save |
||
| 84 | :type trigger_id: int |
||
| 85 | :type data: dict |
||
| 86 | :return: the status of the save statement |
||
| 87 | :rtype: boolean |
||
| 88 | """ |
||
| 89 | from th_wallabag.models import Wallabag |
||
| 90 | |||
| 91 | if self.token and 'link' in data and data['link'] is not None\ |
||
| 92 | and len(data['link']) > 0: |
||
| 93 | # get the data of this trigger |
||
| 94 | trigger = Wallabag.objects.get(trigger_id=trigger_id) |
||
| 95 | |||
| 96 | title = self.set_title(data) |
||
| 97 | # convert htmlentities |
||
| 98 | title = HtmlEntities(title).html_entity_decode |
||
| 99 | |||
| 100 | try: |
||
| 101 | self.wall.post_entries(url=data['link'], |
||
| 102 | title=title, |
||
| 103 | tags=(trigger.tag.lower())) |
||
| 104 | |||
| 105 | sentence = str('wallabag {} created').format(data['link']) |
||
| 106 | logger.debug(sentence) |
||
| 107 | status = True |
||
| 108 | except Exception as e: |
||
| 109 | if e.errno == 401: |
||
| 110 | new_token = self.refresh_token() |
||
| 111 | UserService.objects.filter( |
||
| 112 | id=data['userservice_id']).update( |
||
| 113 | token=new_token) |
||
| 114 | |||
| 115 | # new wallabag session with new token |
||
| 116 | new_wall = self.new_wall(new_token) |
||
| 117 | try: |
||
| 118 | return new_wall.post_entries(url=data['link'], |
||
| 119 | title=title, |
||
| 120 | tags=(trigger.tag.lower()) |
||
| 121 | ) |
||
| 122 | except Exception as e: |
||
| 123 | logger.critical(e.errno, e.strerror) |
||
| 124 | status = False |
||
| 125 | else: |
||
| 126 | logger.critical(e.errno, e.strerror) |
||
| 127 | status = False |
||
| 128 | |||
| 129 | elif self.token and 'link' in data and data['link'] is not None\ |
||
| 130 | and len(data['link']) == 0: |
||
| 131 | logger.warning( |
||
| 132 | "no link provided for trigger ID {}, so we ignore it".format(trigger_id)) |
||
| 133 | status = True |
||
| 134 | else: |
||
| 135 | logger.critical( |
||
| 136 | "no token provided for trigger ID {}".format(trigger_id)) |
||
| 137 | status = False |
||
| 138 | return status |
||
| 139 | |||
| 187 |