def __init__(self, char_id): self.char_id = char_id try: self.mail = MongoMail.objects.get(id=self.char_id) except DoesNotExist: self.mail = MongoMail(id=self.char_id) self.mail.mails = {} self.mail.save()
def __init__(self, char_id, mailobj=None): self.char_id = char_id if mailobj: self.mail = mailobj else: try: self.mail = MongoMail.objects.get(id=self.char_id) except DoesNotExist: self.mail = MongoMail(id=self.char_id) self.mail.mails = {} self.mail.save()
def clean(signum): logger = Logger("clean_mail.log") logger.write("Start") try: amount = 0 mails = MongoMail._get_collection().find({}, {'_id': 1}) for m in mails: char_id = m['_id'] char_mail = Mail(char_id) cleaned = char_mail.delete_expired(MAIL_KEEP_DAYS) amount += cleaned except: logger.error(traceback.format_exc()) else: logger.write("Clean Mail Complete. Cleaned Amount: {0}".format(amount)) finally: logger.close()
class Mail(object): def __init__(self, char_id): self.char_id = char_id try: self.mail = MongoMail.objects.get(id=self.char_id) except DoesNotExist: self.mail = MongoMail(id=self.char_id) self.mail.mails = {} self.mail.save() def count(self): return len(self.mail.mails) def _get_mail_id(self): mail_ids = [int(i) for i in self.mail.mails.keys()] if not mail_ids: mail_id = 1 else: mail_id = max(mail_ids) + 1 return mail_id def _make_mail_obj(self, name, only_one): mail_id = self._get_mail_id() if not only_one: return (mail_id, MongoEmbededMail()) for k, v in self.mail.mails.iteritems(): if v.name == name: return (int(k), v) return (mail_id, MongoEmbededMail()) def add(self, name, content, create_at=None, attachment='', only_one=False, send_notify=True): if not isinstance(name, unicode): name = name.decode('utf-8') if not isinstance(content, unicode): content = content.decode('utf-8') mail_id, m = self._make_mail_obj(name, only_one) m.name = name m.content = content m.attachment = attachment m.has_read = False m.create_at = arrow.utcnow().format(MAIL_ARROW_FORMAT) self.mail.mails[str(mail_id)] = m self.mail.save() if send_notify: self.send_notify() return mail_id def delete(self, mail_id): try: self.mail.mails.pop(str(mail_id)) except KeyError: raise SanguoException(errormsg.MAIL_NOT_EXIST, self.char_id, "Mail Delete", "mail {0} not exist".format(mail_id)) self.mail.save() self.send_notify() def delete_expired(self, mail_keep_days, send_notify=True): delete_ids = [] day = arrow.utcnow().replace(days=-mail_keep_days) for k, v in self.mail.mails.iteritems(): create_at = arrow.get(v.create_at, MAIL_ARROW_FORMAT) if create_at < day: delete_ids.append(k) for i in delete_ids: self.mail.mails.pop(i) if len(self.mail.mails) == 0: self.mail.delete() else: self.mail.save() if send_notify: self.send_notify() return len(delete_ids) def open(self, mail_id): try: self.mail.mails[str(mail_id)].has_read = True except KeyError: raise SanguoException(errormsg.MAIL_NOT_EXIST, self.char_id, "Mail Open", "mail {0} not exist".format(mail_id)) self.mail.save() self.send_notify() def get_attachment(self, mail_id): if str(mail_id) not in self.mail.mails: raise SanguoException(errormsg.MAIL_NOT_EXIST, self.char_id, "Mail Get Attachment", "mail {0} not exist".format(mail_id)) if not self.mail.mails[str(mail_id)].attachment: raise SanguoException(errormsg.MAIL_HAS_NO_ATTACHMENT, self.char_id, "Mail Get Attachment", "mail {0} has no attachment".format(mail_id)) resource = Resource(self.char_id, "Mail Attachment") attachment = json.loads(self.mail.mails[str(mail_id)].attachment) resource.add(**attachment) self.mail.mails[str(mail_id)].attachment = '' self.mail.mails[str(mail_id)].has_read = True self.mail.save() self.send_notify() return attachment def send_notify(self): msg = protomsg.MailNotify() for k, v in self.mail.mails.iteritems(): m = msg.mails.add() m.id = int(k) m.name = v.name m.content = v.content m.has_read = v.has_read if v.attachment: m.attachment.MergeFrom( standard_drop_to_attachment_protomsg( json.loads(v.attachment))) m.start_at = arrow.get(v.create_at, "YYYY-MM-DD HH:mm:ss").timestamp m.max_days = MAIL_KEEP_DAYS publish_to_char(self.char_id, pack_msg(msg))
class Mail(object): def __init__(self, char_id, mailobj=None): self.char_id = char_id if mailobj: self.mail = mailobj else: try: self.mail = MongoMail.objects.get(id=self.char_id) except DoesNotExist: self.mail = MongoMail(id=self.char_id) self.mail.mails = {} self.mail.save() def count(self): return len(self.mail.mails) def _get_mail_id(self): mail_ids = [int(i) for i in self.mail.mails.keys()] if not mail_ids: mail_id = 1 else: mail_id = max(mail_ids) + 1 return mail_id def _make_mail_obj(self, name, only_one): mail_id = self._get_mail_id() if not only_one: return (mail_id, MongoEmbededMail()) for k, v in self.mail.mails.iteritems(): if v.name == name: return (int(k), v) return (mail_id, MongoEmbededMail()) def add(self, name, content, create_at=None, attachment='', only_one=False, send_notify=True): if not isinstance(name, unicode): name = name.decode('utf-8') if not isinstance(content, unicode): content = content.decode('utf-8') mail_id, m = self._make_mail_obj(name, only_one) m.name = name m.content = content m.attachment = attachment m.has_read = False m.create_at = arrow.utcnow().format('YYYY-MM-DD HH:mm:ss') self.mail.mails[str(mail_id)] = m self.mail.save() if send_notify: self.send_notify() return mail_id def delete(self, mail_id): try: self.mail.mails.pop(str(mail_id)) except KeyError: raise SanguoException( errormsg.MAIL_NOT_EXIST, self.char_id, "Mail Delete", "mail {0} not exist".format(mail_id) ) self.mail.save() self.send_notify() def open(self, mail_id): try: self.mail.mails[str(mail_id)].has_read = True except KeyError: raise SanguoException( errormsg.MAIL_NOT_EXIST, self.char_id, "Mail Open", "mail {0} not exist".format(mail_id) ) self.mail.save() self.send_notify() def get_attachment(self, mail_id): if str(mail_id) not in self.mail.mails: raise SanguoException( errormsg.MAIL_NOT_EXIST, self.char_id, "Mail Get Attachment", "mail {0} not exist".format(mail_id) ) if not self.mail.mails[str(mail_id)].attachment: raise SanguoException( errormsg.MAIL_HAS_NO_ATTACHMENT, self.char_id, "Mail Get Attachment", "mail {0} has no attachment".format(mail_id) ) resource = Resource(self.char_id, "Mail Attachment") attachment = json.loads(self.mail.mails[str(mail_id)].attachment) resource.add(**attachment) self.mail.mails[str(mail_id)].attachment = '' self.mail.mails[str(mail_id)].has_read = True self.mail.save() self.send_notify() return attachment def send_notify(self): msg = protomsg.MailNotify() for k, v in self.mail.mails.iteritems(): m = msg.mails.add() m.id = int(k) m.name = v.name m.content = v.content m.has_read = v.has_read if v.attachment: m.attachment.MergeFrom( standard_drop_to_attachment_protomsg(json.loads(v.attachment)) ) m.start_at = arrow.get(v.create_at, "YYYY-MM-DD HH:mm:ss").timestamp m.max_days = MAIL_KEEP_DAYS publish_to_char(self.char_id, pack_msg(msg))
class Mail(object): def __init__(self, char_id, mailobj=None): self.char_id = char_id if mailobj: self.mail = mailobj else: try: self.mail = MongoMail.objects.get(id=self.char_id) except DoesNotExist: self.mail = MongoMail(id=self.char_id) self.mail.mails = {} self.mail.save() def count(self): return len(self.mail.mails) def add(self, name, content, create_at, attachment=''): if not isinstance(name, unicode): name = name.decode('utf-8') if not isinstance(content, unicode): content = content.decode('utf-8') m = MongoEmbededMail() m.name = name m.content = content m.attachment = attachment m.has_read = False m.create_at = create_at mail_ids = [int(i) for i in self.mail.mails.keys()] if not mail_ids: mail_id = 1 else: mail_id = max(mail_ids) + 1 self.mail.mails[str(mail_id)] = m self.mail.save() self.send_mail_notify() return mail_id def delete(self, mail_id): try: self.mail.mails.pop(str(mail_id)) except KeyError: raise SanguoException( errormsg.MAIL_NOT_EXIST, self.char_id, "Mail Delete", "mail {0} not exist".format(mail_id) ) self.mail.save() self.send_mail_notify() def open(self, mail_id): try: self.mail.mails[str(mail_id)].has_read = True except KeyError: raise SanguoException( errormsg.MAIL_NOT_EXIST, self.char_id, "Mail Open", "mail {0} not exist".format(mail_id) ) self.mail.save() self.send_mail_notify() def get_attachment(self, mail_id): if str(mail_id) not in self.mail.mails: raise SanguoException( errormsg.MAIL_NOT_EXIST, self.char_id, "Mail Get Attachment", "mail {0} not exist".format(mail_id) ) if not self.mail.mails[str(mail_id)].attachment: raise SanguoException( errormsg.MAIL_HAS_NO_ATTACHMENT, self.char_id, "Mail Get Attachment", "mail {0} has no attachment".format(mail_id) ) resource = Resource(self.char_id, "Mail Attachment") attachment = json.loads(self.mail.mails[str(mail_id)].attachment) resource.add(**attachment) self.mail.mails[str(mail_id)].attachment = '' self.mail.mails[str(mail_id)].has_read = True self.mail.save() self.send_mail_notify() def send_mail_notify(self): msg = protomsg.MailNotify() for k, v in self.mail.mails.iteritems(): m = msg.mails.add() m.id = int(k) m.name = v.name m.content = v.content m.has_read = v.has_read if v.attachment: m.attachment.MergeFrom( standard_drop_to_attachment_protomsg(json.loads(v.attachment), is_prepare=True) ) create_at = datetime.datetime.strptime(v.create_at, FORMAT) m.start_at = int(create_at.strftime('%s')) # m.start_at = v.create_at m.max_days = MAIL_KEEP_DAYS publish_to_char(self.char_id, pack_msg(msg))