def create(cls, user, email, obj, role=APP_ROLE_READ): """ Invites a user to an object. Params are supposed to be: user invites email to obj with role This will find the user if he already exists. """ cls._setup_lookups() if role not in APP_ROLES: raise ex.AppException('Check your role(%s) param' % (type, role), ex.INVALID) if not obj or not user or type(obj).__name__ not in cls.put_types: raise ex.AppException('Check your user and org params. They must not be None.' % (type, role), ex.INVALID) type_ = cls.put_types[type(obj).__name__].strip() invited = Session.query(User).filter_by(email=email).first() invites = Session.query(Invite).filter_by(invited_email=email, object_id=obj.id, type=type_) invites = invites.filter(Invite.status.in_([STATUS_APPROVED, STATUS_PENDING])).first() if (invited and obj.get_role(invited)) or invites: raise ex.AppException('User has already been added to this %s' % type(obj).__name__, ex.DUPLICATE) inv = Invite(role=role, type=type_, invited_email=email, user=user, invited_user=invited, object_id=obj.id, status=STATUS_PENDING) Session.flush() Session.add(activity.InviteEvent(user, inv)) return inv
def _create_parent_directories(self): """ Check if the parent directory exists and if it doesn't create it. self.path is the path part of the full filepath already so name here is actually the name of the directory. """ path = self.path.lstrip("/") if not path: # if path is '' then there's no parent to create because # we are at root. return segments = path.split(u"/") current_path = u"/" for segment in segments: q = Session.query(Directory) q = q.filter_by(path=current_path) q = q.filter_by(name=segment) directory = q.first() if not directory: directory = Directory(path=current_path, name=segment, project=self.project) Session.add(directory) current_path = directory.full_path
def set_completion_status(self, user, status): cs = CommentStatus(user=user, status=status, comment=self) Session.add(cs) Session.flush() Session.add(activity.CommentComplete(cs)) return cs
def __init__(self, **kwargs): super(Project, self).__init__(**kwargs) if not self.slug: self.slug = get_unique_slug(self.organization, kwargs['name']) creator = kwargs.get('creator') if creator: self.attach_user(creator, APP_ROLE_ADMIN) Session.add(activity.NewProject(creator, self))
def deactivate(self, user): """ Deactivate this project. Since there is a unique contraint in the organization_id/name tuple we also rename the project using the unique eid. """ self.status = STATUS_INACTIVE self.name = "%s-%s" % (self.eid, self.name) self.update_activity() Session.add(activity.DeleteProject(user, self))
def set_preference(self, key, val): """ Sets a user's preference """ self._load_prefs() if key in self.preferences_dict: self.preferences_dict[key].value = val pref = self.preferences_dict[key] else: pref = UserPreference(key=key, value=val, user=self) Session.add(pref) self.preferences_dict[key] = pref return pref
def attach_user(self, user, role=APP_ROLE_READ, status=STATUS_APPROVED): cu = Session.query(self.connection_class) \ .filter(getattr(self.connection_class, self.object_name)==self) \ .filter(self.connection_class.user==user).first() if cu: cu.role = role cu.status = status return cu cu = self.connection_class(user=user, role=role, status=status) setattr(cu, self.object_name, self) Session.add(cu) return cu
def create(cls, sid, email, creator=None, send_email=True): from desio.utils import email as email_mod be = cls.get(sid, email) if be: return be be = BetaEmail(sid=sid, email=email.lower(), creator=creator) Session.add(be) if send_email: email_mod.send(email, 'beta_email.txt', reply_to='*****@*****.**') return be
def completion_status(self): """ Users can 'complete' comments. This will get the most recent row from CommentStatus. Should be either 'open' or 'completed' """ cs = Session.query(CommentStatus).filter_by(comment_id=self.id).order_by(sa.desc(CommentStatus.created_date)).first() if not cs: #create a default cs = CommentStatus(user=self.creator, created_date=self.created_date, status=STATUS_OPEN, comment=self) Session.add(cs) Session.flush() return cs
def __init__(self, *args, **kwargs): version = kwargs.get('version', None) super(Change, self).__init__(*args, **kwargs) if version is None: version = self._get_next_version() self.version = version creator = kwargs.get('creator', None) if version == 1: Session.add(activity.NewFile(creator, self.entity)) else: Session.add(activity.NewVersion(creator, self))
def add_change(self, user, filepath, temp_contents_filepath, description): """ Check that file exists, if it does add a Change directly. If it doesn't exist, create the File and then add the change. """ file_object = self.get_file(filepath) if file_object is None: path, name = os.path.split(filepath) file_object = File(path=path, name=name, project=self) Session.add(file_object) return file_object.add_change(user, temp_contents_filepath, description)
def add_directory(self, user, path): """ Check if dir exists, if so return it. If not create dir. """ dir_object = self.get_entities(path, only_type=Directory.TYPE) if dir_object: return dir_object path, name = os.path.split(path) dir_object = Directory(path=path, name=name, project=self) Session.add(dir_object) return dir_object
def add_change(self, user, temp_contents_filepath, description): """ Introduce a new change in the given changeset using the file stored at temp_contents_filepath with the given description for the change. """ size = 0 if temp_contents_filepath: size = os.stat(temp_contents_filepath).st_size change = Change(description=description, size=size, entity=self, project=self.project, creator=user) change.digest = digests.md5_file(temp_contents_filepath) Session.add(change) Session.flush() change.set_contents(temp_contents_filepath) self.update_activity() return change
def accept(self, user=None): """ Accept an invite. Will attach the user to the object with an approved status. """ if self.status != STATUS_PENDING: raise ex.AppException('Invite %r already accepted' % self, ex.INVALID) if not self.invited_user: if not user: raise ex.AppException('Specify user to Invite.accept()', ex.INVALID) if user.email.lower() != self.invited_email.lower(): raise ex.AppException("User's email does not match invite's email in Invite.accept()", ex.INVALID) self.invited_user = user self.status = STATUS_APPROVED obj = self.object obj.attach_user(self.invited_user, self.role, status=STATUS_APPROVED) Session.add(activity.InviteAccept(self.invited_user, self))
def add_comment(self, user, body, x=None, y=None, width=None, height=None, in_reply_to=None): """ Add a new comment to this ChangeExtract. """ entity = None if hasattr(self, 'entity_id'): entity = self.entity elif hasattr(self, 'change'): entity = self.change.entity comment = Comment(creator=user, body=body, x=x, y=y, width=width, height=height, in_reply_to=in_reply_to, **{self._comment_attribute: self}) Session.add(comment) Session.flush() if in_reply_to: Session.add(activity.NewReply(user, entity, comment)) else: Session.add(activity.NewComment(user, entity, comment)) return comment