Example #1
0
def request_create_group(request, user, language, group_name, slot):
    from django.template.defaultfilters import slugify

    logger.debug("user %s group_name %s slot %s" % (user, group_name, slot))

    slot = urllib.unquote_plus(slot)

    group_type = "private"

    # only allow group leaders to create groups
    # group leader is the group admin of the main/default organization's group
    default_org = UserGroups.objects.filter(user=user).order_by("pk")[0].group.org
    groups = Vikundi.objects.filter(group_name=slugify(default_org))
    if len(groups) > 0:
        if not user.is_admin(groups[0]):
            return [False, language.action_not_allowed()]

    if not user.has_empty_slot():
        return [False, language.user_has_no_empty_slots()]

    type_and_slot_regex = re.compile("^\w+\s+\d$")
    slot_only_regex = re.compile("^\d$")

    if type_and_slot_regex.match(slot):
        (group_type, slot) = slot.split()
        if group_type != "private":
            group_type = "public"
    elif len(slot) > 1 and not slot_only_regex.match(slot):  # only provided type without slot
        group_type = slot
        if group_type != "private":
            group_type = "public"  # check to ensure its actually a group type
        slot = ""
    else:
        # group_type = 'po'
        slot = ""

    msg_list = []

    if slot is None or len(slot) < 1:
        logger.debug("slot is none")
        slot = auto_alloc_slot(user)
        logger.info("slot allocated %s" % slot)
    else:
        logger.debug("slot is %s" % slot)

    # regex = re.compile ('^[A-Za-z]')
    # this regex match doesnt work with unicode character set
    # if not regex.match (group_name) or len(group_name) < 4 or len(group_name) > 60:
    if not group_name[0].isalpha() or len(group_name) < 4 or len(group_name) > 60:
        msg_list.append(language.group_name_not_valid(group_name))

    if slot >= 0 and not user.slot_is_empty(slot):
        msg_list.append(language.slot_not_free(slot))

    # XXX calling this causes an error (and we've fixed the types as unchangeable for now)
    # if not Groups.is_valid_type (group_type):
    # ok = False
    # msg_list.append (language.invalid_group_type (group_type))

    # that should take care of the race condition
    with lock:

        if not Vikundi.is_name_available(group_name):
            msg_list.append(language.group_name_not_available(group_name))
            # TODO add suggestions

        if len(msg_list) > 0:
            msg_text = " ".join(msg_list)
            logger.debug("TWIG: %s" % msg_text)
            return [False, msg_text]

        # Any time you create a user the first group they join is the
        # organization's group
        default_org = UserGroups.objects.filter(user=user).order_by("pk")[0].group.org
        logger.debug("default: %s %s %s %s %s" % (user, group_name, slot, group_type, default_org))
        # create group and assign it to the given slot, if one has been provided
        group = Vikundi.create(user, group_name, slot, group_type, org=default_org)

    return [True, language.group_created(group_name, slot, group_type)]
Example #2
0
def request_create_group(request, user, language, group_name, slot):
    from django.template.defaultfilters import slugify

    logger.debug("user %s group_name %s slot %s" % (user, group_name, slot))

    slot = urllib.unquote_plus(slot)

    group_type = 'private'

    #only allow group leaders to create groups
    #group leader is the group admin of the main/default organization's group
    default_org = UserGroups.objects.filter(
        user=user).order_by('pk')[0].group.org
    groups = Vikundi.objects.filter(group_name=slugify(default_org))
    if len(groups) > 0:
        if not user.is_admin(groups[0]):
            return [False, language.action_not_allowed()]

    if not user.has_empty_slot():
        return [False, language.user_has_no_empty_slots()]

    type_and_slot_regex = re.compile("^\w+\s+\d$")
    slot_only_regex = re.compile("^\d$")

    if type_and_slot_regex.match(slot):
        (group_type, slot) = slot.split()
        if group_type != 'private':
            group_type = 'public'
    elif len(slot) > 1 and not slot_only_regex.match(
            slot):  #only provided type without slot
        group_type = slot
        if group_type != 'private':
            group_type = 'public'  #check to ensure its actually a group type
        slot = ''
    else:
        #group_type = 'po'
        slot = ''

    msg_list = []

    if slot is None or len(slot) < 1:
        logger.debug("slot is none")
        slot = auto_alloc_slot(user)
        logger.info("slot allocated %s" % slot)
    else:
        logger.debug("slot is %s" % slot)

    #regex = re.compile ('^[A-Za-z]')
    #this regex match doesnt work with unicode character set
    #if not regex.match (group_name) or len(group_name) < 4 or len(group_name) > 60:
    if not group_name[0].isalpha() or len(group_name) < 4 or len(
            group_name) > 60:
        msg_list.append(language.group_name_not_valid(group_name))

    if slot >= 0 and not user.slot_is_empty(slot):
        msg_list.append(language.slot_not_free(slot))

    # XXX calling this causes an error (and we've fixed the types as unchangeable for now)
    #if not Groups.is_valid_type (group_type):
    #ok = False
    #msg_list.append (language.invalid_group_type (group_type))

    # that should take care of the race condition
    with lock:

        if not Vikundi.is_name_available(group_name):
            msg_list.append(language.group_name_not_available(group_name))
            # TODO add suggestions

        if len(msg_list) > 0:
            msg_text = " ".join(msg_list)
            logger.debug("TWIG: %s" % msg_text)
            return [False, msg_text]

        #Any time you create a user the first group they join is the
        #organization's group
        default_org = UserGroups.objects.filter(
            user=user).order_by('pk')[0].group.org
        logger.debug('default: %s %s %s %s %s' %
                     (user, group_name, slot, group_type, default_org))
        # create group and assign it to the given slot, if one has been provided
        group = Vikundi.create(user,
                               group_name,
                               slot,
                               group_type,
                               org=default_org)

    return [True, language.group_created(group_name, slot, group_type)]