Clean up too long lines in profile.py. Add ReadOnlyInput widget to custom_widgets module, it can be used to display read-only form fields. Display read-only "Id" field in LookUp view if user has been found and in User Edit Developer view.
Patch by: Pawel Solyga
Review by: to-be-reviewed
--- a/app/soc/templates/soc/site/user/profile/edit.html Tue Sep 30 16:26:30 2008 +0000
+++ b/app/soc/templates/soc/site/user/profile/edit.html Tue Sep 30 16:49:54 2008 +0000
@@ -38,6 +38,9 @@
<form method="POST">
{{ form.key_name }}
<table>
+ {% if existing_user %}
+ {% readonly_field_as_table_row "Id" existing_user.id %}
+ {% endif %}
{% field_as_table_row form.id %}
{% if lookup_error %}
<tr>
--- a/app/soc/templates/soc/site/user/profile/lookup.html Tue Sep 30 16:26:30 2008 +0000
+++ b/app/soc/templates/soc/site/user/profile/lookup.html Tue Sep 30 16:49:54 2008 +0000
@@ -28,6 +28,9 @@
</p>
<form method="POST">
<table>
+{% if found_user %}
+{% readonly_field_as_table_row "Id" found_user.id %}
+{% endif %}
{% if email_error %}
<tr>
<td> </td>
--- a/app/soc/views/helpers/custom_widgets.py Tue Sep 30 16:26:30 2008 +0000
+++ b/app/soc/views/helpers/custom_widgets.py Tue Sep 30 16:49:54 2008 +0000
@@ -28,49 +28,62 @@
from django.utils import simplejson
from django.utils.safestring import mark_safe
-class TinyMCE(forms.widgets.Textarea):
- """TinyMCE widget.
-
- Requires to include tiny_mce_src.js in your template. Widget can be
- customized by overwriting or adding extra options to mce_settings
- dictionary
+
+class ReadOnlyInput(forms.widgets.Input):
+ """Read only input widget.
+ """
+ input_type = 'text'
+
+ def render(self, name, value, attrs=None):
+ """Render ReadOnlyInput widget as HTML.
+ """
+ attrs['readonly'] = 'readonly'
+ return super(ReadOnlyInput, self).render(name, value, attrs)
+
- You can set TinyMCE widget for particular form field using code below:
- class ExampleForm(forms_helpers.DbModelForm):
- content = forms.fields.CharField(widget=custom_widgets.TinyMCE())
-
- You can include tiny_mce_src.js in your template using:
- {% block scripts %}
- <script type="text/javascript" src="/tiny_mce/tiny_mce_src.js"></script>
- {% endblock %}
- """
- DEF_MCE_SETTINGS = { 'mode': "exact",
- 'theme': "simple",
- 'theme_advanced_toolbar_location': "top",
- 'theme_advanced_toolbar_align': "center"}
+class TinyMCE(forms.widgets.Textarea):
+ """TinyMCE widget.
+
+ Requires to include tiny_mce_src.js in your template. Widget can be
+ customized by overwriting or adding extra options to mce_settings
+ dictionary
- mce_settings = DEF_MCE_SETTINGS.copy()
+ You can set TinyMCE widget for particular form field using code below:
+ class ExampleForm(forms_helpers.DbModelForm):
+ content = forms.fields.CharField(widget=custom_widgets.TinyMCE())
+
+ You can include tiny_mce_src.js in your template using:
+ {% block scripts %}
+ <script type="text/javascript" src="/tiny_mce/tiny_mce_src.js"></script>
+ {% endblock %}
+ """
+ DEF_MCE_SETTINGS = { 'mode': "exact",
+ 'theme': "simple",
+ 'theme_advanced_toolbar_location': "top",
+ 'theme_advanced_toolbar_align': "center"}
- TINY_MCE_HTML_FMT = u'''\
+ mce_settings = DEF_MCE_SETTINGS.copy()
+
+ TINY_MCE_HTML_FMT = u'''\
<textarea %(attrs)s>%(value)s</textarea>
<script type="text/javascript">
- tinyMCE.init(%(settings_json)s)
+tinyMCE.init(%(settings_json)s)
</script>'''
+
+ def render(self, name, value, attrs=None):
+ """Render TinyMCE widget as HTML.
+ """
+ if value is None:
+ value = ''
+ value = smart_unicode(value)
+ final_attrs = self.build_attrs(attrs, name=name)
- def render(self, name, value, attrs=None):
- """Render TinyMCE widget as HTML.
- """
- if value is None:
- value = ''
- value = smart_unicode(value)
- final_attrs = self.build_attrs(attrs, name=name)
+ self.mce_settings['elements'] = "id_%s" % name
- self.mce_settings['elements'] = "id_%s" % name
-
- # convert mce_settings from dict to JSON
- mce_json = simplejson.JSONEncoder().encode(self.mce_settings)
+ # convert mce_settings from dict to JSON
+ mce_json = simplejson.JSONEncoder().encode(self.mce_settings)
- return mark_safe(self.TINY_MCE_HTML_FMT %
- {'attrs': flatatt(final_attrs),
- 'value': escape(value),
- 'settings_json': mce_json})
+ return mark_safe(self.TINY_MCE_HTML_FMT %
+ {'attrs': flatatt(final_attrs),
+ 'value': escape(value),
+ 'settings_json': mce_json})
--- a/app/soc/views/site/user/profile.py Tue Sep 30 16:26:30 2008 +0000
+++ b/app/soc/views/site/user/profile.py Tue Sep 30 16:49:54 2008 +0000
@@ -146,7 +146,7 @@
# else: # method == 'GET'
if user:
- # User entity found, so populate form with existing User information
+ # User entity found, so populate form with existing User information
# context['found_user'] = user
form = LookupForm(initial={'id': user.id.email,
'link_name': user.link_name})
@@ -298,7 +298,7 @@
# populate form with the existing User entity
form = EditForm(initial={ 'key_name': user.key().name(),
'id': user.id.email, 'link_name': user.link_name,
- 'nick_name': user.nick_name, 'is_developer': user.is_developer})
+ 'nick_name': user.nick_name, 'is_developer': user.is_developer})
else:
if request.GET.get(profile.SUBMIT_MSG_PARAM_NAME):
# redirect to aggressively remove 'Profile saved' query parameter