<commit_before>from social_django.models import UserSocialAuth
from moderation.models import SocialUser, SocialMedia
from community.models import AccessLevel, Retweet
from constance import config
from typing import List
from moderation.models import Category
from bot.doctoctocbot import is_follower
import logging

logger = logging.getLogger(__name__)


def get_all_categories(user, community):
    su = get_twitter_social_user(user)
    # determine the categories the user belongs to
    return get_all_categories_from_social_user(su, community)

def get_all_categories_from_social_user(su, community):
    # determine the categories the user belongs to
    category: List[Category] = []
    # check twitter
    try:
        category.extend(twitter_categories_from_social_user(su, community))
    except TypeError:
        pass
    # check local social network
    try:
        category.extend(local_categories_from_social_user(su, community))
    except TypeError:
        pass
    # remove duplicates
    return list(set(category))

def member_level(user, community, category):
    if any(cat in category for cat in community.membership.all()):
        return access_level_object("member")

def broadcast_level(user, community, category):
    broadcast_categories = [rt.category for rt in Retweet.objects.filter(
        community=community,
        retweet=True
        )]
    if any(cat in category for cat in broadcast_categories):
        return access_level_object("broadcast")

def follower_level(user, community, category):
    follower_categories = community.follower.all()
    # local
    su_local = get_local_social_user(user)
    if su_local:
        if any(cat in category for cat in follower_categories):
            return access_level_object("follower")
    # twitter
    su_twitter = get_twitter_social_user(user)
    if su_twitter:
        protected = community.account.protected
        is_follower = is_follower(
            su_twitter.user_id,
            community.account.username
        )
        allowed = any(cat in category for cat in follower_categories)
        if protected:
            if allowed and is_follower:
                return access_level_object("follower")
            else:
                return
        elif is_follower:
            return access_level_object("follower")

def get_api_level(user, community):
    if not user.is_authenticated:
        return access_level_object("anonymous")
    category: List[Category] = get_all_categories(user, community)
    member = member_level(user, community, category)
    if member:
        return member
    broadcast = broadcast_level(user, community, category)
    if broadcast:
        return broadcast
    follower = follower_level(user, community, category)
    if follower:
        return follower
    return access_level_object("registered")

def access_level_object(level):
    try:
        return AccessLevel.objects.get(name=level)
    except AccessLevel.DoesNotExist:
        logger.error(f"{level} access level was not created")
        return None

def twitter_categories(user, community):<commit_msg>fix same name for variable and function<commit_after>from social_django.models import UserSocialAuth
from moderation.models import SocialUser, SocialMedia
from community.models import AccessLevel, Retweet
from constance import config
from typing import List
from moderation.models import Category
from bot.doctoctocbot import is_follower
import logging

logger = logging.getLogger(__name__)


def get_all_categories(user, community):
    su = get_twitter_social_user(user)
    # determine the categories the user belongs to
    return get_all_categories_from_social_user(su, community)

def get_all_categories_from_social_user(su, community):
    # determine the categories the user belongs to
    category: List[Category] = []
    # check twitter
    try:
        category.extend(twitter_categories_from_social_user(su, community))
    except TypeError:
        pass
    # check local social network
    try:
        category.extend(local_categories_from_social_user(su, community))
    except TypeError:
        pass
    # remove duplicates
    return list(set(category))

def member_level(user, community, category):
    if any(cat in category for cat in community.membership.all()):
        return access_level_object("member")

def broadcast_level(user, community, category):
    broadcast_categories = [rt.category for rt in Retweet.objects.filter(
        community=community,
        retweet=True
        )]
    if any(cat in category for cat in broadcast_categories):
        return access_level_object("broadcast")

def follower_level(user, community, category):
    follower_categories = community.follower.all()
    # local
    su_local = get_local_social_user(user)
    if su_local:
        if any(cat in category for cat in follower_categories):
            return access_level_object("follower")
    # twitter
    su_twitter = get_twitter_social_user(user)
    if su_twitter:
        protected = community.account.protected
        _is_follower = is_follower(
            su_twitter.user_id,
            community.account.username
        )
        allowed = any(cat in category for cat in follower_categories)
        if protected:
            if allowed and _is_follower:
                return access_level_object("follower")
            else:
                return
        elif _is_follower:
            return access_level_object("follower")

def get_api_level(user, community):
    if not user.is_authenticated:
        return access_level_object("anonymous")
    category: List[Category] = get_all_categories(user, community)
    member = member_level(user, community, category)
    if member:
        return member
    broadcast = broadcast_level(user, community, category)
    if broadcast:
        return broadcast
    follower = follower_level(user, community, category)
    if follower:
        return follower
    return access_level_object("registered")

def access_level_object(level):
    try:
        return AccessLevel.objects.get(name=level)
    except AccessLevel.DoesNotExist:
        logger.error(f"{level} access level was not created")
        return None

def twitter_categories(user, community):
{'fraction_non_alphanumeric': 0.06757405924739791, 'fraction_numerical': 0.0, 'mean_word_length': 3.370888733379986, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5480122', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1649, 'n_words': 463}