Example #1
0
def delete_admin_from_group(request, curr_admin, language, group_name_or_slot,
                            del_admin_phone):
    admin_list = del_admin_phone.replace("+", " ").split()
    logger.debug("group: %s users: %s" % (group_name_or_slot, admin_list))

    msg_list = []

    group = Vikundi.resolve(curr_admin, group_name_or_slot)

    if group is None:
        return [False, language.unknown_group(group_name_or_slot)]

    if not curr_admin.is_admin(group):
        msg_list.append(language.admin_privileges_required(group))

    # is he removing himself?
    for admin_phone in admin_list:
        del_admin = Watumiaji.resolve(admin_phone)

        if curr_admin.user_id == del_admin.user_id:
            admin_count = group.get_admin_count()
            if admin_count == 1:
                msg_list.append(
                    language.cannot_delete_only_admin(curr_admin, group))

    # have to stay admin of your own group
    if curr_admin.is_mine(group):
        msg_list.append(
            language.cannot_delete_self_from_my_group(curr_admin, group))

    if len(msg_list) > 0:
        return [False, string.join(msg_list, " ")]

    msg_list = []
    valid_users = []
    invalid_users = []

    for admin_phone in admin_list:
        del_admin = Watumiaji.resolve(del_admin_phone)

        if del_admin == None:
            invalid_users.append(admin_phone)
        else:
            if del_admin.is_admin(group):
                group.delete_admin(curr_admin, del_admin)
            valid_users.append(admin_phone)

    if len(valid_users) > 0:
        valid_users = string.join(valid_users, ",")
        msg_list.append(language.deleted_admin(valid_users, group))

    if len(invalid_users) > 0:
        invalid_users = string.join(invalid_users, ",")
        msg_list.append(language.unknown_user(invalid_users))

    return [True, string.join(msg_list, " ")]
Example #2
0
def delete_admin_from_group(request, curr_admin, language, group_name_or_slot, del_admin_phone):
    admin_list = del_admin_phone.replace("+", " ").split()
    logger.debug("group: %s users: %s" % (group_name_or_slot, admin_list))

    msg_list = []

    group = Vikundi.resolve(curr_admin, group_name_or_slot)

    if group is None:
        return [False, language.unknown_group(group_name_or_slot)]

    if not curr_admin.is_admin(group):
        msg_list.append(language.admin_privileges_required(group))

    # is he removing himself?
    for admin_phone in admin_list:
        del_admin = Watumiaji.resolve(admin_phone)

        if curr_admin.user_id == del_admin.user_id:
            admin_count = group.get_admin_count()
            if admin_count == 1:
                msg_list.append(language.cannot_delete_only_admin(curr_admin, group))

    # have to stay admin of your own group
    if curr_admin.is_mine(group):
        msg_list.append(language.cannot_delete_self_from_my_group(curr_admin, group))

    if len(msg_list) > 0:
        return [False, string.join(msg_list, " ")]

    msg_list = []
    valid_users = []
    invalid_users = []

    for admin_phone in admin_list:
        del_admin = Watumiaji.resolve(del_admin_phone)

        if del_admin == None:
            invalid_users.append(admin_phone)
        else:
            if del_admin.is_admin(group):
                group.delete_admin(curr_admin, del_admin)
            valid_users.append(admin_phone)

    if len(valid_users) > 0:
        valid_users = string.join(valid_users, ",")
        msg_list.append(language.deleted_admin(valid_users, group))

    if len(invalid_users) > 0:
        invalid_users = string.join(invalid_users, ",")
        msg_list.append(language.unknown_user(invalid_users))

    return [True, string.join(msg_list, " ")]
Example #3
0
def add_user_to_group(request, user, language, user_added_phone, group_name):
    user_added = Watumiaji.resolve_or_create(request, user, language, user_added_phone)
    group = Vikundi.resolve(user, group_name)

    if group is None:
        return [False, language.unknown_group(group_name)]

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

    group.add_user(user, user_added)

    return [True, language.added_user_to_group(user, group)]
Example #4
0
def add_user_to_group(request, user, language, user_added_phone, group_name):
    user_added = Watumiaji.resolve_or_create(request, user, language,
                                             user_added_phone)
    group = Vikundi.resolve(user, group_name)

    if group is None:
        return [False, language.unknown_group(group_name)]

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

    group.add_user(user, user_added)

    return [True, language.added_user_to_group(user, group)]
Example #5
0
def add_admin_to_group(request, curr_admin, language, group_name_or_slot,
                       new_admin_phone):

    admin_list = new_admin_phone.replace("+", " ").split()
    logger.debug("group: %s admins: %s" % (group_name_or_slot, admin_list))

    msg_list = []

    group = Vikundi.resolve(curr_admin, group_name_or_slot)

    if group is None:
        return [False, language.unknown_group(group_name_or_slot)]

    if not curr_admin.is_admin(group):
        msg_list.append(language.admin_privileges_required(group))

    if len(msg_list) > 0:
        return [False, string.join(msg_list, " ")]

    msg_list = []
    invalid_users = []
    non_members = []
    valid_users = []

    for admin_phone in admin_list:
        new_admin = Watumiaji.resolve(admin_phone)

        if new_admin == None:
            invalid_users.append(admin_phone)
        else:
            if not new_admin.is_member(group):
                non_members.append(admin_phone)
            else:
                if not new_admin.is_admin(group):
                    group.add_admin(curr_admin, new_admin)
                    valid_users.append(admin_phone)

    if len(valid_users) > 0:
        valid_users = string.join(valid_users, ",")
        msg_list.append(language.added_admin(valid_users, group))

    if len(non_members) > 0:
        non_members = string.join(non_members, ",")
        msg_list.append(language.admin_must_be_member(non_members))

    if len(invalid_users) > 0:
        invalid_users = string.join(invalid_users, ",")
        msg_list.append(language.unknown_user(invalid_users))

    return [False, string.join(msg_list, " ")]
Example #6
0
def add_admin_to_group(request, curr_admin, language, group_name_or_slot, new_admin_phone):

    admin_list = new_admin_phone.replace("+", " ").split()
    logger.debug("group: %s admins: %s" % (group_name_or_slot, admin_list))

    msg_list = []

    group = Vikundi.resolve(curr_admin, group_name_or_slot)

    if group is None:
        return [False, language.unknown_group(group_name_or_slot)]

    if not curr_admin.is_admin(group):
        msg_list.append(language.admin_privileges_required(group))

    if len(msg_list) > 0:
        return [False, string.join(msg_list, " ")]

    msg_list = []
    invalid_users = []
    non_members = []
    valid_users = []

    for admin_phone in admin_list:
        new_admin = Watumiaji.resolve(admin_phone)

        if new_admin == None:
            invalid_users.append(admin_phone)
        else:
            if not new_admin.is_member(group):
                non_members.append(admin_phone)
            else:
                if not new_admin.is_admin(group):
                    group.add_admin(curr_admin, new_admin)
                    valid_users.append(admin_phone)

    if len(valid_users) > 0:
        valid_users = string.join(valid_users, ",")
        msg_list.append(language.added_admin(valid_users, group))

    if len(non_members) > 0:
        non_members = string.join(non_members, ",")
        msg_list.append(language.admin_must_be_member(non_members))

    if len(invalid_users) > 0:
        invalid_users = string.join(invalid_users, ",")
        msg_list.append(language.unknown_user(invalid_users))

    return [False, string.join(msg_list, " ")]
Example #7
0
def delete_user_from_group(request, admin, language, group_name_or_slot, del_user_phone):
    del_user_list = del_user_phone.replace("+", " ").split()
    logger.debug("group: %s users: %s" % (group_name_or_slot, del_user_list))

    msg_list = []

    group = Vikundi.resolve(admin, group_name_or_slot)

    if group is None:
        return [False, language.unknown_group(group_name_or_slot)]

    if len(del_user_list) < 1:
        return [False, language.no_user_specified()]

    del_user = Watumiaji.objects.filter(name_text=del_user_list[0])
    if not del_user:
        return [False, language.unknown_user(del_user_list[0])]

    if del_user[0].pk == admin.pk:
        msg_list.append(language.cannot_leave_own_group())

    if not admin.is_admin(group):
        msg_list.append(language.admin_privileges_required(group))

    if len(msg_list) > 0:
        return [False, string.join(msg_list, " ")]

    msg_list = []
    valid_users = []
    invalid_users = []
    non_members = []

    for user_phone in del_user_list:
        if user_phone.isdigit():
            del_user = Watumiaji.resolve(user_phone)
        else:
            # in case the admin used the members nickname
            del_user = Watumiaji.objects.filter(name_text=user_phone)
            if len(del_user) > 0:
                del_user = del_user[0]

        if not del_user:
            invalid_users.append(user_phone)
        else:
            if del_user.is_member(group):
                # delete as admin as well
                if del_user.is_admin(group):
                    group.delete_admin(admin, del_user_phone)

                group.delete_user(admin, del_user)
                valid_users.append(user_phone)

    if len(valid_users) > 0:
        valid_users = string.join(valid_users, ",")
        msg_list.append(language.deleted_user_from_group(valid_users, group))

    if len(invalid_users) > 0:
        invalid_users = string.join(invalid_users, ",")
        msg_list.append(language.unknown_user(invalid_users))

    return [True, string.join(msg_list, " ")]
Example #8
0
def invite_user_to_group(request, user, language, group_name_or_slot, invite_user_phone, smsc="mosms"):
    from django.conf import settings

    # logger.debug('user: %s, language: %s, group: %s, phone: %s, smsc %s' % (user, language, group_name_or_slot, invite_user_phone, smsc))

    invited_user_list = invite_user_phone.replace("+", " ").replace(",", " ").split()
    name = ""

    if not invited_user_list[0].isdigit():
        name = invited_user_list[0]
        # if user hasnt registered a name add that as their name
        if not user.name_text:
            user.set_name(name)
        invited_user_list.remove(name)

    logger.debug("group: %s users: %s" % (group_name_or_slot, invited_user_list))

    msg_list = []

    group = Vikundi.resolve(user, group_name_or_slot)

    if group is None:
        return [False, language.unknown_group(group_name_or_slot)]

    if not user.is_admin(group):
        if not group.is_public():
            msg_list.append(language.cannot_invite_user())

    if len(msg_list) > 0:
        return [False, string.join(msg_list, " ")]

    msg_list = []
    valid_users = []
    invalid_users = []

    for user_phone in invited_user_list:
        # make sure its a number
        phone_regex = re.compile("^\d+$")
        if not phone_regex.match(user_phone):
            continue

        invited_user = Watumiaji.resolve_or_create(request, user, language, user_phone)

        if invited_user == None:
            invalid_users.append(user_phone)
        else:
            if not invited_user.is_member(group):
                user.invite_user(invited_user, group)
                if invited_user.is_mine(group):
                    group.group_name = "mine"
            valid_users.append(user_phone)

            origin = settings.SMS_VOICE[smsc]

            global_send_sms(
                "+" + invited_user.phone_number,
                name
                + " <"
                + re.sub("^2547", "07", user.phone_number)
                + "> invited you to the "
                + group.group_name
                + " group. Reply: join "
                + group.group_name
                + ", to get their messages. "
                + "Create ur own groups and tangaza by flashing %s. Enjoy!" % settings.SMS_VOICE["VOICE_%s" % origin],
                origin,
            )

    if len(valid_users) > 0:
        valid_users = string.join(valid_users, ",")
        msg_list.append(language.invited_user(valid_users, group))

    if len(invalid_users) > 0:
        invalid_users = string.join(invalid_users, ",")
        msg_list.append(language.unknown_user(invalid_users))

    return [True, string.join(msg_list, " ")]
Example #9
0
def delete_user_from_group(request, admin, language, group_name_or_slot,
                           del_user_phone):
    del_user_list = del_user_phone.replace("+", " ").split()
    logger.debug("group: %s users: %s" % (group_name_or_slot, del_user_list))

    msg_list = []

    group = Vikundi.resolve(admin, group_name_or_slot)

    if group is None:
        return [False, language.unknown_group(group_name_or_slot)]

    if len(del_user_list) < 1:
        return [False, language.no_user_specified()]

    del_user = Watumiaji.objects.filter(name_text=del_user_list[0])
    if not del_user:
        return [False, language.unknown_user(del_user_list[0])]

    if del_user[0].pk == admin.pk:
        msg_list.append(language.cannot_leave_own_group())

    if not admin.is_admin(group):
        msg_list.append(language.admin_privileges_required(group))

    if len(msg_list) > 0:
        return [False, string.join(msg_list, " ")]

    msg_list = []
    valid_users = []
    invalid_users = []
    non_members = []

    for user_phone in del_user_list:
        if user_phone.isdigit():
            del_user = Watumiaji.resolve(user_phone)
        else:
            #in case the admin used the members nickname
            del_user = Watumiaji.objects.filter(name_text=user_phone)
            if len(del_user) > 0:
                del_user = del_user[0]

        if not del_user:
            invalid_users.append(user_phone)
        else:
            if del_user.is_member(group):
                #delete as admin as well
                if del_user.is_admin(group):
                    group.delete_admin(admin, del_user_phone)

                group.delete_user(admin, del_user)
                valid_users.append(user_phone)

    if len(valid_users) > 0:
        valid_users = string.join(valid_users, ",")
        msg_list.append(language.deleted_user_from_group(valid_users, group))

    if len(invalid_users) > 0:
        invalid_users = string.join(invalid_users, ",")
        msg_list.append(language.unknown_user(invalid_users))

    return [True, string.join(msg_list, " ")]
Example #10
0
def invite_user_to_group(request,
                         user,
                         language,
                         group_name_or_slot,
                         invite_user_phone,
                         smsc='mosms'):
    from django.conf import settings

    #logger.debug('user: %s, language: %s, group: %s, phone: %s, smsc %s' % (user, language, group_name_or_slot, invite_user_phone, smsc))

    invited_user_list = invite_user_phone.replace("+",
                                                  " ").replace(",",
                                                               " ").split()
    name = ''

    if not invited_user_list[0].isdigit():
        name = invited_user_list[0]
        #if user hasnt registered a name add that as their name
        if not user.name_text:
            user.set_name(name)
        invited_user_list.remove(name)

    logger.debug('group: %s users: %s' %
                 (group_name_or_slot, invited_user_list))

    msg_list = []

    group = Vikundi.resolve(user, group_name_or_slot)

    if group is None:
        return [False, language.unknown_group(group_name_or_slot)]

    if not user.is_admin(group):
        if not group.is_public():
            msg_list.append(language.cannot_invite_user())

    if len(msg_list) > 0:
        return [False, string.join(msg_list, " ")]

    msg_list = []
    valid_users = []
    invalid_users = []

    for user_phone in invited_user_list:
        #make sure its a number
        phone_regex = re.compile("^\d+$")
        if not phone_regex.match(user_phone):
            continue

        invited_user = Watumiaji.resolve_or_create(request, user, language,
                                                   user_phone)

        if invited_user == None:
            invalid_users.append(user_phone)
        else:
            if not invited_user.is_member(group):
                user.invite_user(invited_user, group)
                if invited_user.is_mine(group): group.group_name = 'mine'
            valid_users.append(user_phone)

            origin = settings.SMS_VOICE[smsc]

            global_send_sms(
                "+" + invited_user.phone_number,
                name + " <" + re.sub('^2547', '07', user.phone_number) +
                "> invited you to the " + group.group_name +
                " group. Reply: join " + group.group_name +
                ", to get their messages. " +
                "Create ur own groups and tangaza by flashing %s. Enjoy!" %
                settings.SMS_VOICE['VOICE_%s' % origin], origin)

    if len(valid_users) > 0:
        valid_users = string.join(valid_users, ",")
        msg_list.append(language.invited_user(valid_users, group))

    if len(invalid_users) > 0:
        invalid_users = string.join(invalid_users, ",")
        msg_list.append(language.unknown_user(invalid_users))

    return [True, string.join(msg_list, " ")]