def crypt(action, data, iv_random=True): try: import os from config import AppConfig config = AppConfig()['crypt'] if not config['is_active']: return data # Clave, debe de ser de 128, 192, o 256 bits, check configuration key = config['key'] # Initialization vector. Almacenado en los 16 primeros bytes del mensaje. # Utilizado para para tener el mismo mensaje encritado con distinto texto. # CBCMode de AES if iv_random: iv = os.urandom(16 * 1024)[0:16] else: # This case should be for the emails iv = ' ' * 16 # La longitud de la informacion a cifrar debe ser multiple de 16 (tamaño de bloque de AES), por eso PADDING. # Garantiza que el valor a cifrar sea multiple del tamaño de bloque padding = ' ' pad = lambda s: s + (16 - len(s) % 16) * padding import gluon.contrib.aes as AES import base64 if action == 'encrypt': return base64.b64encode(iv + AES.new(key, AES.MODE_CBC, iv).encrypt(pad(data))) elif action == 'decrypt': return AES.new(key, AES.MODE_CBC, data[:16]).decrypt(base64.b64decode(data).rstrip(padding))[16:] except Exception as e: logger.error(str(e))
def thumb(image, nx=120, ny=120, gae=False, name='thumb'): ''' :param image: :param nx: :param ny: :param gae: In case you run in google app engine :param name: prefix name for the entire name picture :return:string with the name of the thumb ''' try: if image: if not gae: request = current.request from PIL import Image import os img = Image.open(request.folder + 'uploads/' + image) thumb_size = (int(nx), int(ny)) img.thumbnail(thumb_size, Image.ANTIALIAS) root, ext = os.path.splitext(image) thumb_name = '%s_%s%s' % (root, name, ext) img.save(request.folder + 'uploads/' + thumb_name, 'JPEG', quality=90) return thumb_name else: return image except Exception as e: logger.error(str(e))
def get_attachment_name(self, file_name): try: import re items = re.compile('(?P<table>.*?)\.(?P<field>.*?)\..*').match(file_name) (t, f) = (items.group('table'), items.group('field')) field = self.db[t][f] (name, stream) = field.retrieve(file_name, nameonly=True) except Exception, e: logger.error(str(e)) return None
def action(self, user_id, action): try: query = self.db(self.db.auth_user.id == user_id).select().first() if action == 'block': query.update_record(registration_key="blocked") elif action == 'enable': query.update_record(registration_key='') self.db.commit() except Exception as e: self.db.rollback() logger.error(str(e)) return False return True
def send_email(self, to, subject, event_type, attachments=[], render_html=True, **kwargs): message = self.build_message_from_template(event_type, render_html, **kwargs) try: if attachments: attachment = [] for i in attachments: attachment.append(self.Attachment(i)) params = dict( to=to, subject=subject, attachments=attachment, bcc=self.config.take("smtp.bcc_to"), **message ) else: params = dict(to=to, subject=subject, bcc=self.config.take("smtp.bcc_to"), **message) sent = self.send(**params) except Exception, e: logger.error("Fail sending email to: %s" % to) logger.error(str(e)) sent = False
def thumb(image, nx=120, ny=120, gae=False, name='thumb'): try: if image: if not gae: request = current.request from PIL import Image import os img = Image.open(request.folder + 'uploads/' + image) thumb_size = (int(nx), int(ny)) img.thumbnail(thumb_size, Image.ANTIALIAS) root, ext = os.path.splitext(image) thumb_bin = '%s_%s%s' % (root, name, ext) img.save(request.folder + 'uploads/' + thumb, 'JPEG', quality=90) return thumb_bin else: return image except Exception as e: logger.error(str(e))
def action(self, app_id, action): """The app can be enable or disable :param app_id: :param action: enable or disable :return:True or False """ try: query = self.db(self.db.t_apps.id == app_id).select().first() if action == 'disable': query.update_record(f_is_active=False) elif action == 'enable': query.update_record(f_is_active=True) self.db.commit() except Exception as e: self.db.rollback() logger.error(str(e)) return False return True
def send_email(self, to, event_type, lang=None, render_html=True, bcc=[], **kwargs): lang = lang or self.T.accepted_language mail = Mailer() try: message = self.build_message_from_template(event_type, lang, render_html, **kwargs) if message['attachments'] != ['']: attachment = [] for i in message['attachments']: attachment.append(mail.Attachment('%suploads/' % self.request.folder + i, filename=self.get_attachment_name(i))) message['attachments'] = attachment else: del message['attachments'] if 'bcc' in message: bcc = message['bcc'].split(',') + bcc del message['bcc'] params = dict(to=to, bcc=bcc, **message) sent = mail.send(**params) except Exception, e: logger.error("Fail sending email to: %s" % to) logger.error(str(e)) sent = False