def visa_create(self, resend=False): """ Creates visa files and attaches them to a mail for responsible person. Afterwards delete files from disk and set proper status for register record. :return: Object. Pending object """ record = self.verify() name = record.project_id() status = record.status.upper() if ("APPROVED" not in status) and ("VISA SENT" not in status): raise ValueError("Project %s has to be approved first!" % name) if ("VISA SENT" in status) and not resend: raise ValueError("Visa for project %s has been already sent" % name) mail = Mail() u_list = mail.cfg.get("DEFAULT", "USER_LIST", fallback=None) r_list = mail.cfg.get("DEFAULT", "RESPONSIBLE_LIST", fallback=None) record.user_list = "(%s)" % u_list if u_list else "" record.resp_list = "(%s)" % r_list if r_list else "" path = create_visa(record) if not path: raise ValueError("Failed to generate visa document") mail.registration(record).visa_attach(path).start() map(lambda x: Path(x).unlink(), path) debug("Temporary file(s) %s was deleted" % ",".join(path)) record.status = "visa sent" if resend: self.result = RequestLog(record).visa_resent() else: self.result = RequestLog(record).visa_sent() return self.commit()
def send_reset_passwd_notice(user_obj, email_subj=None, email_html=None, relative_url=None): if email_subj is None: email_subj = "Password Reset" if email_html is None: email_html = read_template("email/reset_passwd.html") if relative_url is None: relative_url = "/user/reset-password/" token = generate_token(user_obj, AccountActivity.RESET_PASSWORD) url = "%s%s%s/%s/" % (PLOP_DOMAIN, relative_url, user_obj.id(), token) email_html = email_html % {"url": url} mail = Mail(MAIL_OUTBOUND_REALNAME, MAIL_OUTBOUND_REPLYTO) mail.send(user_obj.email, email_subj, email_html, async=True)
def send_confirmation(user_obj, email_subject=None, email_html=None, relative_url=None): if email_subject is None: email_subject = "Complete your account registration" if email_html is None: email_html = read_template("email/verify_email.html") if relative_url is None: relative_url = "/register/confirm/" token = generate_token(user_obj, AccountActivity.VERIFY_EMAIL_ADDR) url = "%s%s%s/%s/" % (PLOP_DOMAIN, relative_url, user_obj.id(), token) email_html = email_html % {"url": url} mail = Mail(MAIL_OUTBOUND_REALNAME, MAIL_OUTBOUND_REPLYTO) mail.send(user_obj.email, email_subject, email_html, async=True)
def user_create(self): """ Re-create TmpUser object user out of task description, create DB entry for User record and ACL record and append ne user to project associated with the task. :return: String. The log event associated with this action """ project = self.task.project tmp_user = TmpUser().from_task(self) acl = ACLDB(is_user=tmp_user.is_user, is_responsible=tmp_user.is_responsible, is_tech=tmp_user.is_tech, is_manager=tmp_user.is_manager, is_committee=tmp_user.is_committee, is_admin=tmp_user.is_admin) user = User(login=tmp_user.login, name=tmp_user.name, surname=tmp_user.surname, email=tmp_user.email, active=tmp_user.active, acl=acl, project=[project], created=dt.now()) db.session.add(acl) db.session.add(user) project.users.append(user) Mail().user_new(tmp_user) return ProjectLog(project).user_created(self.task)
def user_update(self, info): changes = [] for name, value in info.items(): old = getattr(self.user, name) prop = name.capitalize() changes.append("%s change: %s -> %s" % (prop, old, value)) self.log.event = "; ".join(changes) return self.commit(Mail().user_update(self))
def commit(self, mail=None): db.session.add(self.log) db.session.commit() Mail().log(self.log).start() try: if mail and self.send: mail.start() finally: return self.log.event
def accept(self): """ Marking the task for execution in case of positive decision. Sending mail with technical details to tech stuff by default :return: Object. Task record """ self.task.decision = "accept" Mail().task_accepted(self.task).send() return self.process()
def reject(self): """ Marking the task as rejected in case of negative decision. Sending mail with technical details to tech stuff by default Task is marked as done and shouldn't be in the task queue for execution :return: Object. Task record """ self.task.decision = "reject" Mail().task_rejected(self.task).send() self.process() return self.done()
def register_message(rid, form): """ Send message to new registered project responsible. Takes request record ID and wtforms object as argument extract data from it and construct dict with keys "title", "body" and "destination". Finally in calls pending_message method of Mail object with email dict as an argument for this method. :param rid: Int. ID of new project request record :param form: Object. Copy of WTForms form object :return: """ record = get_registration_record(rid) pid = record.project_id() email = { "title": "[%s] %s" % (pid, form.title.data), "body": form.message.data, "destination": record.responsible_email } return Mail().pending_message(email)
def info_update(self, info=None, acl=None, projects=None, active=None): changes = [] if info is not None: for name, value in info.items(): old = getattr(self.user, name) prop = name.capitalize() changes.append("%s change: %s -> %s" % (prop, old, value)) if acl is not None: for name, value in acl.items(): old = getattr(self.user.acl, name) changes.append("ACL %s change %s -> %s" % (name, old, value)) if active is not None: old = self.user.active changes.append("Set active status from %s to '%s'" % (old, active)) if projects is not None: old = self.user.project_names() for name in projects: if name in old: changes.append("Add to project %s" % name) else: changes.append("Remove from project %s" % name) result = "; ".join(changes) self.log.event = "User information changes requested: %s" % result return self.commit(Mail().user_update(self))
def transformed(self, extension): self.log.event = "Transformation to type %s finished successfully" \ % extension.transform self.log.extension = extension return self.commit(Mail().project_transformed(extension))
def activate(self, extension): self.log.event = "Activation request has been registered" self.log.extension = extension return self.commit(Mail().project_activate(extension))
def user_updated(self, task): full = task.user.full() self.log.event = "Modifications for user %s has been applied" % full return self.commit(Mail().user_updated(self))
def activity_report(self, file_rec): file_name = file_rec.path self.log.event = "Activity report saved on the server in the file %s" \ % file_name mail = Mail().report_uploaded(file_rec).attach_file(file_name) return self.commit(mail)
def ignore(self): self.log.event = "Project creation request ignored" return self.commit(Mail().pending_ignore(self))
def ignore(self, extension): new = "Extension" if extension.extend else "Renewal" self.log.event = "%s request for %s hours is ignored" \ % (new, extension.hours) self.log.extension = extension return self.commit(Mail().allocation_ignored(extension, new))
def extend(self, extension): article = "an exceptional" if extension.exception else "a" self.log.event = "Made %s request to extend project for %s hour(s)" \ % (article, extension.hours) self.log.extension = extension return self.commit(Mail().project_extend(extension))
def responsible_assign(self, task): self.log.event = "Made a request to assign new responsible %s" \ % task.user.full() return self.user(task.user).commit(Mail().responsible_assign(task))
def user_create(self, task): user = TmpUser().from_task(Task(task)) user.task = task self.log.event = "Made a request to create a user %s" % user.full() return self.commit(Mail().user_create(user))
def visa_received(self): self.log.event = "Visa received" return self.commit(Mail().visa_received(self.pending))
def expire_warning(self): self.log.event = "Expiring message sent" return self.commit(Mail().project_expiring(self.project))
def expired(self): self.log.event = "Project is expired and deactivated. Notification sent" return self.commit(Mail().project_expired(self.project))
def transform(self, extension): self.log.event = "Transformation request has been registered" self.log.extension = extension return self.commit(Mail().project_transform(extension))
def reset(self): self.log.event = "Project creation process has been reset" return self.commit(Mail().pending_reset(self))
def extended(self, extension): self.log.event = "Extension request for %s hour(s) has been processed" \ % extension.hours self.log.extension = extension return self.commit(Mail().project_extended(extension))
def reject(self, message): self.log.event = "Project creation request rejected" return self.commit(Mail().pending_reject(self, message))
def visa_sent(self): self.log.event = "Visa sent to %s" % self.pending.responsible_email return self.commit(Mail().visa_sent(self))
def reject(self, extension): ext_or_new = "Extension" if extension.extend else "Renewal" self.log.event = "%s request for %s hours is rejected" \ % (ext_or_new, extension.hours) self.log.extension = extension return self.commit(Mail().allocation_rejected(extension, ext_or_new))
def responsible_assigned(self, task): self.log.event = "Assigned a new project responsible %s" \ % task.user.full() return self.user(task.user).commit(Mail().responsible_assigned(task))
def visa_skip(self): self.log.event = "Visa sending step has been skipped" return self.commit(Mail().visa_skip(self))
def user_created(self, task): user = TmpUser().from_task(Task(task)) user.task = task self.log.event = "User %s has been created" % user.full() return self.commit(Mail().user_created(user))
def approve(self): self.log.event = "Project software requirements approved" return self.commit(Mail().pending_approve(self))