'Alters membership to Groups and Skills.'

def set_membership(self, model, membership_list):

    if (model is Group):
        m2mfield = self.groups
        alias_model = GroupAlias
    elif (model is Skill):
        m2mfield = self.skills
        alias_model = SkillAlias
    if (model is Group):
        GroupMembership.objects.filter(userprofile=self, group__visible=True).exclude(group__name__in=membership_list).delete()
    else:
        m2mfield.remove(*[g for g in m2mfield.all() if ((g.name not in membership_list) and g.is_visible)])
    groups_to_add = []
    for g in membership_list:
        if alias_model.objects.filter(name=g).exists():
            group = alias_model.objects.get(name=g).alias
        else:
            group = model.objects.create(name=g)
        if group.is_visible:
            groups_to_add.append(group)
    if (model is Group):
        for group in groups_to_add:
            group.add_member(self)
    else:
        m2mfield.add(*groups_to_add)
