<commit_before>#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#

from __future__ import absolute_import, unicode_literals

from datetime import date
import random

from oclubs.access import database, redis
from oclubs.enums import ClubType, UserType, ClubJoinMode
from oclubs.objs.base import BaseObject, Property, ListProperty, paged_db_read


class Club(BaseObject):
    table = 'club'
    identifier = 'club_id'
    name = Property('club_name', search=True)
    teacher = Property('club_teacher', 'User')
    leader = Property('club_leader', 'User')
    description = Property('club_desc', 'FormattedText', search=True)
    location = Property('club_location')
    is_active = Property('club_inactive', lambda v: not v)
    intro = Property('club_intro', search=True)
    picture = Property('club_picture', 'Upload')
    type = Property('club_type', ClubType)
    joinmode = Property('club_joinmode', ClubJoinMode)
    members = ListProperty('club_member', 'cm_club', 'cm_user', 'User')
    all_act = ListProperty('activities', 'act_club', 'act_id', 'Activity')

    @property
    def is_excellent(self):
        return self in self.excellentclubs()

    @property
    def members_num(self):
        num = 0
        for member in self.members:
            num += 1
        return num

    # We can't use @property because fails with Club.excellentclubs = []
    @classmethod
    def excellentclubs(cls, amount=None):
        ret = [cls(item) for item in redis.RedisList('excellentclubs', -1)]
        if amount:
            amount = min(len(ret), amount)
            return random.sample(ret, amount)
        return ret

    @staticmethod
    def set_excellentclubs(newval):
        newval = [item.id for item in newval]
        lst = redis.RedisList('excellentclubs', -1)
        lst[:] = newval

    @classmethod
    def randomclubs(cls, amount, types=None, active_only=True):
        where = []
        if types:
            types = [_type.value for _type in types]
            where.append(('in', 'club_type', types))
        if active_only:
            where.append(('=', 'club_inactive', False))
        tempdata = database.fetch_onecol(
            cls.table,
            cls.identifier,
            {
                'where': where,
                'order': [(database.RawSQL('RAND()'), True)],
                'limit': amount
            }
        )
        return [cls(item) for item in tempdata]

    @classmethod
    @paged_db_read
    def allclubs(cls, types=None, active_only=True, pager=None):
        where = []
        if types:
            types = [_type.value for _type in types]
            where.append(('in', 'club_type', types))
        if active_only:
            where.append(('=', 'club_inactive', False))

        pager_fetch, pager_return = pager

        tempdata = pager_fetch(
            database.fetch_onecol,
            cls.table,
            cls.identifier,
            {
                'where': where,
            }
        )
        return pager_return([cls(item) for item in tempdata])

    def activities(self, types, dates=(True, True)):<commit_msg>club.py: activity() default to all activities<commit_after>#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#

from __future__ import absolute_import, unicode_literals

from datetime import date
import random

from oclubs.access import database, redis
from oclubs.enums import ClubType, UserType, ClubJoinMode
from oclubs.objs.base import BaseObject, Property, ListProperty, paged_db_read


class Club(BaseObject):
    table = 'club'
    identifier = 'club_id'
    name = Property('club_name', search=True)
    teacher = Property('club_teacher', 'User')
    leader = Property('club_leader', 'User')
    description = Property('club_desc', 'FormattedText', search=True)
    location = Property('club_location')
    is_active = Property('club_inactive', lambda v: not v)
    intro = Property('club_intro', search=True)
    picture = Property('club_picture', 'Upload')
    type = Property('club_type', ClubType)
    joinmode = Property('club_joinmode', ClubJoinMode)
    members = ListProperty('club_member', 'cm_club', 'cm_user', 'User')
    all_act = ListProperty('activities', 'act_club', 'act_id', 'Activity')

    @property
    def is_excellent(self):
        return self in self.excellentclubs()

    @property
    def members_num(self):
        num = 0
        for member in self.members:
            num += 1
        return num

    # We can't use @property because fails with Club.excellentclubs = []
    @classmethod
    def excellentclubs(cls, amount=None):
        ret = [cls(item) for item in redis.RedisList('excellentclubs', -1)]
        if amount:
            amount = min(len(ret), amount)
            return random.sample(ret, amount)
        return ret

    @staticmethod
    def set_excellentclubs(newval):
        newval = [item.id for item in newval]
        lst = redis.RedisList('excellentclubs', -1)
        lst[:] = newval

    @classmethod
    def randomclubs(cls, amount, types=None, active_only=True):
        where = []
        if types:
            types = [_type.value for _type in types]
            where.append(('in', 'club_type', types))
        if active_only:
            where.append(('=', 'club_inactive', False))
        tempdata = database.fetch_onecol(
            cls.table,
            cls.identifier,
            {
                'where': where,
                'order': [(database.RawSQL('RAND()'), True)],
                'limit': amount
            }
        )
        return [cls(item) for item in tempdata]

    @classmethod
    @paged_db_read
    def allclubs(cls, types=None, active_only=True, pager=None):
        where = []
        if types:
            types = [_type.value for _type in types]
            where.append(('in', 'club_type', types))
        if active_only:
            where.append(('=', 'club_inactive', False))

        pager_fetch, pager_return = pager

        tempdata = pager_fetch(
            database.fetch_onecol,
            cls.table,
            cls.identifier,
            {
                'where': where,
            }
        )
        return pager_return([cls(item) for item in tempdata])

    def activities(self, types=(), dates=(True, True)):
{'fraction_non_alphanumeric': 0.11782671596206398, 'fraction_numerical': 0.0016074586079408454, 'mean_word_length': 2.4432761483121195, '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': '1027207', 'n_tokens_mistral': 1893, 'n_tokens_neox': 1826, 'n_words': 464}