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 gen_extracts(change, tmp_filepath): """ Will generate the file extracts for this change. The extracts include the thumbnail. :param change: is the change object. :param tmp_filepath: is the path to the original file """ # find previous change previous = change.entity.get_change(version=change.version - 1) extracts = [] indices = dd(lambda: 0) #each type of file will have its own count raw_extracts, parse_info = image.extract(tmp_filepath) change.file_type = parse_info.file_type raw_extracts += gen_diffs(previous, raw_extracts) for e in raw_extracts: change_extract = projects.ChangeExtract( change=change, extract_type=e.extract_type, order_index=indices[e.extract_type]) change_extract.set_contents(e.filename) 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
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 gen_extracts(change, tmp_filepath): """ Will generate the file extracts for this change. The extracts include the thumbnail. :param change: is the change object. :param tmp_filepath: is the path to the original file """ # find previous change previous = change.entity.get_change(version=change.version-1) extracts = [] indices = dd(lambda: 0) #each type of file will have its own count raw_extracts, parse_info = image.extract(tmp_filepath) change.file_type = parse_info.file_type raw_extracts += gen_diffs(previous, raw_extracts) for e in raw_extracts: change_extract = projects.ChangeExtract(change=change, extract_type=e.extract_type, order_index=indices[e.extract_type]) change_extract.set_contents(e.filename) 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
def create(real_user, user, organization, **params): """ Creates a project. """ class ProjectForm(formencode.Schema): name = formencode.All(fv.UnicodeString(not_empty=True), UniqueName(organization)) description = fv.UnicodeString(not_empty=False) scrubbed = validate(ProjectForm, **params) project = projects.Project(name=scrubbed.name, creator=user, description=scrubbed.description, organization=organization) Session.add(project) Session.flush() #email users = organization.interested_users if user in users: users.remove(user) email.send(users, 'create_project.txt', { 'project': project, 'creator': user }) return project
def create_project(user=None, organization=None, role=users.APP_ROLE_ADMIN, **kw): kw.setdefault("name", create_unique_str(u'project')) kw.setdefault("description", create_unique_str(u"description")) kw.setdefault("status", STATUS_APPROVED) org = organization or create_organization(user, role) project = projects.Project(organization=org, creator=user or org.creator, **kw) Session.add(project) Session.flush() return project
def create_user(is_admin=False, **kw): kw.setdefault("email", create_email_address()) kw.setdefault("username", create_unique_str(u'user', extra=u'')) kw.setdefault("password", u'testpassword') if is_admin: kw.setdefault("role", users.ROLE_ADMIN) else: kw.setdefault("role", users.ROLE_USER) user = users.User(**kw) Session.add(user) Session.flush() return user
def create_organization(user=None, role=users.APP_ROLE_ADMIN, status=STATUS_APPROVED, **kw): """ create an org and will attach a user to it. If no user specified, will make one. """ kw.setdefault("name", create_unique_str(u'org')) kw.setdefault("url", u'http://%s.com' % create_unique_str(u'url')) kw.setdefault("subdomain", create_str(length=10)) user = user or create_user() org = users.Organization(creator=user, **kw) Session.add(org) #connect user to org as admin of org org_user = org.attach_user(user, role=role, status=status) Session.flush() return org
def create(real_user, user, **params): """ Creates an organization. Attaches it to User. """ scrubbed = validate(CreateForm, **params) scrubbed.setdefault('is_active', True) scrubbed['name'] = scrubbed['company_name'] del scrubbed['company_name'] #attach the user as a creator. org = users.Organization(creator=user, **scrubbed) Session.add(org) #connect user to org as admin of org org.attach_user(user, role=users.APP_ROLE_ADMIN, status=STATUS_APPROVED) Session.add(activity.NewOrganization(user, org)) Session.flush() return org