Services

class cards.services.cards_services.CardsServices[source]

Bases: object

The CardsServices class provides methods for interacting with cards.

classmethod get_card_data(english_word, russian_word, word_usage)[source]

This function gets the Card from the database and returns a tuple of three values. If the user does not provide an English word or a Russian word, the function will use translator() to translate the provided language into the other language. If no usage is provided by the user, then it will be generated using the fact generator function.

Parameters:
  • cls – Refer to the class itself

  • english_word – Pass the english word to the function

  • russian_word – Pass the russian word to the function

  • word_usage – Pass a fact about the word to the function

Returns:

A tuple of three values.

classmethod get_card_from_collection(english=None, russian=None, card_id=None)[source]

Takes in a string of either an English word or Russian word, and returns the corresponding card object from the database. If no such card exists, it raises a ValueError.

Parameters:
  • cls – Refer to the class itself

  • english – Get the card from the database using its english word, default is None

  • russian – Get the card from the database, default is None

  • card_id – Get a card by its id, default is None

Returns:

A Card instance from the database

Raises:

ValueError if no card is found

classmethod get_information_from_forms(request)[source]

Takes in a request object and returns the values of the form fields.

Parameters:

cls – Refer to the class itself

Returns:

A tuple of four values

classmethod get_new_card(english, russian, usage, collection)[source]

Get the information from the parameters and create a new card in the database. If the card already exists, it will be added to the collection.

Parameters:
  • cls – Refer to the class itself

  • form – Get the information from the form

Returns:

A dictionary with a message

class cards.services.collections_services.CollectionServices[source]

Bases: object

The CollectionServices class provides methods for interacting with collections.

static change_card_position(collection, replace, card_id)[source]

The change_card_position function takes in a collection, a boolean value, and a word_id. It removes the word_id from the collection’s order_list and inserts it at the 4th index if replace is False, or appends it to the end of the list if replace is True.

Parameters:
  • collection – Collection object

  • replace – Boolean value

  • card_id – The id of the Card

Returns:

None

static create_collection(owner, collection_name, collection_img)[source]

The create_collection function takes in an owner, a collection_name, and a collection_img, and creates a new collection with the given parameters.

Parameters:
  • owner – User object

  • collection_name – The name of the collection

  • collection_img (File object, collection image) – The image of the collection

Returns:

A message indicating whether the collection was created

static get_all_cards(collection)[source]

The get_all_cards function takes a collection as an argument and returns all of the cards in that collection.

Parameters:

collection – The collection object

Returns:

A list of all the Cards in a collection

static get_collection_by_id(collection_id)[source]

The get_collection_by_id function takes in a collection_id and returns the CardsCollections object with that id.

Parameters:

collection_id – The id of the collection.

Returns:

A collection object

static get_collections_by_owner(owner_id)[source]

The get_collections_by_owner function returns a list of all collections owned by the user with the given owner_id.

Parameters:

owner_id – The id of the owner of the collections

Returns:

A list of collections that belong to the owner with the given id