def assign_responsible(name, form): """ Assigning a responsible to a project. Admins do that without check with any users. For non admins several conditions has to be satisfied: 1) New responsible has to be different user. 2) New responsible should be one of project's users :param name: String. Name of the project :param form: WTForm. Form with data :return: object. Instance of ProjectLog object """ if not form.validate_on_submit(): raise ValueError(form_error_string(form.errors)) uid = form.login.data send = form.send.data user = user_by_id(uid) if "admin" in current_user.permissions(): project = get_project_by_name(name) else: project = check_responsible(name) if user == project.responsible: raise ValueError("User %s is already responsible for the project %s" % (user.full_name(), project.get_name())) if "admin" in current_user.permissions(): task = TaskQueue().project(project).responsible_assign(user).task Task(task).accept() return ProjectLog(project).send_message(send).responsible_assign(task) if user not in project.users: raise ValueError("New responsible has to be one of the project users") task = TaskQueue().project(project).responsible_assign(user).task return ProjectLog(project).responsible_assign(task)
def warn_expired_projects(projects, config): """ Check if end of life of a project resources within time interval from configuration option 'finish_notice' and if a warning has been already sent. Sent warning message if it's not done yet. :return: Nothing """ now = dt.now().replace(tzinfo=timezone.utc) for rec in projects: if not rec.active: continue finish = rec.resources.ttl warn = config[rec.type].finish_notice_dt if warn <= now < finish: debug("Expiring %s, %s" % (rec.name, finish.isoformat())) debug("Checking if warning has been send already") log = ProjectLog(rec) logs = log.after(warn).list() debug("List of log events found: %s" % logs) was_sent = list(filter(lambda x: "Expiring" in x.event, logs)) debug("Events with word Expiring found: %s" % was_sent) if not was_sent: debug("Sending warning cause no previous warning events found") log.expire_warning() db.session.commit() return
def project_create_user(name, form): """ Function which creates a temporary user based on provide info and add a create user task in the task queue :param name: String. Name of the project where a use should be created :param form: Instance of WTForm :return: Instance of a project to which a new user has to be attached and an instance of TmpUser class """ project = check_responsible(name) prenom = form.prenom.data.strip().lower() surname = form.surname.data.strip().lower() email = form.email.data.strip().lower() if User.query.filter(User.email == email).first(): raise ValueError("User with e-mail %s has been registered already" % email) user = TmpUser() user.login = generate_login(prenom, surname) user.password = generate_password(16) user.name = prenom user.surname = surname user.email = email task = TaskQueue().project(project).user_create(user).task if current_user.login and "admin" in current_user.permissions(): Task(task).accept() return ProjectLog(project).user_create(task)
def user_create_by_admin(form): email = form.email.data.strip().lower() if User.query.filter_by(email=email).first(): raise ValueError("User with e-mail %s has been registered already" % email) user = TmpUser() user.name = form.name.data.strip().lower() user.surname = form.surname.data.strip().lower() user.email = email user.login = form.login.data.strip().lower() user.active = True if form.active.data else False user.is_user = True if form.is_user.data else False user.is_responsible = True if form.is_responsible.data else False user.is_manager = True if form.is_manager.data else False user.is_tech = True if form.is_tech.data else False user.is_admin = True if form.is_admin.data else False user.is_committee = True if form.is_committee.data else False real = filter(lambda x: True if x != "None" else False, form.project.data) names = list(real) if not names: raise ValueError("Can't create a user in not existing project: %s" % ", ".join(form.project.data)) for name in names: project = get_project_by_name(name) tid = TaskQueue().project(project).user_create(user).task.id TaskOld(tid).accept() msg = "Add a new user: %s '%s %s <%s>'" % (user.login, user.name, user.surname, user.email) ProjectLog(project).event(msg) return "Add creation of the user %s for %s to the execution queue" % ( user.login, ", ".join(form.project.data))
def web_board_accept(): eid, note, cpu, ext = get_arguments() record = Extensions(eid) record.cpu = cpu record.extend = ext record.accept(note) return jsonify(message=ProjectLog(record.rec.project).accept(record.rec), data={"id": record.id})
def transform_project(ext, date): ext.project.type = ext.transform ext.project.name = "%s%s" % (ext.transform, str(ext.project.id).zfill(3)) ext.project.resources.valid = False ext.project.resources = create_resource(ext.project, ext.hours) msg = "Created based on transformation request ID %s on %s" % (ext.id, date) ext.project.resources.comment = msg return ProjectLog(ext.project).transformed(ext)
def transform(): eid, note, cpu = get_arguments(True) record = Extensions(eid) if cpu > 0: record.cpu = cpu record.extend = False record.transform(note) message = ProjectLog(record.rec.project).transform(record.rec) return record.id, message
def user_delete(uid): user = user_by_id(uid) login = user.login projects = user.project User.query.filter_by(id=uid).delete() db.session.commit() for project in projects: msg = "User %s has been removed from the UserDB by admins" % login ProjectLog(project).event(msg) return "User %s has been removed from the database" % login
def extend_project(pid, ext, date): ext.project.resources.ttl = calculate_ttl(ext.project) ext.project.resources.cpu += ext.hours ext.project.resources.valid = True msg = "CPU value has been extended to %s hours on %s based upon "\ "extension request ID %s" % (ext.hours, date, pid) old_comment = ext.project.resources.comment comment = old_comment.split("\n") if old_comment else [] comment.append(msg) ext.project.resources.comment = "\n".join(comment) return ProjectLog(ext.project).extended(ext)
def web_project_delete_user(): data = request.get_json() if not data: raise ValueError("Expecting application/json requests") pid = check_int(data["project"]) login = check_str(data["login"].strip().lower()) project = get_project_record(pid) users = list(map(lambda x: x.login, project.users)) if login not in users: raise ValueError( "User '%s' seems not to be registered in project '%s'" % (login, project.get_name())) user = get_user_record(login) task = TaskQueue().project(project).user_remove(user).task return jsonify(message=ProjectLog(project).user_delete(task))
def reg_accept(pid, note): rec = get_registration_record(pid) # TEMP code start here p = db.session.query(Project).filter(Project.title.ilike( rec.title)).first() if not p: raise ValueError("Project with title %s not in ProjectDB!" % rec.title) created = p.resources.created ProjectLog(p).created(created) # TEMP code ends here rec.accepted = True rec.accepted_ts = created rec.comment = reg_message(rec.comment, "accept") + note rec.processed = True rec.accepted_ts = created db.session.commit() RequestLog(rec).accept() return accept_message(rec, note)
def suspend_expired_projects(projects): """ Check end of life of resources for all the projects and if the EOL is less the now() the project's active property set to False, project_suspend action is created :return: Nothing """ now = dt.now().replace(tzinfo=timezone.utc) for rec in projects: if not rec.active: continue finish = rec.resources.ttl if now > finish: rec.active = False debug("%s: suspended. Resource expired %s" % (rec.name, finish.isoformat())) ProjectLog(rec).expired() db.session.commit() return
def project_attach_user(name, form): """ Function which attach an existing user to a given project :param name: String. Name of the project to which user should be attached :param form: Instance of WTForm :return: Instance of a project to which a new user has to be attached and an instance of User class """ project = check_responsible(name) uid = form.login.data user = User.query.filter(User.id == uid).first() if not user: raise ValueError("Failed to find user with ID '%s' in database" % uid) if user in project.users: raise ValueError("User %s has been already attached to project %s" % (user.full(), project.get_name())) task = TaskQueue().project(project).user_assign(user).task if "admin" in current_user.permissions(): Task(task).accept() return ProjectLog(project).user_assign(task)
def web_board_ignore(eid): record = Extensions(eid).ignore() return jsonify(message=ProjectLog(record.project).ignore(record), data={"id": record.id})
def web_board_reject(eid): record = reject_extension(eid) return jsonify(message=ProjectLog(record.project).reject(record), data={"id": record.id})
def renew_project(pid, ext, date): ext.project.resources.valid = False ext.project.resources = create_resource(ext.project, ext.hours) msg = "Created based on renewal request ID %s on %s" % (pid, date) ext.project.resources.comment = msg return ProjectLog(ext.project).renewed(ext)
def project_activity(project_name): form = ActivityForm() report = report_activity(project_name, form) return jsonify(message=ProjectLog(report.project).activity_report(report))
def web_project_extend(project_name): form = ExtendForm() record = project_extend(project_name, form) return jsonify(message=ProjectLog(record.project).extend(record))
def web_project_renew(project_name): form = RenewForm() record = project_renew(project_name, form) if not is_activity_report(record): raise ValueError("Please upload an activity report first!") return jsonify(message=ProjectLog(record.project).renew(record))
def web_project_reactivate(project_name): form = ActivateForm() record = project_renew(project_name, form, activate=True) record.activate = True return jsonify(message=ProjectLog(record.project).activate(record))
def web_project_transform(project_name): form = TransForm() form.new.choices = get_transformation_options() record = project_transform(project_name, form) return jsonify(message=ProjectLog(record.project).transform(record))