blob: 67961406ea297c16042745f9d737586ab636a90e [file] [log] [blame]
# Copyright 2011 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Module for rendering toggle buttons for enabling/disabling feature."""
from soc.views.template import Template
class ToggleButtonTemplate(Template):
"""Template to render toggle buttons."""
def __init__(self, data, button_type, title, button_id, post_url,
checked=False, help_text=None, note=None, labels=None):
"""Instantiates the template for rendering review page buttons.
Args:
data: RequestData object
button_type: Type of the button, can be one of on_off, disabled or long
title: The text that must be displayed before the button
button_id: value to be given to HTML id attribute for button
post_url: The url to which post should be performed on click
enable: if True Enable button is activated otherwise Disable button
help_text: Tooltip to be displayed for the button.
note: additional message to be displayed along with the buttons
labels: dictionary containing the label for each of the two buttons
"""
super(ToggleButtonTemplate, self).__init__(data)
self.checked = checked
self.help_text = help_text
self.labels = labels
self.note = note
self.title = title
self.button_type = button_type
self.button_id = button_id
self.post_url = post_url
def context(self):
"""The context for this template used in render()."""
context = {
'button': self,
}
return context
@property
def state(self):
"""Returns the state as needed by the Javascript and HTML."""
return 'checked' if self.checked else 'unchecked'
def templatePath(self):
return 'soc/_toggle_button.html'
class ToggleButtonTwoActionsTemplate(Template):
"""Template to render toggle buttons which defines two different actions,
represented by URLs. One action is triggered when the button gets checked,
whereas the other action starts when the button gets unchecked."""
def __init__(self, data, button_type, title, button_id, on_check_url,
on_uncheck_url, checked=False, help_text=None, note=None, labels=None):
"""Instantiates the template for rendering review page buttons.
Args:
data: RequestData object
button_type: Type of the button, can be one of on_off, disabled or long.
title: The text that must be displayed before the button.
button_id: value to be given to HTML id attribute for the button.
on_check_url: The URL to which POST request is sent when the button
is checked.
on_uncheck_url: The URL to which POST request is sent when the button
is unchecked.
checked: If True the toggle button is checked.
help_text: Tooltip to be displayed for the button.
note: additional message to be displayed along with the buttons
labels: A dict containing the label for each of the two states.
"""
super(ToggleButtonTwoActionsTemplate, self).__init__(data)
self.checked = checked
self.help_text = help_text
self.labels = labels
self.note = note
self.title = title
self.button_type = button_type
self.button_id = button_id
self.on_check_url = on_check_url
self.on_uncheck_url = on_uncheck_url
def context(self):
"""The context for this template used in render()."""
context = {
'button': self,
}
return context
@property
def state(self):
"""Returns the state as needed by the Javascript and HTML."""
return 'checked' if self.checked else 'unchecked'
def templatePath(self):
return 'soc/_toggle_button.html'