Switch to unified view

a/Allura/allura/lib/helpers.py b/Allura/allura/lib/helpers.py
...
...
164
    parts = [
164
    parts = [
165
        name[:i+1]
165
        name[:i+1]
166
        for i in range(num_parts) ]
166
        for i in range(num_parts) ]
167
    return '/'.join(parts)
167
    return '/'.join(parts)
168
168
169
def set_context(project_shortname, mount_point=None, app_config_id=None, neighborhood=None):
169
def set_context(project_shortname_or_id, mount_point=None, app_config_id=None, neighborhood=None):
170
    from allura import model
170
    from allura import model
171
    try:
172
        p = model.Project.query.get(_id=ObjectId(str(project_shortname_or_id)))
173
    except InvalidId:
174
        p = None
175
    if p is None:
176
        if neighborhood is None:
177
            raise TypeError('neighborhood is required; it must not be None')
171
    if not isinstance(neighborhood, model.Neighborhood):
178
        if not isinstance(neighborhood, model.Neighborhood):
172
        if neighborhood is not None:
173
            n = model.Neighborhood.query.get(name=neighborhood)
179
            n = model.Neighborhood.query.get(name=neighborhood)
174
            if n is None:
180
            if n is None:
175
                try:
181
                try:
176
                    n = model.Neighborhood.query.get(_id=ObjectId(str(neighborhood)))
182
                    n = model.Neighborhood.query.get(_id=ObjectId(str(neighborhood)))
177
                except InvalidId:
183
                except InvalidId:
...
...
179
            if n is None:
185
            if n is None:
180
                raise exc.NoSuchNeighborhoodError("Couldn't find neighborhood %s" %
186
                raise exc.NoSuchNeighborhoodError("Couldn't find neighborhood %s" %
181
                                      repr(neighborhood))
187
                                      repr(neighborhood))
182
            neighborhood = n
188
            neighborhood = n
183
189
184
    query = dict(shortname=project_shortname)
190
        query = dict(shortname=project_shortname_or_id, neighborhood_id=neighborhood._id)
185
    if neighborhood is not None:
186
        query['neighborhood_id'] = neighborhood._id
187
    p = model.Project.query.get(**query)
191
        p = model.Project.query.get(**query)
188
    if p is None:
192
    if p is None:
189
        try:
190
            del query['shortname']
191
            query['_id'] = ObjectId(str(project_shortname))
192
            p = model.Project.query.get(**query)
193
        except InvalidId:
194
            pass
195
196
    if p is None:
197
        raise exc.NoSuchProjectError("Couldn't find project %s" %
193
        raise exc.NoSuchProjectError("Couldn't find project %s nbhd %s" %
198
                                 repr(project_shortname))
194
                                 (project_shortname_or_id, neighborhood))
199
    c.project = p
195
    c.project = p
200
196
201
    if app_config_id is None:
197
    if app_config_id is None:
202
        c.app = p.app_instance(mount_point)
198
        c.app = p.app_instance(mount_point)
203
    else:
199
    else:
...
...
205
            app_config_id = ObjectId(app_config_id)
201
            app_config_id = ObjectId(app_config_id)
206
        app_config = model.AppConfig.query.get(_id=app_config_id)
202
        app_config = model.AppConfig.query.get(_id=app_config_id)
207
        c.app = p.app_instance(app_config)
203
        c.app = p.app_instance(app_config)
208
204
209
@contextmanager
205
@contextmanager
210
def push_context(project_id, mount_point=None, app_config_id=None):
206
def push_context(project_id, mount_point=None, app_config_id=None, neighborhood=None):
211
    project = getattr(c, 'project', ())
207
    project = getattr(c, 'project', ())
212
    app = getattr(c, 'app', ())
208
    app = getattr(c, 'app', ())
213
    set_context(project_id, mount_point, app_config_id)
209
    set_context(project_id, mount_point, app_config_id, neighborhood)
214
    try:
210
    try:
215
        yield
211
        yield
216
    finally:
212
    finally:
217
        if project == ():
213
        if project == ():
218
            del c.project
214
            del c.project
...
...
397
def full_url(url):
393
def full_url(url):
398
    """Make absolute URL from the relative one.
394
    """Make absolute URL from the relative one.
399
    """
395
    """
400
    global site_url
396
    global site_url
401
    if site_url is None:
397
    if site_url is None:
402
        # XXX: add a separate tg option instead of re-using openid.realm 
398
        # XXX: add a separate tg option instead of re-using openid.realm
403
        site_url = tg.config.get('openid.realm', 'https://newforge.sf.geek.net/')
399
        site_url = tg.config.get('openid.realm', 'https://newforge.sf.geek.net/')
404
        site_url = site_url.replace('https:', 'http:')
400
        site_url = site_url.replace('https:', 'http:')
405
        if not site_url.endswith('/'):
401
        if not site_url.endswith('/'):
406
            site_url += '/'
402
            site_url += '/'
407
    if url.startswith('/'):
403
    if url.startswith('/'):
...
...
423
    mbox = M.Mailbox.query.get(user_id=user._id, is_flash=True)
419
    mbox = M.Mailbox.query.get(user_id=user._id, is_flash=True)
424
    if mbox:
420
    if mbox:
425
        notifications = M.Notification.query.find(dict(_id={'$in':mbox.queue}))
421
        notifications = M.Notification.query.find(dict(_id={'$in':mbox.queue}))
426
        mbox.queue = []
422
        mbox.queue = []
427
        for n in notifications: yield n
423
        for n in notifications: yield n
428
    
424
429
def config_with_prefix(d, prefix):
425
def config_with_prefix(d, prefix):
430
    '''Return a subdictionary keys with a given prefix,
426
    '''Return a subdictionary keys with a given prefix,
431
    with the prefix stripped
427
    with the prefix stripped
432
    '''
428
    '''
433
    plen=len(prefix)
429
    plen=len(prefix)
...
...
517
            else:
513
            else:
518
                result['username'] = '*system'
514
                result['username'] = '*system'
519
            try:
515
            try:
520
                result['url'] = request.url
516
                result['url'] = request.url
521
                ip_address = request.headers.get('X_FORWARDED_FOR', request.remote_addr)
517
                ip_address = request.headers.get('X_FORWARDED_FOR', request.remote_addr)
522
                if ip_address is not None: 
518
                if ip_address is not None:
523
                    ip_address = ip_address.split(',')[0].strip()
519
                    ip_address = ip_address.split(',')[0].strip()
524
                    result['ip_address'] = ip_address
520
                    result['ip_address'] = ip_address
525
                else:
521
                else:
526
                    result['ip_address'] = '0.0.0.0'
522
                    result['ip_address'] = '0.0.0.0'
527
            except TypeError:
523
            except TypeError: