diff options
-rw-r--r-- | askbot/migrations/0135_auto__add_askwidget.py | 2 | ||||
-rw-r--r-- | askbot/models/widgets.py | 17 | ||||
-rw-r--r-- | askbot/skins/default/templates/delete_ask_widget.html | 14 | ||||
-rw-r--r-- | askbot/skins/default/templates/list_ask_widget.html | 19 | ||||
-rwxr-xr-x | askbot/skins/default/templates/widgets/askbot_widget.css (renamed from askbot/skins/default/media/style/askbot-modal.css) | 8 | ||||
-rwxr-xr-x | askbot/skins/default/templates/widgets/askbot_widget.js | 31 | ||||
-rw-r--r-- | askbot/tests/widget_tests.py | 13 | ||||
-rw-r--r-- | askbot/urls.py | 12 | ||||
-rw-r--r-- | askbot/views/widgets.py | 43 |
9 files changed, 131 insertions, 28 deletions
diff --git a/askbot/migrations/0135_auto__add_askwidget.py b/askbot/migrations/0135_auto__add_askwidget.py index 11b287e8..ebf6f717 100644 --- a/askbot/migrations/0135_auto__add_askwidget.py +++ b/askbot/migrations/0135_auto__add_askwidget.py @@ -13,7 +13,7 @@ class Migration(SchemaMigration): ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), ('title', self.gf('django.db.models.fields.CharField')(max_length=100)), ('group', self.gf('django.db.models.fields.related.ForeignKey')(blank=True, related_name='groups', null=True, to=orm['askbot.Tag'])), - ('default_group', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['askbot.Tag'], null=True, blank=True)), + ('tag', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['askbot.Tag'], null=True, blank=True)), ('include_text_field', self.gf('django.db.models.fields.BooleanField')(default=False, blank=True)), ('inner_style', self.gf('django.db.models.fields.TextField')(default='')), ('outer_style', self.gf('django.db.models.fields.TextField')(default='')), diff --git a/askbot/models/widgets.py b/askbot/models/widgets.py index 09b9a2d9..3303eca7 100644 --- a/askbot/models/widgets.py +++ b/askbot/models/widgets.py @@ -4,6 +4,8 @@ from django.utils.translation import ugettext as _ from askbot.conf import settings as askbot_settings from askbot.models import Tag from askbot.models.tag import get_groups +from askbot.forms import FormWithHideableFields +from askbot.conf import settings as askbot_settings from django import forms DEFAULT_INNER_STYLE = '' @@ -28,7 +30,7 @@ class AskWidget(models.Model): def __unicode__(self): return "Widget: %s" % self.title -class CreateAskWidgetForm(forms.ModelForm): +class CreateAskWidgetForm(forms.ModelForm, FormWithHideableFields): inner_style = forms.CharField( widget=forms.Textarea, required=False, @@ -39,9 +41,16 @@ class CreateAskWidgetForm(forms.ModelForm): required=False, initial=DEFAULT_OUTER_STYLE ) - #these don't work for some reason - #group = forms.ModelChoiceField(query_set=get_groups()) - #tag = forms.ModelChoiceField(query_set=Tag.objects.get_content_tags()) + + group = forms.ModelChoiceField(queryset=get_groups().exclude(name__startswith='_internal'), + required=False) + tag = forms.ModelChoiceField(queryset=Tag.objects.get_content_tags(), + required=False) + + def __init__(self, *args, **kwargs): + super(CreateAskWidgetForm, self).__init__(*args, **kwargs) + if not askbot_settings.GROUPS_ENABLED: + self.hide_field('group') class Meta: model = AskWidget diff --git a/askbot/skins/default/templates/delete_ask_widget.html b/askbot/skins/default/templates/delete_ask_widget.html new file mode 100644 index 00000000..17f48b8c --- /dev/null +++ b/askbot/skins/default/templates/delete_ask_widget.html @@ -0,0 +1,14 @@ +{% extends "one_column_body.html" %} +<!-- create_ask_widget.html --> +{% block title %}Delete Ask Question Widget{% endblock %} +{% block content %} +<h1>Are you sure that you cant to delete this Ask Question Widget?</h1> +<br/> +<strong>Warning: This could break the widgets on sites that currently use this widget please make sure that you don't use the widget in other sites</strong> +<form action="." method="POST"> + <p><input type='submit' value='Delete' /> <a href="{% url list_ask_widgets %}">Go Back</a></p> +</form> +{% endblock %} +{% block endjs %} +{% endblock %} + diff --git a/askbot/skins/default/templates/list_ask_widget.html b/askbot/skins/default/templates/list_ask_widget.html index bda917a4..27b8a20a 100644 --- a/askbot/skins/default/templates/list_ask_widget.html +++ b/askbot/skins/default/templates/list_ask_widget.html @@ -4,11 +4,20 @@ {% block content %} <h1>Ask Question widget list</h1> -{%for widget in widgets%} - <p>Widget: {{widget.title}} (<a href="{% url edit_ask_widget widget.id %}">Edit</a>)</p> - <p>Code: <script type="text/javascript" src=http://{{request.get_host()}}{% url render_ask_widget widget.id%} ><script> -{%endfor%} - +<table border="0"> + <tr> + <th>Widget Title </th> + <th>Code</th> + <th>Actions</th> + </tr> + {%for widget in widgets%} + <tr> + <td>{{widget.title}}</td> + <td> <script type="text/javascript" src="http://{{request.get_host()}}{% url render_ask_widget widget.id%}" ></script></td> + <td><a href="{% url edit_ask_widget widget.id %}">Edit</a> | <a href="{% url delete_ask_widget widget.id %}">Delete</a></td> + </tr> + {%endfor%} +</table> {% endblock %} {% block endjs %} diff --git a/askbot/skins/default/media/style/askbot-modal.css b/askbot/skins/default/templates/widgets/askbot_widget.css index cec8bc6a..573a07fd 100755 --- a/askbot/skins/default/media/style/askbot-modal.css +++ b/askbot/skins/default/templates/widgets/askbot_widget.css @@ -1,4 +1,4 @@ -#AskbotAskWidget { +#{{variable_name}} { visibility: hidden; position: absolute; left: 0px; @@ -6,10 +6,10 @@ width:100%; height:100%; text-align:center; - z-index: 1000; + z-index: 99999; } -#AskbotAskWidget div{ +#{{variable_name}} div{ width:600px; height:400px; margin: 100px auto; @@ -19,7 +19,7 @@ text-align:right; } -#AskbotAskWidget iframe{ +#{{variable_name}} iframe{ width:600px; height:400px; } diff --git a/askbot/skins/default/templates/widgets/askbot_widget.js b/askbot/skins/default/templates/widgets/askbot_widget.js index 646d3dd5..57cead90 100755 --- a/askbot/skins/default/templates/widgets/askbot_widget.js +++ b/askbot/skins/default/templates/widgets/askbot_widget.js @@ -1,18 +1,20 @@ -var AskbotAskWidget = { - element_id: "AskbotAskWidget", +var {{variable_name}} = { + element_id: "{{variable_name}}", widgetToggle: function() { - element = document.getElementById(AskbotAskWidget.element_id); + element = document.getElementById({{variable_name}}.element_id); element.style.visibility = (element.style.visibility == "visible") ? "hidden" : "visible"; }, toHtml: function() { - var html = AskbotAskWidget.createButton(); + var html = {{variable_name}}.createButton(); var link = document.createElement('link'); link.setAttribute("rel", "stylesheet"); - link.setAttribute("href", 'http://{{host}}{{"/style/askbot-modal.css"|media}}'); + //link.setAttribute("href", 'http://{{host}}{{"/style/askbot-modal.css"|media}}'); + link.setAttribute("href", 'http://{{host}}{%url render_ask_widget_css widget.id%}'); //creating the div var motherDiv = document.createElement('div'); - motherDiv.setAttribute("id", AskbotAskWidget.element_id); + motherDiv.setAttribute("id", {{variable_name}}.element_id); + console.log(motherDiv); var containerDiv = document.createElement('div'); motherDiv.appendChild(containerDiv); @@ -26,7 +28,7 @@ var AskbotAskWidget = { var closeButton = document.createElement('a'); closeButton.setAttribute('href', '#'); closeButton.setAttribute('id', 'AskbotModalClose'); - closeButton.setAttribute('onClick', 'AskbotAskWidget.widgetToggle();'); + closeButton.setAttribute('onClick', '{{variable_name}}.widgetToggle();'); closeButton.innerText = 'Close'; containerDiv.appendChild(closeButton); @@ -38,6 +40,7 @@ var AskbotAskWidget = { var body = document.getElementsByTagName('body')[0]; if (body){ + console.log(body.firstChild); body.insertBefore(motherDiv, body.firstChild); body.insertBefore(link, body.firstChild); } @@ -48,7 +51,7 @@ var AskbotAskWidget = { buttonDiv.setAttribute('id', "AskbotAskButton"); var closeButton = document.createElement('button'); - closeButton.setAttribute('onClick', 'AskbotAskWidget.widgetToggle();'); + closeButton.setAttribute('onClick', '{{variable_name}}.widgetToggle();'); closeButton.innerText = label; buttonDiv.appendChild(closeButton); @@ -57,6 +60,14 @@ var AskbotAskWidget = { } }; +previous_function = window.onload; +var onload_functions = function(){ + if (previous_function){ + previous_function(); + } + {{variable_name}}.toHtml(); +} -window.onload = AskbotAskWidget.toHtml; -document.write(AskbotAskWidget.createButton().outerHTML); +console.log(onload_functions); +window.onload = onload_functions(); +document.write({{variable_name}}.createButton().outerHTML); diff --git a/askbot/tests/widget_tests.py b/askbot/tests/widget_tests.py index 673b17f4..0ead07f5 100644 --- a/askbot/tests/widget_tests.py +++ b/askbot/tests/widget_tests.py @@ -110,3 +110,16 @@ class WidgetCreatorViewsTests(AskbotTestCase): response = self.client.post(reverse('edit_ask_widget', args=(self.widget.id, )), post_data) self.assertEquals(response.status_code, 302) + + def test_delete_ask_widget_get(self): + self.client.login(username='user1', password='testpass') + response = self.client.get(reverse('delete_ask_widget', + args=(self.widget.id, ))) + self.assertEquals(response.status_code, 200) + self.assertTrue('widget' in response.context) + + def test_delete_ask_widget_post(self): + self.client.login(username='user1', password='testpass') + response = self.client.post(reverse('delete_ask_widget', + args=(self.widget.id, ))) + self.assertEquals(response.status_code, 302) diff --git a/askbot/urls.py b/askbot/urls.py index abadcfe9..4fe999da 100644 --- a/askbot/urls.py +++ b/askbot/urls.py @@ -407,6 +407,12 @@ urlpatterns = patterns('', name = 'render_ask_widget' ), url( + r'^widgets/ask/(?P<widget_id>\d+).css$', + views.widgets.render_ask_widget_css, + name = 'render_ask_widget_css' + ), + + url( r'^widgets/ask/complete/$', views.widgets.ask_widget_complete, name = 'ask_by_widget_complete' @@ -422,6 +428,12 @@ urlpatterns = patterns('', name = 'edit_ask_widget' ), url( + r'^widgets/ask/delete/(?P<widget_id>\d+)/$', + views.widgets.delete_ask_widget, + name = 'delete_ask_widget' + ), + + url( r'^widgets/ask/$', views.widgets.list_ask_widget, name = 'list_ask_widgets' diff --git a/askbot/views/widgets.py b/askbot/views/widgets.py index 3eb2a756..1940eedd 100644 --- a/askbot/views/widgets.py +++ b/askbot/views/widgets.py @@ -46,12 +46,25 @@ def ask_widget(request, widget_id): text = form.cleaned_data['text'] else: text = ' ' + + + if widget.group: + group_id = widget.group.id + else: + group_id = None + + if widget.tag: + tagnames = widget.tag.name + else: + tagnames = '' + data_dict = { 'title': title, 'added_at': datetime.now(), 'wiki': False, 'text': text, - 'tagnames': '', + 'tagnames': tagnames, + 'group_id': group_id, 'is_anonymous': ask_anonymously } if request.user.is_authenticated(): @@ -132,12 +145,34 @@ def edit_ask_widget(request, widget_id): data = {'form': form} return render_into_skin('ask_widget_form.html', data, request) +@decorators.admins_only +def delete_ask_widget(request, widget_id): + widget = get_object_or_404(models.AskWidget, pk=widget_id) + if request.method=="POST": + widget.delete() + return redirect('list_ask_widgets') + else: + return render_into_skin('delete_ask_widget.html', + {'widget': widget}, request) #TODO: Add cache def render_ask_widget_js(request, widget_id): - widget = get_object_or_404(models.AskWidget) + widget = get_object_or_404(models.AskWidget, pk=widget_id) + variable_name = "AskbotAskWidget%d" % widget.id content_tpl = get_template('widgets/askbot_widget.js', request) - context_dict = {'widget': widget, 'host': request.get_host()} + context_dict = {'widget': widget, + 'host': request.get_host(), + 'variable_name': variable_name} content = content_tpl.render(Context(context_dict)) - print content return HttpResponse(content, mimetype='text/javascript') + +#TODO: Add cache +def render_ask_widget_css(request, widget_id): + widget = get_object_or_404(models.AskWidget, pk=widget_id) + variable_name = "AskbotAskWidget%d" % widget.id + content_tpl = get_template('widgets/askbot_widget.css', request) + context_dict = {'widget': widget, + 'host': request.get_host(), + 'variable_name': variable_name} + content = content_tpl.render(Context(context_dict)) + return HttpResponse(content, mimetype='text/css') |