Akron | ca9bd98 | 2016-12-06 16:59:57 +0100 | [diff] [blame] | 1 | import os |
| 2 | |
| 3 | __author__ = 'hanl' |
| 4 | |
| 5 | path = os.path.dirname(__file__) |
| 6 | # modify this to change the Template Directory |
| 7 | TEMPLATE_DIR = '/templates/' |
| 8 | |
| 9 | |
| 10 | class Handler(object): |
| 11 | def register(self, key, value): |
| 12 | setattr(self, key, value) |
| 13 | |
| 14 | |
| 15 | def get(self, key): |
| 16 | if hasattr(self, key): |
| 17 | return getattr(self, key) |
| 18 | |
| 19 | |
| 20 | |
| 21 | # for possible email usage |
| 22 | class MessageTemplate(object): |
| 23 | def __init__(self, template_name=None, html=True, values={}): |
| 24 | self.values = values |
| 25 | self.template_name = template_name |
| 26 | self.html = html |
| 27 | |
| 28 | def render(self): |
| 29 | content = open(os.path.join(path, TEMPLATE_DIR, self.template_name)).read() |
| 30 | for k, v in self.values.iteritems(): |
| 31 | content = content.replace('%%s%%' % k, v) |
| 32 | return content |
| 33 | |
| 34 | |
| 35 | class HtmlBuilder(object): |
| 36 | def __init__(self): |
| 37 | self.title = None |
| 38 | self.head = None |
| 39 | self.body = None |
| 40 | |
| 41 | def appendHead(self, head): |
| 42 | if not self.head: |
| 43 | self.head = "" |
| 44 | self.head.append("\n" + head) |
| 45 | |
| 46 | def appendBody(self, body): |
| 47 | if not self.body: |
| 48 | self.body = "" |
| 49 | self.head.append("\n" + body) |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | class NotificationHandler(object): |
| 55 | |
| 56 | @staticmethod |
| 57 | def isError(response): |
| 58 | try: |
| 59 | raw_json = response.json() |
| 60 | if raw_json.get('errors') or raw_json.get('error') or raw_json.get('err'): |
| 61 | return True |
| 62 | except ValueError: |
| 63 | if response.status_code != 200: |
| 64 | return True |
| 65 | return False |
| 66 | |
| 67 | @staticmethod |
| 68 | def getMessage(response): |
| 69 | pass |
| 70 | |
| 71 | @staticmethod |
| 72 | def notify(json, notify_func, **kwargs): |
| 73 | if "category" not in kwargs.keys(): |
| 74 | kwargs['category'] = "danger" |
| 75 | |
| 76 | if json.get('errors'): |
| 77 | for error in json.get("errors"): |
| 78 | notify_func(error[1], "danger") |
| 79 | elif json.get('error'): |
| 80 | if json.get('error_description'): |
| 81 | notify_func(json.get('error_description'), **kwargs) |
| 82 | else: |
| 83 | notify_func(json.get('error'), **kwargs) |
| 84 | elif json.get('err'): |
| 85 | notify_func(json.get('errstr'), **kwargs) |
| 86 | |
| 87 | @staticmethod |
| 88 | def getLocalized(code): |
| 89 | # return _("status_" + str(code)) |
| 90 | return "this is a default message" |
| 91 | |