def test_remove_friendship__no_requests(self): with self.check_no_messages(self.account_2.id): with self.check_no_messages(self.account_1.id): FriendshipPrototype.remove_friendship(self.account_1, self.account_2) self.assertEqual(Friendship.objects.all().count(), 0)
def test_get_friends_for__only_candidates(self): FriendshipPrototype.request_friendship(self.account_1, self.account_2, 'text 1') FriendshipPrototype.request_friendship(self.account_3, self.account_1, 'text 2') self.assertEqual(FriendshipPrototype.get_friends_for(self.account_1), [])
def test_remove_friendship(self): FriendshipPrototype.request_friendship(self.account_1, self.account_2, 'text 1')._confirm() self.check_ajax_ok( self.client.post( url('accounts:friends:remove', friend=self.account_2.id))) self.assertEqual(Friendship.objects.all().count(), 0)
def test_remove_friendship__his_request_exists(self): with self.check_new_message(self.account_2.id, [accounts_logic.get_system_user_id()]): with self.check_new_message(self.account_1.id, [self.account_2.id]): FriendshipPrototype.request_friendship(self.account_2, self.account_1, 'text 1') FriendshipPrototype.remove_friendship(self.account_1, self.account_2) self.assertEqual(Friendship.objects.all().count(), 0)
def test_remove_friendship__his_request_exists(self): FriendshipPrototype.request_friendship(self.account_2, self.account_1, 'text 1') FriendshipPrototype.remove_friendship(self.account_1, self.account_2) self.assertEqual(Friendship.objects.all().count(), 0) self.assertEqual(Message.objects.all().count(), 2) self.assertEqual(Message.objects.all().order_by('id')[1].recipient_id, self.account_2.id)
def test_get_friends_for__friends_exists(self): FriendshipPrototype.request_friendship(self.account_1, self.account_2, 'text 1')._confirm() FriendshipPrototype.request_friendship(self.account_3, self.account_1, 'text 2')._confirm() self.assertEqual( set(account.id for account in FriendshipPrototype.get_friends_for( self.account_1)), set([self.account_2.id, self.account_3.id]))
def test_friends__candidates_only(self): FriendshipPrototype.request_friendship(self.account_1, self.account_2, 'text 1') FriendshipPrototype.request_friendship(self.account_1, self.account_3, 'text 2') self.check_html_ok(self.request_html(url('accounts:friends:')), texts=[('pgf-no-friends-message', 1), (self.account_2.nick, 0), (self.account_3.nick, 0), (self.clan_2.abbr, 0), (self.clan_3.abbr, 0)])
def test_index(self): FriendshipPrototype.request_friendship(self.account_1, self.account_2, 'text 1')._confirm() FriendshipPrototype.request_friendship(self.account_1, self.account_3, 'text 2')._confirm() self.check_html_ok(self.request_html(url('accounts:friends:')), texts=[('pgf-no-friends-message', 0), (self.account_2.nick, 1), (self.account_3.nick, 1), (self.clan_2.abbr, 1), (self.clan_3.abbr, 1)])
def test_show_friends_in_list_button(self): self.request_login(self.account_2.email) texts = [('pgf-friends-request-friendship', 0), ('pgf-friends-in-list', 1), ('pgf-friends-request-from', 0), ('pgf-friends-request-to', 0)] FriendshipPrototype.request_friendship(self.account_2, self.account_1, text='text')._confirm() self.check_html_ok(self.request_html(reverse('accounts:show', args=[self.account_1.id])), texts=texts)
def test_show_friends_in_list_button(self): self.request_login('*****@*****.**') texts = [('pgf-friends-request-friendship', 0), ('pgf-friends-in-list', 1), ('pgf-friends-request-from', 0), ('pgf-friends-request-to', 0)] FriendshipPrototype.request_friendship(self.account_2, self.account_1, text=u'text')._confirm() self.check_html_ok(self.request_html(reverse('accounts:show', args=[self.account_1.id])), texts=texts)
def test_show_friends_request_to_button(self): self.request_login('*****@*****.**') texts = [('pgf-friends-request-friendship', 0), ('pgf-friends-in-list', 0), ('pgf-friends-request-from', 0), ('pgf-friends-request-to', 1)] FriendshipPrototype.request_friendship(self.account_2, self.account_1, text=u'text') self.check_html_ok(self.request_html(reverse('accounts:show', args=[self.account_1.id])), texts=texts)
def test_show_friends_request_to_button(self): self.request_login(self.account_2.email) texts = [('pgf-friends-request-friendship', 0), ('pgf-friends-in-list', 0), ('pgf-friends-request-from', 0), ('pgf-friends-request-to', 1)] FriendshipPrototype.request_friendship(self.account_2, self.account_1, text='text') self.check_html_ok(self.request_html(reverse('accounts:show', args=[self.account_1.id])), texts=texts)
def test_request_friendship__own_request_exists(self): own_request_1 = FriendshipPrototype.request_friendship(self.account_1, self.account_2, u'text 1') own_request_2 = FriendshipPrototype.request_friendship(self.account_1, self.account_2, u'text 2') self.assertEqual(own_request_1.id, own_request_2.id) self.assertFalse(own_request_2.is_confirmed) self.assertEqual(own_request_2.text_html, u'text 2') self.assertEqual(Friendship.objects.all().count(), 1) self.assertEqual(Message.objects.all().count(), 1) self.assertEqual(Message.objects.all()[0].recipient_id, self.account_2.id)
def test_remove_friendship__own_request_exists(self): with self.check_new_message( self.account_2.id, [accounts_logic.get_system_user_id(), self.account_1.id], number=2): FriendshipPrototype.request_friendship(self.account_1, self.account_2, 'text 1') FriendshipPrototype.remove_friendship(self.account_1, self.account_2)
def test_get_candidates_for__candidates_exists(self): FriendshipPrototype.request_friendship(self.account_1, self.account_2, 'text 1') FriendshipPrototype.request_friendship(self.account_3, self.account_1, 'text 2') self.assertEqual([ account.id for account in FriendshipPrototype.get_candidates_for( self.account_1) ], [self.account_3.id])
def test_request_friendship__own_request_exists(self): own_request_1 = FriendshipPrototype.request_friendship(self.account_1, self.account_2, 'text 1') own_request_2 = FriendshipPrototype.request_friendship(self.account_1, self.account_2, 'text 2') self.assertEqual(own_request_1.id, own_request_2.id) self.assertFalse(own_request_2.is_confirmed) self.assertEqual(own_request_2.text_html, 'text 2') self.assertEqual(Friendship.objects.all().count(), 1) self.assertEqual(Message.objects.all().count(), 1) self.assertEqual(Message.objects.all()[0].recipient_id, self.account_2.id)
def test_request_friendship__own_request_exists(self): with self.check_new_message(self.account_2.id, [self.account_1.id]): own_request_1 = FriendshipPrototype.request_friendship(self.account_1, self.account_2, 'text 1') own_request_2 = FriendshipPrototype.request_friendship(self.account_1, self.account_2, 'text 2') self.assertEqual(own_request_1.id, own_request_2.id) self.assertFalse(own_request_2.is_confirmed) self.assertEqual(own_request_2.text_html, 'text 2') self.assertEqual(Friendship.objects.all().count(), 1)
def test_request_friendship__his_request_exists(self): his_request = FriendshipPrototype.request_friendship(self.account_2, self.account_1, u'text 1') own_request = FriendshipPrototype.request_friendship(self.account_1, self.account_2, u'text 2') self.assertEqual(his_request.id, own_request.id) self.assertTrue(own_request.is_confirmed) self.assertEqual(own_request.text_html, u'text 1') self.assertEqual(Friendship.objects.all().count(), 1) self.assertEqual(Message.objects.all().count(), 2) self.assertEqual(Message.objects.all()[0].recipient_id, self.account_1.id) self.assertEqual(Message.objects.all()[1].recipient_id, self.account_2.id)
def friends(self): friends = FriendshipPrototype.get_friends_for(self.account) candidates = FriendshipPrototype.get_candidates_for(self.account) accounts_ids = [account.id for account in friends] clans_ids = [model.clan_id for model in friends] heroes = {hero.account_id: hero for hero in heroes_logic.load_heroes_by_account_ids(accounts_ids)} clans = {clan.id: clan for clan in ClanPrototype.get_list_by_id(clans_ids)} return self.template( "friends/friends_list.html", {"friends": friends, "candidates": candidates, "heroes": heroes, "clans": clans}, )
def test_request_friendship__his_request_exists(self): with self.check_new_message(self.account_2.id, [accounts_logic.get_system_user_id()]): with self.check_new_message(self.account_1.id, [self.account_2.id]): his_request = FriendshipPrototype.request_friendship(self.account_2, self.account_1, 'text 1') own_request = FriendshipPrototype.request_friendship(self.account_1, self.account_2, 'text 2') self.assertEqual(his_request.id, own_request.id) self.assertTrue(own_request.is_confirmed) self.assertEqual(own_request.text_html, 'text 1') self.assertEqual(Friendship.objects.all().count(), 1)
def friends(self): friends = FriendshipPrototype.get_friends_for(self.account) candidates = FriendshipPrototype.get_candidates_for(self.account) accounts_ids = [account.id for account in friends] clans_ids = [ model.clan_id for model in friends] heroes = dict( (model.account_id, HeroPrototype(model=model)) for model in Hero.objects.filter(account_id__in=accounts_ids)) clans = {clan.id:clan for clan in ClanPrototype.get_list_by_id(clans_ids)} return self.template('friends/friends_list.html', {'friends': friends, 'candidates': candidates, 'heroes': heroes, 'clans': clans})
def test_request_friendship__his_request_exists(self): his_request = FriendshipPrototype.request_friendship( self.account_2, self.account_1, 'text 1') own_request = FriendshipPrototype.request_friendship( self.account_1, self.account_2, 'text 2') self.assertEqual(his_request.id, own_request.id) self.assertTrue(own_request.is_confirmed) self.assertEqual(own_request.text_html, 'text 1') self.assertEqual(Friendship.objects.all().count(), 1) self.assertEqual(Message.objects.all().count(), 2) self.assertEqual(Message.objects.all()[0].recipient_id, self.account_1.id) self.assertEqual(Message.objects.all()[1].recipient_id, self.account_2.id)
def request_friendship(self, friend): if friend.is_fast: return self.json_error('friends.request_friendship.fast_friend', u'Вы не можете пригласить в друзья игрока не завершившего регистрацию') if friend.id == get_system_user().id: return self.json_error('friends.request_friendship.system_user', u'Вы не можете пригласить в друзья системного пользователя') form = RequestForm(self.request.POST) if not form.is_valid(): return self.json_error('friends.request_friendship.form_errors', form.errors) FriendshipPrototype.request_friendship(self.account, friend, text=form.c.text) return self.json_ok()
def test_request_friendship__new_request(self): own_request = FriendshipPrototype.request_friendship(self.account_1, self.account_2, u'text 1') self.assertFalse(own_request.is_confirmed) self.assertEqual(own_request.text_html, u'text 1') self.assertEqual(Friendship.objects.all().count(), 1) self.assertEqual(Message.objects.all().count(), 1) self.assertEqual(Message.objects.all()[0].recipient_id, self.account_2.id)
def show(context): from the_tale.game.ratings import relations as ratings_relations from the_tale.game.ratings import conf as ratings_conf friendship = FriendshipPrototype.get_for_bidirectional( context.account, context.master_account) master_hero = heroes_logic.load_hero(account_id=context.master_account.id) return dext_views.Page( 'accounts/show.html', content={ 'master_hero': master_hero, 'account_meta_object': meta_relations.Account.create_from_object(context.master_account), 'account_info': logic.get_account_info(context.master_account, master_hero), 'master_account': context.master_account, 'accounts_settings': conf.accounts_settings, 'RATING_TYPE': ratings_relations.RATING_TYPE, 'resource': context.resource, 'ratings_on_page': ratings_conf.ratings_settings.ACCOUNTS_ON_PAGE, 'informer_link': conf.accounts_settings.INFORMER_LINK % { 'account_id': context.master_account.id }, 'friendship': friendship })
def test_request_friendship__new_request(self): own_request = FriendshipPrototype.request_friendship( self.account_1, self.account_2, 'text 1') self.assertFalse(own_request.is_confirmed) self.assertEqual(own_request.text_html, 'text 1') self.assertEqual(Friendship.objects.all().count(), 1) self.assertEqual(Message.objects.all().count(), 1) self.assertEqual(Message.objects.all()[0].recipient_id, self.account_2.id)
def candidates(self): candidates = FriendshipPrototype.get_candidates_for(self.account) accounts_ids = [account.id for account in candidates] clans_ids = [ model.clan_id for model in candidates] heroes = {hero.account_id: hero for hero in heroes_logic.load_heroes_by_account_ids(accounts_ids)} clans = {clan.id:clan for clan in ClanPrototype.get_list_by_id(clans_ids)} return self.template('friends/friends_candidates.html', {'candidates': candidates, 'heroes': heroes, 'clans': clans})
def friends(self): friends = FriendshipPrototype.get_friends_for(self.account) candidates = FriendshipPrototype.get_candidates_for(self.account) accounts_ids = [account.id for account in friends] clans_ids = [model.clan_id for model in friends] heroes = dict( (model.account_id, HeroPrototype(model=model)) for model in Hero.objects.filter(account_id__in=accounts_ids)) clans = { clan.id: clan for clan in ClanPrototype.get_list_by_id(clans_ids) } return self.template( 'friends/friends_list.html', { 'friends': friends, 'candidates': candidates, 'heroes': heroes, 'clans': clans })
def friends(self): friends = FriendshipPrototype.get_friends_for(self.account) candidates = FriendshipPrototype.get_candidates_for(self.account) accounts_ids = [account.id for account in friends] clans_ids = [model.clan_id for model in friends] heroes = { hero.account_id: hero for hero in heroes_logic.load_heroes_by_account_ids(accounts_ids) } clans = { clan.id: clan for clan in ClanPrototype.get_list_by_id(clans_ids) } return self.template( 'friends/friends_list.html', { 'friends': friends, 'candidates': candidates, 'heroes': heroes, 'clans': clans })
def request_friendship(self, friend): if friend.is_fast: return self.json_error( 'friends.request_friendship.fast_friend', u'Вы не можете пригласить в друзья игрока не завершившего регистрацию' ) if friend.id == get_system_user().id: return self.json_error( 'friends.request_friendship.system_user', u'Вы не можете пригласить в друзья системного пользователя') form = RequestForm(self.request.POST) if not form.is_valid(): return self.json_error('friends.request_friendship.form_errors', form.errors) FriendshipPrototype.request_friendship(self.account, friend, text=form.c.text) return self.json_ok()
def show(context): from the_tale.game.ratings import relations as ratings_relations from the_tale.game.ratings import conf as ratings_conf friendship = FriendshipPrototype.get_for_bidirectional(context.account, context.master_account) master_hero = HeroPrototype.get_by_account_id(context.master_account.id) return dext_views.Page('accounts/show.html', content={'master_hero': master_hero, 'account_meta_object': meta_relations.Account.create_from_object(context.master_account), 'account_info': logic.get_account_info(context.master_account, master_hero), 'master_account': context.master_account, 'accounts_settings': conf.accounts_settings, 'RATING_TYPE': ratings_relations.RATING_TYPE, 'resource': context.resource, 'ratings_on_page': ratings_conf.ratings_settings.ACCOUNTS_ON_PAGE, 'informer_link': conf.accounts_settings.INFORMER_LINK % {'account_id': context.master_account.id}, 'friendship': friendship} )
def account_sidebar(user_account, page_account, page_caption, page_type, can_moderate=False): from the_tale.forum.models import Thread from the_tale.game.bills.prototypes import BillPrototype from the_tale.linguistics.prototypes import ContributionPrototype from the_tale.linguistics.relations import CONTRIBUTION_TYPE from the_tale.accounts.friends.prototypes import FriendshipPrototype from the_tale.accounts.clans.logic import ClanInfo from the_tale.blogs.models import Post as BlogPost, POST_STATE as BLOG_POST_STATE bills_count = BillPrototype.accepted_bills_count(page_account.id) threads_count = Thread.objects.filter(author=page_account._model).count() threads_with_posts = Thread.objects.filter(post__author=page_account._model).distinct().count() templates_count = ContributionPrototype._db_filter(account_id=page_account.id, type=CONTRIBUTION_TYPE.TEMPLATE).count() words_count = ContributionPrototype._db_filter(account_id=page_account.id, type=CONTRIBUTION_TYPE.WORD).count() folclor_posts_count = BlogPost.objects.filter(author=page_account._model, state=BLOG_POST_STATE.ACCEPTED).count() friendship = FriendshipPrototype.get_for_bidirectional(user_account, page_account) return jinja2.Markup(jinja2.render('accounts/sidebar.html', context={'user_account': user_account, 'page_account': page_account, 'page_caption': page_caption, 'master_clan_info': ClanInfo(page_account), 'own_clan_info': ClanInfo(user_account), 'friendship': friendship, 'bills_count': bills_count, 'templates_count': templates_count, 'words_count': words_count, 'folclor_posts_count': folclor_posts_count, 'threads_count': threads_count, 'threads_with_posts': threads_with_posts, 'can_moderate': can_moderate, 'page_type': page_type, 'commission': conf.accounts_settings.MONEY_SEND_COMMISSION}))
def show(context): from the_tale.game.ratings import relations as ratings_relations from the_tale.game.ratings import conf as ratings_conf friendship = FriendshipPrototype.get_for_bidirectional(context.account, context.master_account) master_hero = heroes_logic.load_hero(account_id=context.master_account.id) return dext_views.Page( "accounts/show.html", content={ "master_hero": master_hero, "account_meta_object": meta_relations.Account.create_from_object(context.master_account), "account_info": logic.get_account_info(context.master_account, master_hero), "master_account": context.master_account, "accounts_settings": conf.accounts_settings, "RATING_TYPE": ratings_relations.RATING_TYPE, "resource": context.resource, "ratings_on_page": ratings_conf.ratings_settings.ACCOUNTS_ON_PAGE, "informer_link": conf.accounts_settings.INFORMER_LINK % {"account_id": context.master_account.id}, "friendship": friendship, }, )
def accept_friendship(self, friend): FriendshipPrototype.request_friendship(self.account, friend) return self.json_ok()
def remove(self, friend): FriendshipPrototype.remove_friendship(self.account, friend) return self.json_ok()
def account_sidebar(user_account, page_account, page_caption, page_type, can_moderate=False): from the_tale.forum.models import Thread from the_tale.game.bills.prototypes import BillPrototype from the_tale.linguistics.prototypes import ContributionPrototype from the_tale.linguistics.relations import CONTRIBUTION_TYPE from the_tale.accounts.friends.prototypes import FriendshipPrototype from the_tale.accounts.clans.logic import ClanInfo from the_tale.blogs.models import Post as BlogPost, POST_STATE as BLOG_POST_STATE bills_count = BillPrototype.accepted_bills_count(page_account.id) threads_count = Thread.objects.filter(author=page_account._model).count() threads_with_posts = Thread.objects.filter( post__author=page_account._model).distinct().count() templates_count = ContributionPrototype._db_filter( account_id=page_account.id, type=CONTRIBUTION_TYPE.TEMPLATE).count() words_count = ContributionPrototype._db_filter( account_id=page_account.id, type=CONTRIBUTION_TYPE.WORD).count() folclor_posts_count = BlogPost.objects.filter( author=page_account._model, state=BLOG_POST_STATE.ACCEPTED).count() friendship = FriendshipPrototype.get_for_bidirectional( user_account, page_account) return jinja2.Markup( jinja2.render('accounts/sidebar.html', context={ 'user_account': user_account, 'page_account': page_account, 'page_caption': page_caption, 'master_clan_info': ClanInfo(page_account), 'own_clan_info': ClanInfo(user_account), 'friendship': friendship, 'bills_count': bills_count, 'templates_count': templates_count, 'words_count': words_count, 'folclor_posts_count': folclor_posts_count, 'threads_count': threads_count, 'threads_with_posts': threads_with_posts, 'can_moderate': can_moderate, 'page_type': page_type, 'commission': conf.accounts_settings.MONEY_SEND_COMMISSION }))
def test_remove_friendship__no_requests(self): FriendshipPrototype.remove_friendship(self.account_1, self.account_2) self.assertEqual(Friendship.objects.all().count(), 0) self.assertEqual(Message.objects.all().count(), 0)
def test_get_friends_for__friends_exists(self): FriendshipPrototype.request_friendship(self.account_1, self.account_2, 'text 1')._confirm() FriendshipPrototype.request_friendship(self.account_3, self.account_1, 'text 2')._confirm() self.assertEqual(set(account.id for account in FriendshipPrototype.get_friends_for(self.account_1)), set([self.account_2.id, self.account_3.id]))
def test_get_friends_for__no_friendship(self): self.assertEqual(FriendshipPrototype.get_friends_for(self.account_1), [])
def test_remove_friendship(self): FriendshipPrototype.request_friendship(self.account_1, self.account_2, 'text 1')._confirm() self.check_ajax_ok(self.client.post(url('accounts:friends:remove', friend=self.account_2.id))) self.assertEqual(Friendship.objects.all().count(), 0)
def test_get_candidates_for__no_friendship(self): self.assertEqual(FriendshipPrototype.get_candidates_for(self.account_1), [])
def test_get_candidates_for__only_friends(self): FriendshipPrototype.request_friendship(self.account_1, self.account_2, 'text 1')._confirm() FriendshipPrototype.request_friendship(self.account_3, self.account_1, 'text 2')._confirm() self.assertEqual(FriendshipPrototype.get_candidates_for(self.account_1), [])
def test_get_candidates_for__candidates_exists(self): FriendshipPrototype.request_friendship(self.account_1, self.account_2, 'text 1') FriendshipPrototype.request_friendship(self.account_3, self.account_1, 'text 2') self.assertEqual([account.id for account in FriendshipPrototype.get_candidates_for(self.account_1)], [self.account_3.id])