def upload_extract(real_user, user, change, **kw): """ File is binary in the request body. """ from pylons import request order_index = int(to_unicode(request.headers.get('X-Up-Order-Index'))) type = to_unicode(request.headers.get('X-Up-Type')) if not type: return None f, tmpname = tempfile.mkstemp('png') if isinstance(f, int): f = open(tmpname, 'wb') #this is inefficient. Pylons supposedly already creates a tmp file. We are instead #reading the thing into memory. I'm lazy until this beomes an issue (prolly soon) #V: please make this not suck f.write(request.environ['wsgi.input'].read()) f.close() new = False change_extract = Session.query(projects.ChangeExtract).filter_by(change=change, extract_type=type, order_index=order_index).first() if not change_extract: new = True change_extract = projects.ChangeExtract(change=change, extract_type=type, order_index=order_index) Session.add(change_extract) change_extract.set_contents(tmpname) return new and 'new' or 'existing'
def project_user_module(real_user, user, organization, project=None): """ """ # email: user def list org_users = api.organization.get_users(real_user, user, organization, status=STATUS_APPROVED) user_map = dict([(ou.user.email, v1.user.get().output(ou.user)) for ou in org_users]) #email list for lookup emails = [u.user.email for u in org_users] # list of users in the project and their role invite_url = None proj_users = [] if project: invites = Session.query(users.Invite).filter_by(type=users.INVITE_TYPE_PROJECT, object_id=project.id, status=STATUS_PENDING) invites = [{'invite': True, 'user': {'name': i.invited_email}} for i in invites] proj_users = api.project.get_users(real_user, user, project, status=STATUS_APPROVED) proj_users = v1.project.get_users().output(proj_users) + invites invite_url = h.api_url('invite', 'create', project=c.project.eid) params = { 'emails': emails, 'projectUsers': proj_users, 'userMap': user_map, 'roleUrl': h.api_url('project', 'set_user_role', project=project and project.eid or None), 'syncUrl': h.api_url('project', 'attach_users'), 'inviteUrl': invite_url } return params
def create(**params): """ Creates a user. DO NOT EXPOSE THIS to the web api. Please. """ numusers = len(Session.query(users.User).all()) scrubbed = validate(RegisterForm, **params) logger.info(scrubbed) user = users.User() Session.add(user) user.email = scrubbed.email user.username = '******' in scrubbed and scrubbed.username or scrubbed.email user.password = scrubbed.password user.set_timezone_int(scrubbed.default_timezone) if scrubbed.get('name'): name = scrubbed.get('name').split(' ', 1) user.first_name = name[0].strip() user.last_name = len(name) == 2 and name[1].strip() or u'' else: user.first_name = scrubbed.get('first_name') user.last_name = scrubbed.get('last_name') #first user is an admin. if numusers == 0: user.role = users.ROLE_ADMIN return user
def _to_python(self, value, state): # we don't support multiple values, so we run a quick check here (we got a webapp where this was a problem) if type(value) != type(u""): raise fv.Invalid('You must supply a valid email.', value, state) user = Session.query(users.User).filter(sa.func.lower(users.User.email)==value.lower()).first() # if this user is the same as the logged in one then don't throw the error - allows keeping old email address when editing contact info if user and user != self.user: raise fv.Invalid('That user already exists. Please choose another.', value, state) return value
def get(u=None, username=None): if not u and not username: abort(403) elif u: return u u = Session.query(users.User).filter_by(username=username).first() if not u: abort(403) return u
def _to_python(self, value, state): if type(value) != type(u""): raise fv.Invalid('You must supply a valid subdomain.', value, state) subd = Session.query(users.Organization).filter(sa.func.lower(users.Organization.subdomain)==value.lower()).first() if subd: raise fv.Invalid('%s is already taken' % value, value, state) return value.lower()
def get(real_user, user, organization, project=None): if not user and not project: abort(403) if project: q = Session.query(projects.Project).filter_by(organization=organization) p = q.filter(sa.or_(projects.Project.eid==project, projects.Project.slug==project)).first() CanReadProject().check(real_user, user, project=p) return p #this will do the check to make sure to return only projects the user can see return organization.get_projects(user)
def get(real_user=None, user=None, organization=None, subdomain=None): """ get a single project, or all the user's projects """ if organization: CanReadOrg().check(real_user, user, organization=organization) return organization elif subdomain: return Session.query(users.Organization).filter_by(subdomain=subdomain).first() return user.get_organizations()
def _to_python(self, value, state): # we don't support multiple values, so we run a quick check here (we got a webapp where this was a problem) if type(value) != type(u""): raise fv.Invalid('You must supply a valid email.', value, state) user = Session.query(users.User).filter( sa.func.lower(users.User.email) == value.lower()).first() # if this user is the same as the logged in one then don't throw the error - allows keeping old email address when editing contact info if user and user != self.user: raise fv.Invalid( 'That user already exists. Please choose another.', value, state) return value
def get(real_user=None, user=None, organization=None, subdomain=None): """ get a single project, or all the user's projects """ if organization: CanReadOrg().check(real_user, user, organization=organization) return organization elif subdomain: return Session.query( users.Organization).filter_by(subdomain=subdomain).first() return user.get_organizations()
def _to_python(self, value, state): if type(value) != type(u""): raise fv.Invalid('You must supply a valid subdomain.', value, state) subd = Session.query(users.Organization).filter( sa.func.lower(users.Organization.subdomain) == value.lower()).first() if subd: raise fv.Invalid('%s is already taken' % value, value, state) return value.lower()
def _to_python(self, value, state): # we don't support multiple values, so we run a quick check here (we got a webapp where this was a problem) if not isinstance(value, unicode): raise fv.Invalid('You must supply a valid string.', value, state) file = Session.query(projects.Entity ).filter(projects.Entity.project==self.project ).filter(projects.Entity.path==self.file.path ).filter(projects.Entity.name==value ) file = file.filter(projects.Entity.id != self.file.id) file = file.first() if file: raise fv.Invalid('A file with this name already exists. Please choose another.', value, state) return value
def _to_python(self, value, state): # we don't support multiple values, so we run a quick check here (we got a webapp where this was a problem) if not isinstance(value, unicode): raise fv.Invalid('You must supply a valid string.', value, state) project = Session.query(projects.Project ).filter(projects.Project.organization==self.organization ).filter(projects.Project.name==value ) if self.project: project = project.filter(projects.Project.id != self.project.id) project = project.first() if project: raise fv.Invalid('A project with this name already exists. Please choose another.', value, state) return value
def get(real_user, user, change=None, parse_type=None, parse_status=None): if change: return change if not parse_status and not parse_type: raise ClientException('either parse_type or parse_status') q = Session.query(projects.Change) if parse_type: q = q.filter(projects.Change.parse_type.in_(parse_type)) if parse_status: q = q.filter(projects.Change.parse_status.in_(parse_status)) q = q.order_by(sa.asc(projects.Change.created_date)) return q.all()
def _to_python(self, value, state): # we don't support multiple values, so we run a quick check here (we got a webapp where this was a problem) if not isinstance(value, unicode): raise fv.Invalid('You must supply a valid string.', value, state) file = Session.query(projects.Entity).filter( projects.Entity.project == self.project).filter( projects.Entity.path == self.file.path).filter( projects.Entity.name == value) file = file.filter(projects.Entity.id != self.file.id) file = file.first() if file: raise fv.Invalid( 'A file with this name already exists. Please choose another.', value, state) return value
indices[e.extract_type] += 1 change.parse_type = parse_info.type change.parse_status = parse_info.status commit() return extracts if __name__ == '__main__': """ accept 2 params: eid of the change path to file, the raw file (PSD, PDF, whatev...) """ if len(sys.argv) != 4: print 'Usage: %s development.ini change_eid /path/to/tmp_file.blah' % sys.argv[ 0] exit(1) ini, eid, filepath = sys.argv[1:] load_config(os.path.abspath(ini)) change = Session.query(projects.Change).filter_by(eid=eid).first() if not change: print 'Cannot find change %s' % eid exit(1) print gen_extracts(change, filepath)
Session.add(change_extract) extracts.append(change_extract) indices[e.extract_type] += 1 change.parse_type = parse_info.type change.parse_status = parse_info.status commit() return extracts if __name__ == '__main__': """ accept 2 params: eid of the change path to file, the raw file (PSD, PDF, whatev...) """ if len(sys.argv) != 4: print 'Usage: %s development.ini change_eid /path/to/tmp_file.blah' % sys.argv[0] exit(1) ini, eid, filepath = sys.argv[1:] load_config(os.path.abspath(ini)) change = Session.query(projects.Change).filter_by(eid=eid).first() if not change: print 'Cannot find change %s' % eid exit(1) print gen_extracts(change, filepath)