pyplanet.views

pyplanet.views

class pyplanet.views.base.View(manager=None, id=None, version='3', body=None, template=None, timeout=0, hide_click=False, data=None, player_data=None, disable_alt_menu=False, throw_exceptions=False, relaxed_updating=False)[source]

Base view. The base view will inherit from StaticManiaLink class.

destroy()

Destroy the Manialink with it’s handlers and references. Will also hide the Manialink for all users!

destroy_sync()

Destroy the Manialink with it’s handlers and references. Will also hide the Manialink for all users!

This method is sync and will call a async method (destroying of the manialink at our players) async but will not be executed at the same time. Be aware with this one!

display(player_logins=None, **kwargs)

Display the manialink. Will also render if no body is given. Will show per player or global. depending on the data given and stored!

Parameters:player_logins – Only display to the list of player logins given.
handle_catch_all(player, action, values, **kwargs)

Override this class to handle all other actions related to this view/manialink.

Parameters:
  • player – Player instance.
  • action – Action name/string
  • values – Values provided by the user client.
  • kwargs
hide(player_logins=None)

Hide manialink globally of only for the logins given in parameter.

Parameters:player_logins – Only hide for list of players, None for all players on the server.
render(player_login=None, data=None, player_data=None, template=None)

Render template. Will render template and return body.

Parameters:
  • player_login – Render data only for player, set to None to globally render (and ignore player_data).
  • data – Data to append.
  • player_data – Data to append.
  • template (pyplanet.core.ui.template.Template) – Template instance to use.
Returns:

Body, rendered manialink + script.

subscribe(action, target)

Subscribe to a action given by the manialink.

Parameters:
  • action – Action name.
  • target – Target method.
Returns:

class pyplanet.views.template.TemplateView(manager=None, id=None, version='3', body=None, template=None, timeout=0, hide_click=False, data=None, player_data=None, disable_alt_menu=False, throw_exceptions=False, relaxed_updating=False)[source]

The TemplateView will provide a view based on a XML template (ManiaLink for example). The view contains some class properties that are required to work. Those are described bellow.

To use the TemplateView. Initiate it in your own View class, and override one of the following methods:

Method get_context_data():
 Return the global context data here. Make sure you use the super() to retrieve the current context.
Method get_all_player_data(logins):
 Retrieve the player specific dictionary. Return dict with player as key and value should contain the data dict.
Method get_per_player_data(login):
 Retrieve the player specific dictionary per player. Return dict with the data dict for the specific login (player).
Method get_template():
 Return the template instance from Jinja2. You mostly should not override this method.

As alternative you can manipulate the instance.data and instance.player_data too.

Properties that are useful to change:

Prop data:Global context data. Dict.
Prop player_data:
 Player context data. Dict with player as key.
Prop hide_click:
 Should the manialink disappear after clicking a button/text.
Prop timeout:Timeout to hide manialink in seconds.

Example usage:

class AlertView(TemplateView):
        template_name = 'my_app/test.xml' # template should be in: ./my_app/templates/test.xml
        # Some prefixes that can be used in the template_name:
        #
        # - core.views: ``pyplanet.views.templates``.
        # - core.pyplanet: ``pyplanet.apps.core.pyplanet.templates``.
        # - core.maniaplanet: ``pyplanet.apps.core.pyplanet.templates``.
        # - core.trackmania: ``pyplanet.apps.core.trackmania.templates``.
        # - core.shootmania: ``pyplanet.apps.core.shootmania.templates``.
        # - [app_label]: ``[app path]/templates``.

        async def get_context_data(self):
                context = await super().get_context_data()
                context['title'] = 'Sample'
                return context
destroy()

Destroy the Manialink with it’s handlers and references. Will also hide the Manialink for all users!

destroy_sync()

Destroy the Manialink with it’s handlers and references. Will also hide the Manialink for all users!

This method is sync and will call a async method (destroying of the manialink at our players) async but will not be executed at the same time. Be aware with this one!

display(player_logins=None, **kwargs)[source]

Display the manialink. Will also render if no body is given. Will show per player or global. depending on the data given and stored!

Parameters:player_logins – Only display to the list of player logins given.
get_all_player_data(logins)[source]

Get all player data, should return dictionary with login as key, and dict as value.

Parameters:logins – Login list of players. String list.
Returns:Dictionary with data.
get_context_data()[source]

Get global and local context data, used to render template.

get_per_player_data(login)[source]

Get data for specific player. Will be called for all players that will render the xml for.

Parameters:login (str) – Player login string.
Returns:Dictionary or None to ignore.
get_player_data()[source]

Get data per player, return dict with login => data dict.

Deprecated since version 0.4.0: Use get_per_player_data() and get_all_player_data() instead. Will be removed in 0.6.0!

handle_catch_all(player, action, values, **kwargs)

Override this class to handle all other actions related to this view/manialink.

Parameters:
  • player – Player instance.
  • action – Action name/string
  • values – Values provided by the user client.
  • kwargs
hide(player_logins=None)

Hide manialink globally of only for the logins given in parameter.

Parameters:player_logins – Only hide for list of players, None for all players on the server.
render(*args, player_login=None, **kwargs)[source]

Render template for player. This will only render the body and return it. Not send it!

Parameters:player_login – Render data only for player, set to None to globally render (and ignore player_data).
Returns:Body, rendered manialink + script.
subscribe(action, target)

Subscribe to a action given by the manialink.

Parameters:
  • action – Action name.
  • target – Target method.
Returns:

pyplanet.views.generics

class pyplanet.views.generics.alert.AlertView(message, size='md', buttons=None, manager=None, target=None, **data)[source]

The AlertView can be used to show several generic alerts to a player. You can use 3 different sizes, and adjust the message text.

The 3 sizes: sm, md and lg.

__init__(message, size='md', buttons=None, manager=None, target=None, **data)[source]

Create an AlertView instance.

Parameters:
  • message (str) – The message to display to the end-user, Use \n for new lines. You can use symbols from FontAwesome by using Unicode escaped strings.
  • size (str) – Size to use, this parameter should be a string, and one of the following choices: ‘sm’, ‘md’ or ‘lg. Defaults to ‘md’.
  • buttons (list) – Buttons to display, Should be an array with dictionary which contain: name.
  • manager (pyplanet.core.ui._BaseUIManager) – UI Manager to use, You should always keep this undefined unless you know what your doing!
  • target – Target coroutine method called as handle of button clicks.
close(player, **kwargs)[source]

Close the alert.

wait_for_reaction()[source]

Wait for reaction or input and return it.

Returns:Returns the button clicked or the input value string of the user.
class pyplanet.views.generics.alert.PromptView(message, size='md', buttons=None, manager=None, default='', validator=None)[source]

The PromptView is like the AlertView but can ask for a text entry.

The 3 sizes: sm, md and lg.

You can listen for the results of the players input with the wait_for_input() async handler (future). Example:

prompt = PromptView('Please enter your name')
await prompt.display(['login'])

user_input = await prompt.wait_for_input()
print(user_input)

You can do validations before it’s okay with giving a function to the argument validator. Example:

def my_validator(value):
        try:
                int(value)
                return True, None
        except:
                return False, 'Value should be an integer!'

prompt = PromptView('Please enter your name', validator=my_validator)
await prompt.display(['login'])

user_input = await prompt.wait_for_input()
print(user_input)
wait_for_input()[source]

Wait for input and return it.

Returns:Returns the string value of the user.
pyplanet.views.generics.alert.ask_confirmation(player, message, size='md', buttons=None)[source]

Ask the player for confirmation and return the button number (0 is first button).

Parameters:
  • player – Player login or instance.
  • message – Message to display.
  • size – Size, could be ‘sm’, ‘md’, or ‘lg’.
  • buttons – Buttons, optional, default is yes and no.
Returns:

Number of button that is clicked.

pyplanet.views.generics.alert.show_alert(player, message, size='md', buttons=None)[source]

Show an alert to the player with given details. This is a shortcut method for the class itself.

Parameters:
  • player – Player login or instance.
  • message – Message in string.
  • size – Size, could be ‘sm’, ‘md’, or ‘lg’.
  • buttons – Buttons, optional, default is ‘OK’.
Returns:

Number of the clicked button. (in Future).

class pyplanet.views.generics.list.ListView(*args, **kwargs)[source]

The ListView is an abstract list that uses a database query to show and manipulate the list that is presented to the end-user. The ListView is able to automatically manage the searching, ordering and pagination of your query contents.

The columns could be specified, for each column you can change behaviour, such as searchable and sortable. But also custom rendering of the values that will be displayed.

You can override get_fields(), get_actions(), get_query() if you need any customization or use a self method or variable in one of your properties.

Note

The design and some behaviour can change in updates of PyPlanet. We aim to provide backward compatibility as much as we can. If we are going to break things we will make it deprecated, or if we are in a situation of not having enough time to provide a transition time, we are going to create a separate solution (like a second version).

class SampleListView(ListView):
        query = Model.select()
        model = Model
        title = 'Select your item'
        fields = [
                {'name': 'Name', 'index': 'name', 'searching': True, 'sorting': True},
                {'name': 'Author', 'index': 'author', 'searching': True, 'sorting': True},
        ]
        actions = [
                {
                        'name': 'Delete',
                        'action': self.action_delete,
                        'style': 'Icons64x64_1',
                        'substyle': 'Close'
                },
        ]

        async def action_delete(self, player, values, instance, **kwargs):
                print('Delete value: {}'.format(instance))
close(player, *args, **kwargs)[source]

Close the link for a specific player. Will hide manialink and destroy data for player specific to save memory.

Parameters:player (pyplanet.apps.core.maniaplanet.models.Player) – Player model instance.
display(player=None)[source]

Display list to player.

Parameters:player (str, pyplanet.apps.core.maniaplanet.models.Player) – Player login or model instance.
refresh(player, *args, **kwargs)[source]

Refresh list with current properties for a specific player. Can be used to show new data changes.

Parameters:player (pyplanet.apps.core.maniaplanet.models.Player) – Player model instance.
single_list = True

Change this to False to have multiple lists open at the same time.

class pyplanet.views.generics.list.ManualListView(data=None, *args, **kwargs)[source]

The ManualListView will act as a ListView, but not based on a model or query.

get_data()[source]

Override this method, return a list with dictionaries inside.