def add_user_to_group (request, user, language, user_added_phone, group_name): user_added = Users.resolve_or_create(request, user, language, user_added_phone) group = Groups.resolve (user, group_name) if group is None: return language.unknown_group (group_name) if not user_added.has_empty_slot (): return language.user_has_no_empty_slots () group.add_user (user, user_added) return language.added_user_to_group (user, group)
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 = Groups.resolve (curr_admin, group_name_or_slot) if group is None: return 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? 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 string.join(msg_list, " ") msg_list = [] valid_users = [] invalid_users = [] for admin_phone in admin_list: del_admin = Users.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 string.join(msg_list, " ")
def delete_group (request, admin, language, group_name_or_slot): logger.debug("group: %s" % (group_name_or_slot)) group = Groups.resolve (admin, group_name_or_slot) if group is None: return language.unknown_group (group_name_or_slot) if not admin.is_admin (group): return language.admin_privileges_required (group) Groups.delete (admin, group) return language.group_deleted (group)
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 = Groups.resolve (curr_admin, group_name_or_slot) if group is None: return 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 string.join(msg_list, " ") msg_list= [] invalid_users = [] non_members = [] valid_users = [] for admin_phone in admin_list: new_admin = Users.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 string.join(msg_list, " ")
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 = Groups.resolve (admin, group_name_or_slot) if group is None: return language.unknown_group (group_name_or_slot) if del_user.user_id == admin.user_id: 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 string.join(msg_list, " ") msg_list = [] valid_users = [] invalid_users = [] non_members = [] for user_phone in del_user_list: del_user = Users.resolve(user_phone) if del_user == None: 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 string.join(msg_list, " ")
def invite_user_to_group (request, user, language, group_name_or_slot, invite_user_phone, smsc = 'mosms'): from django.conf import settings #logger.debug('request: %s, user: %s, language: %s, group: %s, phone: %s, smsc %s' % (request, 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 = Groups.resolve (user, group_name_or_slot) if group is None: return 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 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 = Users.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 string.join(msg_list, " ")