Switch to unified view

a/Allura/allura/lib/plugin.py b/Allura/allura/lib/plugin.py
...
...
9
from urllib2 import urlopen
9
from urllib2 import urlopen
10
from cStringIO import StringIO
10
from cStringIO import StringIO
11
from random import randint
11
from random import randint
12
from hashlib import sha256
12
from hashlib import sha256
13
from base64 import b64encode
13
from base64 import b64encode
14
from datetime import datetime
14
from datetime import datetime, timedelta
15
import json
15
16
16
try:
17
try:
17
    import ldap
18
    import ldap
18
    from ldap import modlist
19
    from ldap import modlist
19
except ImportError:
20
except ImportError:
20
    ldap = modlist = None
21
    ldap = modlist = None
21
import pkg_resources
22
import pkg_resources
22
from tg import config, flash
23
from tg import config, flash
23
from pylons import g, c
24
from pylons import g, c
24
from webob import exc
25
from webob import exc
26
from bson.tz_util import FixedOffset
25
27
26
from ming.utils import LazyProperty
28
from ming.utils import LazyProperty
27
from ming.orm import state
29
from ming.orm import state
28
from ming.orm import ThreadLocalORMSession
30
from ming.orm import ThreadLocalORMSession
29
31
...
...
332
        project with a name that matches the regex, the field will
334
        project with a name that matches the regex, the field will
333
        be marked invalid with the message displayed to the user.
335
        be marked invalid with the message displayed to the user.
334
        '''
336
        '''
335
        return []
337
        return []
336
338
339
    def rate_limit(self, user, neighborhood):
340
        '''Check the various config-defined project registration rate
341
        limits, and if any are exceeded, raise ProjectRatelimitError.
342
        '''
343
        if security.has_access(neighborhood, 'admin', user=user)():
344
            return
345
        now = datetime.now().replace(tzinfo=FixedOffset(0, 'UTC'))
346
        project_count = len(list(user.my_projects()))
347
        rate_limits = json.loads(config.get('project.rate_limits', '{}'))
348
        for rate, count in rate_limits.items():
349
            user_age = now - user._id.generation_time
350
            user_age = (user_age.microseconds + (user_age.seconds + user_age.days * 24 * 3600) * 10**6) / 10**6
351
            if user_age < int(rate) and project_count >= count:
352
                raise forge_exc.ProjectRatelimitError()
353
337
    def register_neighborhood_project(self, neighborhood, users, allow_register=False):
354
    def register_neighborhood_project(self, neighborhood, users, allow_register=False):
338
        from allura import model as M
355
        from allura import model as M
339
        shortname='--init--'
356
        shortname='--init--'
340
        p = neighborhood.neighborhood_project
357
        p = neighborhood.neighborhood_project
341
        if p: raise forge_exc.ProjectConflict()
358
        if p: raise forge_exc.ProjectConflict()
...
...
388
        nb_max_projects = neighborhood.get_max_projects()
405
        nb_max_projects = neighborhood.get_max_projects()
389
406
390
        if nb_max_projects is not None and count >= nb_max_projects:
407
        if nb_max_projects is not None and count >= nb_max_projects:
391
            log.exception('Error registering project %s' % project_name)
408
            log.exception('Error registering project %s' % project_name)
392
            raise forge_exc.ProjectOverlimitError()
409
            raise forge_exc.ProjectOverlimitError()
410
411
        self.rate_limit(user, neighborhood)
393
412
394
        if not h.re_path_portion.match(shortname.replace('/', '')):
413
        if not h.re_path_portion.match(shortname.replace('/', '')):
395
            raise ValueError('Invalid project shortname: %s' % shortname)
414
            raise ValueError('Invalid project shortname: %s' % shortname)
396
        try:
415
        try:
397
            p = M.Project.query.get(shortname=shortname, neighborhood_id=neighborhood._id)
416
            p = M.Project.query.get(shortname=shortname, neighborhood_id=neighborhood._id)