示例#1
0
    def test_success(self):

        deck = Card(type=Card.TEXT,
                    name='1 apple',
                    description='...green...',
                    published=True)
        self.db.session.add(deck)
        card = Card(type=Card.TEXT,
                    name='2 green',
                    description='...apple...',
                    published=True)
        self.db.session.add(card)
        card_no = Card(type=Card.TEXT, name='3 does not match', published=True)
        self.db.session.add(card_no)
        self.db.session.commit()

        data = {
            'keywords': ['apple', 'green'],
        }

        response = self.client.get(self.url,
                                   data=json.dumps(data),
                                   content_type='application/json')
        self.assertEqual(response.status, '200 OK')
        cards = json.loads(response.data)['cards']
        cards = sorted(cards, key=lambda c: c['name'])
        self.assertEqual(len(cards), 2)
        self.assertEqual(cards[0]["id"], deck.id)
        self.assertEqual(cards[1]["id"], card.id)
示例#2
0
    def test_givers_and_is_answer_flag(self):
        deck = factories.CardFactory(type=Card.DECK, is_ask_mode_enabled=True)
        self.db.session.add(deck)
        self.db.session.commit()

        question_creator = User(username='******')
        self.db.session.add(question_creator)
        question = Card(type=Card.TEXT,
                        parent_id=deck.id,
                        position=0,
                        is_answer=False,
                        creator=question_creator)
        self.db.session.add(question)

        giver1 = User(username='******')
        self.db.session.add(giver1)

        answer = Card(type=Card.TEXT,
                      parent_id=deck.id,
                      creator=giver1,
                      position=1,
                      is_answer=True)
        self.db.session.add(answer)

        answer1 = Card(type=Card.TEXT,
                       parent_id=deck.id,
                       creator=giver1,
                       position=2,
                       is_answer=True)
        self.db.session.add(answer1)

        question1 = Card(type=Card.TEXT,
                         parent_id=deck.id,
                         position=3,
                         is_answer=False,
                         creator=question_creator)
        self.db.session.add(question1)

        self.db.session.commit()

        res = deck.to_dict()
        self.assertEqual(len(res['givers']), 1)
        self.assertEqual(res['givers'][0]['user_id'], giver1.id)

        #check if children elements has correct flag  is_answer
        children = res['children']
        self.assertEqual(len(children), 4)

        self.assertEqual(children[0]['id'], question.id)
        self.assertEqual(children[0]['is_answer'], False)

        self.assertEqual(children[1]['id'], answer.id)
        self.assertEqual(children[1]['is_answer'], True)

        self.assertEqual(children[2]['id'], answer1.id)
        self.assertEqual(children[2]['is_answer'], True)

        self.assertEqual(children[3]['id'], question1.id)
        self.assertEqual(children[3]['is_answer'], False)
示例#3
0
def create_test_cards():
    """Creates the roles."""
    from lib.models.card import Card
    from lib.models.user import User
    u = User.query.filter(User.email == '*****@*****.**').first()
    deck = Card(type=Card.DECK, creator=u)
    card1 = Card(type=Card.TEXT, creator=u, parent=deck)
    card2 = Card(type=Card.TEXT, creator=u, parent=deck)

    db.session.add(card1)
    db.session.add(card2)
    db.session.commit()
    print(card1.id)
    print(card2.id)
示例#4
0
    def test_success(self):
        deck = Card(evaluation_period_status=card_module.EVALUATION_PERIOD_STATUS_DONE)
        self.db.session.add(deck)
        user = User()
        self.db.session.add(user)
        ucr = UserRoleCard(user=user, card=deck, role_id=card_role.CardRole.ASKED, total_likes=10, prize=100)
        self.db.session.add(ucr)

        self.db.session.commit()
        data = {
            'deck_id': deck.id
        }
        response = self.client.get('/api/v1/deck/get_winners', query_string=data, content_type='application/json')
        self.assertEqual(response.status, '200 OK')
        res = flask.json.loads(response.data)

        total_likes = res['total_likes']
        self.assertEqual(total_likes, 10)

        winners = res['winners']
        self.assertEqual(len(winners), 1)
        it = winners[0]
        self.assertEqual(it['user_id'], user.id)
        self.assertEqual(it['likes'], 10)
        self.assertEqual(it['likes_relative'], 100)
        self.assertEqual(it['prize'], 100)
示例#5
0
    def call_target(self):
        self.owner = User(username='******')
        self.db.session.add(self.owner)
        self.deck = Card(type=Card.DECK,
                         name='test_deck',
                         creator=self.owner,
                         is_ask_mode_enabled=True,
                         silver_points=self.deck_silver_points,
                         prize_to_join=10)
        self.db.session.add(self.deck)
        self.db.session.commit()

        self.join_user = self.user
        ucr = UserRoleCard(user_id=self.join_user.id,
                           role_id=CardRole.JOINED,
                           card_id=self.deck.id)
        self.db.session.add(ucr)
        self.db.session.commit()

        data = {'deck_id': self.deck.id}

        response = self.client.post('/api/v1/deck/unjoin_ask',
                                    data=json.dumps(data),
                                    content_type='application/json')
        self.response = response
        return response
示例#6
0
    def test_constructorParsesDictionary(self):
        # Arrange
        name = 'testCard'
        widgetId = '123'
        cardId = '987'
        columnId = '456'
        organizationId = '012'

        dictionary = {
            'name': name,
            'widgetCommonId': widgetId,
            'cardId': cardId,
            'columnId': columnId,
            'organizationId': organizationId
        }

        # Act
        card = Card(dictionary)

        # Assert
        self.assertEqual(name, card.name)
        self.assertEqual(widgetId, card.widgetId)
        self.assertEqual(cardId, card.cardId)
        self.assertEqual(columnId, card.columnId)
        self.assertEqual(organizationId, card.organizationId)
示例#7
0
    def test_visible(self):
        from lib.models.user_permission import UserPermission
        user = User(username='******', password='******', active=True)
        role = Role(name='homes')
        user.roles.append(role)
        self.db.session.add(user)
        self.db.session.commit()

        deck_visible = Card(type=Card.DECK, name='1 visible_deck')
        self.db.session.add(deck_visible)

        deck_owned = Card(type=Card.DECK,
                          name='2 owned_by_user',
                          user_id=user.id)
        self.db.session.add(deck_owned)

        deck_invisible = Card(type=Card.DECK, name='3 invisible_deck')
        self.db.session.add(deck_invisible)

        deck_visible_by_role = Card(type=Card.DECK, name='4 visible_by_role')
        self.db.session.add(deck_visible_by_role)

        self.db.session.commit()

        up1 = UserPermission(card_id=deck_visible.id,
                             permission_id='view',
                             user_id=user.id)
        self.db.session.add(up1)

        up2 = UserPermission(card_id=deck_visible_by_role.id,
                             permission_id='view',
                             role_id=role.id)
        self.db.session.add(up2)

        self.db.session.commit()

        deck_repo = DeckRepo()
        decks = list(deck_repo.get_viewable_decks(user))
        decks = sorted(decks, key=lambda d: d.name)
        self.assertEqual(len(decks), 3)
        self.assertEqual(decks[0].id, deck_visible.id)
        self.assertEqual(decks[1].id, deck_owned.id)
        self.assertEqual(decks[2].id, deck_visible_by_role.id)
示例#8
0
    def test_anyone(self):
        deck = Card(type=Card.DECK, name='DEck Test')
        self.db.session.add(deck)
        self.db.session.commit()

        urc = UserRoleCard(user_id=User.ANYONE_ID,
                           role_id=CardRole.ASKED,
                           card_id=deck.id)
        self.db.session.add(urc)

        self.db.session.commit()

        users = card.get_asked_users(deck)
        self.assertEqual(users, [{'user_id': None, 'username': '******'}])
示例#9
0
 def _get_duplicate(self, user, card, now):
     return Card(
         type=card.type,
         name=card.name,
         content=card.content,
         language=card.language,
         pronunciation=card.pronunciation,
         created_at=now,
         updated_at=now,
         description=card.description,
         x_position=card.x_position,
         y_position=card.y_position,
         width=card.width,
         height=card.height
     )
示例#10
0
    def setUp(self):
        super(SetAskersTestCase, self).setUp()

        # input data
        self.owner = User(username='******')
        self.db.session.add(self.owner)
        self.deck = Card(type=Card.DECK,
                         name='test_deck',
                         creator=self.owner,
                         is_ask_mode_enabled=True)
        self.db.session.add(self.deck)
        self.asker = User(username='******')
        self.db.session.add(self.asker)
        self.db.session.commit()
        self.asker_ids = None
    def setUp(self):
        super(GiveCardToDeckTestCase, self).setUp()
        self.deck_creator = User(username='******')

        self.deck = Card(
            type=Card.DECK,
            name='test_deck',
            is_ask_mode_enabled=True,
            evaluation_period_status=card_module.EVALUATION_PERIOD_STATUS_OPEN,
            creator=self.deck_creator)
        self.db.session.add(self.deck)
        self.question_creator = User(username='******')
        self.db.session.add(self.question_creator)
        self.question = Card(type=Card.TEXT,
                             name='question',
                             creator=self.question_creator)
        self.db.session.add(self.question)
        self.deck.cards.append(self.question)

        self.creator = User(username='******')
        self.answer = Card(type=Card.TEXT, name='answer', creator=self.creator)
        self.db.session.add(self.answer)

        self.db.session.commit()
示例#12
0
 def create_text_card(self, user, name, content, pronunciation_language,
                      description, tag_names=None):
     now = arrow.utcnow().timestamp
     card = Card(
         type=Card.TEXT,
         name=name,
         content=content,
         language=pronunciation_language,
         pronunciation=bool(pronunciation_language),
         created_at=now,
         updated_at=now,
         description=description
     )
     user.cards.append(card)
     self.append_tags_to_card(card, tag_names=tag_names)
     self.db.session.commit()
示例#13
0
 def create_video_card(self, user, name, content, sub_content, description,
                       tag_names=None, scale=None, video_length=None):
     now = arrow.utcnow().timestamp
     card = Card(
         type=Card.VIDEO,
         name=name,
         content=content,
         sub_content=sub_content,
         created_at=now,
         updated_at=now,
         description=description,
         scale=scale,
         video_length=video_length,
     )
     user.cards.append(card)
     self.append_tags_to_card(card, tag_names=tag_names)
     self.db.session.commit()
     return card
示例#14
0
 def copy_cards(self, user, cards):
     now = arrow.utcnow().timestamp
     for card in cards:
         if card.type == Card.DECK:
             new_deck = Card(
                 type=Card.DECK,
                 name=card.name,
                 created_at=now,
                 updated_at=now
             )
             for child in card.cards:
                 new_card = self._get_duplicate(user, child, now)
                 user.cards.append(new_card)
                 new_deck.cards.append(new_card)
             user.cards.append(new_deck)
         else:
             new_card = self._get_duplicate(user, card, now)
             user.cards.append(new_card)
     self.db.session.commit()
示例#15
0
    def create_deck(self, user, name, card_ids, public, is_ask_mode_enabled, deck_format, tag_names=None):
        now = arrow.utcnow().timestamp
        deck = Card(
            type=Card.DECK,
            name=name,
            created_at=now,
            updated_at=now,
            is_ask_mode_enabled=is_ask_mode_enabled,
            format=deck_format,
        )
        for card_id in card_ids:
            card = self.get_card(card_id)
            if not card:
                continue
            deck.cards.append(card)
        user.cards.append(deck)
        self.append_tags_to_card(deck, tag_names=tag_names)

        self.db.session.commit()
        return deck
示例#16
0
    def test_success(self):
        deck = Card(evaluation_period_status=card_module.EVALUATION_PERIOD_STATUS_DONE)
        self.db.session.add(deck)
        user = User()
        self.db.session.add(user)
        ucr = UserRoleCard(user=user, card=deck, role_id=card_role.CardRole.JOINED, total_likes=10, prize=100, contribution=10)
        self.db.session.add(ucr)

        self.db.session.commit()
        data = {
            'deck_id': deck.id
        }
        response = self.client.get('/api/v1/deck/get_sponsors', data=flask.json.dumps(data), content_type='application/json')
        self.assertEqual(response.status, '200 OK')
        res = flask.json.loads(response.data)

        sponsors = res['sponsors']
        self.assertEqual(len(sponsors), 1)
        it = sponsors[0]
        self.assertEqual(it['user_id'], user.id)
        self.assertEqual(it['contribution'], 10)
示例#17
0
    def setUp(self):
        super(JoinAskTestCase, self).setUp()
        self.user = factories.UserFactory(silver_points=100)
        self.db.session.add(self.user)
        self.set_current_user(self.user)

        # input data
        self.owner = User(username='******')
        self.db.session.add(self.owner)
        self.deck = Card(type=Card.DECK,
                         name='test_deck',
                         creator=self.owner,
                         is_ask_mode_enabled=True,
                         silver_points=0,
                         prize_to_join=10)
        self.db.session.add(self.deck)
        self.db.session.commit()
        self.asker_ids = None
        self.custom_join_prize = None

        self.response = None
示例#18
0
 def create_image_card(self, user, name, content, description,
                       image_x, image_y, image_width, image_height, tag_names=None,
                       image_scale=None):
     now = arrow.utcnow().timestamp
     card = Card(
         type=Card.IMAGE,
         name=name,
         content=content,
         created_at=now,
         updated_at=now,
         description=description,
         x_position=image_x,
         y_position=image_y,
         width=image_width,
         height=image_height,
         scale=image_scale,
     )
     user.cards.append(card)
     self.append_tags_to_card(card, tag_names=tag_names)
     self.db.session.commit()
     return card
示例#19
0
    def create_input_data(self):
        if self.owner is None:
            self.owner = User(username='******')
            self.db.session.add(self.owner)

        if self.card is None:
            self.card = Card(type=self.card_type,
                             name=self.card_name,
                             creator=self.owner)
            self.db.session.add(self.card)
        self.db.session.commit()

        if self.questions is not None:
            for idx, c in enumerate(self.questions):
                c.position = idx
                self.card.cards.append(c)
            self.db.session.commit()

        if self.asked_users is None:
            if self.asked_user1 is None:
                self.asked_user1 = User(username='******')
                self.db.session.add(self.asked_user1)
                self.db.session.commit()
            self.asked_users = [self.asked_user1]
示例#20
0
 def _clone_card(self, user, card, now):
     fields_to_copy = [
         'type',
         'name',
         'content',
         'sub_content',
         'language',
         'pronunciation',
         'published',
         'description',
         'creator',
         'x_position',
         'y_position',
         'height',
         'width',
         'scale',
         'silver_points',
         'original_prize_pool',
         'gold_points',
         'prize_to_join',
         'answer_visibility',
         'liked_users',
         'comments',
         'is_ask_mode_enabled',
         'format',
         'distribution_rule',
         'distribution_for',
         'evaluation_start_dt',
         'evaluation_end_dt',
         'evaluation_period_status',
         'is_answer',
         'tags',
         'video_length',
     ]
     kwargs = {name: getattr(card, name) for name in fields_to_copy}
     return Card(**kwargs)
示例#21
0
 def addTagToCard(self, card, tag):
     cardJson = self.requester.addTagToCard(self, card.cardId, tag.tagId)
     card = Card(cardJson)
     return card
示例#22
0
if not ivysaur_env:
    print "please set IVYSAUR_ENV environment var"
    sys.exit(1)

create_app(ivysaur_env)

from lib.registry import get_registry
from lib.models.user import User
from lib.models.card import Card
from lib.models import card as card_module
from lib.models.user_role_card import UserRoleCard
from lib.models import card_role

if __name__ == '__main__':
    db = get_registry()['DB']

    deck = Card(
        evaluation_period_status=card_module.EVALUATION_PERIOD_STATUS_DONE)
    db.session.add(deck)
    user = User()
    db.session.add(user)
    ucr = UserRoleCard(user=user,
                       card=deck,
                       role_id=card_role.CardRole.ASKED,
                       total_likes=10,
                       prize=100)
    db.session.add(ucr)

    db.session.commit()
    print(deck.id)
示例#23
0
 def getCard(self, cardId):
     cardJson = self.requester.getCardById(cardId)
     card = Card(cardJson)
     return card