changeset 997:19b86e684cc2

WIP on news v2.0. Initial model changes and submit news functions.
author Brian Neal <bgneal@gmail.com>
date Tue, 17 Nov 2015 21:01:20 -0600
parents ec28e9d1e82a
children e2c3d7ecfa30
files news/forms.py news/migrations/0002_auto_20151117_2029.py news/migrations/0003_auto_20151117_2032.py news/models.py news/views.py sg101/templates/forums/show_form.html sg101/templates/news/submit_news.html sg101/templates/user_photos/image_forms.html
diffstat 8 files changed, 213 insertions(+), 73 deletions(-) [+]
line wrap: on
line diff
--- a/news/forms.py	Sun Nov 15 20:44:27 2015 -0600
+++ b/news/forms.py	Tue Nov 17 21:01:20 2015 -0600
@@ -5,33 +5,69 @@
 from django import forms
 from django.conf import settings
 
+from core.html import ImageCheckError
+from core.html import image_check
+from core.markup import site_markup
 from news.models import PendingStory
 
 
 class AddNewsForm(forms.ModelForm):
-   """Form for a user to submit a news story to the admins for review."""
-   title = forms.CharField(widget=forms.TextInput(attrs={'size': 52}))
-   short_text = forms.CharField(
-           label="Article text",
-           widget=forms.Textarea(
-               attrs={'rows': 60, 'cols': 80, 'style': 'height:500px'}))
+    """Form for a user to submit a news story to the admins for review."""
+    title = forms.CharField(widget=forms.TextInput(attrs={'size': 52}))
+    short_markup = forms.CharField(
+            label="Article text",
+            widget=forms.Textarea(attrs={
+                'rows': 60,
+                'cols': 80,
+                'class': 'markItUp smileyTarget',
+                'style': 'height:500px',
+            }))
 
-   class Meta:
-      model = PendingStory
-      fields = ['title', 'category', 'short_text']
+    class Meta:
+        model = PendingStory
+        fields = ['title', 'category', 'short_markup']
 
-   class Media:
-      js = settings.GPP_THIRD_PARTY_JS['tiny_mce']
+    class Media:
+        css = {
+            'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] +
+                    settings.GPP_THIRD_PARTY_CSS['jquery-ui'])
+        }
+        js = (
+            settings.GPP_THIRD_PARTY_JS['markitup'] +
+            settings.GPP_THIRD_PARTY_JS['jquery-ui'] +
+            ['js/jquery.form.min.js']
+        )
+
+    def clean_short_markup(self):
+        md = self.cleaned_data['short_markup']
+        self.html = None
+        if not md:
+            raise forms.ValidationError("Please enter some article text")
+
+        if md:
+            self.html = site_markup(md)
+            try:
+                image_check(self.html)
+            except ImageCheckError as ex:
+                raise forms.ValidationError(str(ex))
+
+        return md
+
+    def save(self, user):
+        pending_story = super(AddNewsForm, self).save(commit=False)
+        pending_story.submitter = user
+        pending_story.save(short_text=self.html)
+        return pending_story
 
 
 class SendStoryForm(forms.Form):
-   """Form for sending a news story via email to a friend."""
-   friend_name = forms.CharField(label="Friend's Name", max_length=64)
-   friend_email = forms.EmailField(label="Friend's Email")
+    """Form for sending a news story via email to a friend."""
+    friend_name = forms.CharField(label="Friend's Name", max_length=64)
+    friend_email = forms.EmailField(label="Friend's Email")
 
-   def email(self):
-      return self.cleaned_data['friend_email']
+    def email(self):
+        return self.cleaned_data['friend_email']
 
-   def name(self):
-      return self.cleaned_data['friend_name']
+    def name(self):
+        return self.cleaned_data['friend_name']
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/news/migrations/0002_auto_20151117_2029.py	Tue Nov 17 21:01:20 2015 -0600
@@ -0,0 +1,70 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('news', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.AlterModelOptions(
+            name='pendingstory',
+            options={'ordering': ['-date_submitted'], 'verbose_name_plural': 'Pending Stories'},
+        ),
+        migrations.AlterModelOptions(
+            name='story',
+            options={'ordering': ['-date_submitted'], 'verbose_name': 'news story', 'verbose_name_plural': 'news stories'},
+        ),
+        migrations.AddField(
+            model_name='pendingstory',
+            name='admin_content',
+            field=models.TextField(default=b'', blank=True),
+            preserve_default=True,
+        ),
+        migrations.AddField(
+            model_name='pendingstory',
+            name='long_markup',
+            field=models.TextField(default=b'', blank=True),
+            preserve_default=True,
+        ),
+        migrations.AddField(
+            model_name='pendingstory',
+            name='short_markup',
+            field=models.TextField(default=b''),
+            preserve_default=True,
+        ),
+        migrations.AddField(
+            model_name='pendingstory',
+            name='version',
+            field=models.SmallIntegerField(default=0),
+            preserve_default=False,
+        ),
+        migrations.AddField(
+            model_name='story',
+            name='admin_content',
+            field=models.TextField(default=b'', blank=True),
+            preserve_default=True,
+        ),
+        migrations.AddField(
+            model_name='story',
+            name='long_markup',
+            field=models.TextField(default=b'', blank=True),
+            preserve_default=True,
+        ),
+        migrations.AddField(
+            model_name='story',
+            name='short_markup',
+            field=models.TextField(default=b''),
+            preserve_default=True,
+        ),
+        migrations.AddField(
+            model_name='story',
+            name='version',
+            field=models.SmallIntegerField(default=0),
+            preserve_default=False,
+        ),
+    ]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/news/migrations/0003_auto_20151117_2032.py	Tue Nov 17 21:01:20 2015 -0600
@@ -0,0 +1,26 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('news', '0002_auto_20151117_2029'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='pendingstory',
+            name='version',
+            field=models.SmallIntegerField(default=2),
+            preserve_default=True,
+        ),
+        migrations.AlterField(
+            model_name='story',
+            name='version',
+            field=models.SmallIntegerField(default=2),
+            preserve_default=True,
+        ),
+    ]
--- a/news/models.py	Sun Nov 15 20:44:27 2015 -0600
+++ b/news/models.py	Tue Nov 17 21:01:20 2015 -0600
@@ -1,12 +1,18 @@
 """
 Models for the news application.
 """
+import datetime
 
-import datetime
 from django.db import models
 from django.contrib.auth.models import User
 from tagging.fields import TagField
 
+from core.markup import site_markup
+
+
+# News App versions
+NEWS_VERSION = 2
+
 
 class Category(models.Model):
     """News stories belong to categories"""
@@ -39,29 +45,40 @@
     update_date = models.DateTimeField(db_index=True, blank=True)
     priority = models.IntegerField(db_index=True, default=0, blank=True)
     meta_description = models.TextField(blank=True)
+    short_markup = models.TextField(default='')
+    long_markup = models.TextField(default='', blank=True)
+    admin_content = models.TextField(default='', blank=True)
+    version = models.SmallIntegerField(default=NEWS_VERSION)
 
     class Meta:
         abstract = True
 
-
-class PendingStory(StoryBase):
-    """Stories submitted by users are held pending admin approval"""
-
     def save(self, *args, **kwargs):
         if not self.pk:
-            if not self.date_submitted:
-                self.date_submitted = datetime.datetime.now()
+            self.date_submitted = datetime.datetime.now()
             self.update_date = self.date_submitted
         else:
             self.update_date = datetime.datetime.now()
 
-        super(PendingStory, self).save(*args, **kwargs)
+        self.short_text = kwargs.pop('short_text', None)
+        if self.short_text is None and self.short_markup:
+            self.short_text = site_markup(self.short_markup)
+
+        self.long_text = kwargs.pop('long_text', None)
+        if self.long_text is None and self.long_markup:
+            self.long_text = site_markup(self.long_markup)
+
+        super(StoryBase, self).save(*args, **kwargs)
+
+
+class PendingStory(StoryBase):
+    """Stories submitted by users are held pending admin approval"""
 
     def __unicode__(self):
         return self.title
 
     class Meta:
-        ordering = ('-date_submitted', )
+        ordering = ['-date_submitted']
         verbose_name_plural = 'Pending Stories'
 
 
@@ -76,19 +93,10 @@
         return self.title
 
     class Meta:
-        ordering = ('-date_submitted', )
+        ordering = ['-date_submitted']
         verbose_name = 'news story'
         verbose_name_plural = 'news stories'
 
-    def save(self, *args, **kwargs):
-        if not self.pk:
-            self.date_submitted = datetime.datetime.now()
-            self.update_date = self.date_submitted
-        else:
-            self.update_date = datetime.datetime.now()
-
-        super(Story, self).save(*args, **kwargs)
-
     def can_comment_on(self):
         now = datetime.datetime.now()
         delta = now - self.date_submitted
--- a/news/views.py	Sun Nov 15 20:44:27 2015 -0600
+++ b/news/views.py	Tue Nov 17 21:01:20 2015 -0600
@@ -142,11 +142,7 @@
     if request.method == "POST":
         add_form = AddNewsForm(request.POST)
         if add_form.is_valid():
-            pending_story = add_form.save(commit=False)
-            pending_story.submitter = request.user
-            pending_story.short_text = _clean_html(pending_story.short_text)
-            pending_story.long_text = _clean_html(pending_story.long_text)
-            pending_story.save()
+            add_form.save(request.user)
             return HttpResponseRedirect(reverse('news.views.submit_thanks'))
     else:
         add_form = AddNewsForm()
--- a/sg101/templates/forums/show_form.html	Sun Nov 15 20:44:27 2015 -0600
+++ b/sg101/templates/forums/show_form.html	Tue Nov 17 21:01:20 2015 -0600
@@ -39,32 +39,4 @@
 </fieldset>
 </form>
 
-<form id="hot-link-form" action="{% url 'user_photos-hotlink' %}" method="post"
-   enctype="multipart/form-data">{% csrf_token %}
-<fieldset>
-   <legend>Hot Link Image</legend>
-   <p>
-   To add an image already on the Internet to your post, copy &amp; paste the
-   image URL to the box below, and hit "Hot Link". An image code will be placed
-   in the post box, above.
-   </p>
-   <input type="url" name="url" /><br />
-   <input type="submit" id="hot-link-form-submit" name="submit" value="Hot Link" />
-</fieldset>
-</form>
-
-<form id="photo-upload-form" action="{% url 'user_photos-upload_ajax' %}" method="post"
-   enctype="multipart/form-data">{% csrf_token %}
-<fieldset>
-   <legend>Upload Photo</legend>
-   <p>
-   You can upload a photo directly from your computer or device with this
-   form. After the photo is uploaded it will be resized and an image code will be
-   placed in the post box, above.
-   </p>
-   <input type="file" name="image_file" /><br />
-   <input type="submit" id="photo-upload-submit" name="submit" value="Upload photo" />
-   <div style="width: 80%;margin:auto;"><div id="photo-upload-progress"
-         style="margin:auto;width:100%;"></div></div>
-</fieldset>
-</form>
+{% include 'user_photos/image_forms.html' %}
--- a/sg101/templates/news/submit_news.html	Sun Nov 15 20:44:27 2015 -0600
+++ b/sg101/templates/news/submit_news.html	Tue Nov 17 21:01:20 2015 -0600
@@ -15,12 +15,12 @@
       reports, and music reviews are good examples. Bad examples are wishing
       someone a happy birthday or submitting comments on something you saw on
       TV.</li>
-      <li>If you wish to start a discussion, please use the forums, you'll get
-      a much better response there.</li>
       <li>For-sale or wanted-to-buy ads should go in the forums.</li>
       <li>Please spend some time on your grammar and spelling. Minor issues
       will be corrected by the staff, but major problems may cause a delay in
       publishing or even cause your item to be rejected.</li>
+      <li>To add images to your news item, use the forms at the bottom of the
+      page.</li>
    </ul>
    <p>Thank you for contributing content to SG101!</p>
    <form action="." method="post">{% csrf_token %}
@@ -30,6 +30,9 @@
          &nbsp;<a href="{% url 'news-index_page' %}">Cancel</a></td></tr>
       </table>
    </form>
+
+   {% include 'user_photos/image_forms.html' %}
+
 {% else %}
    <p><strong>Thank you for submitting a news item!</strong></p>
    <p>Your news item has been submitted for review to the site staff. Your item may be edited for content,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sg101/templates/user_photos/image_forms.html	Tue Nov 17 21:01:20 2015 -0600
@@ -0,0 +1,29 @@
+<form id="hot-link-form" action="{% url 'user_photos-hotlink' %}" method="post"
+   enctype="multipart/form-data">{% csrf_token %}
+<fieldset>
+   <legend>Hot Link Image</legend>
+   <p>
+   To add an image already on the Internet to your post, copy &amp; paste the
+   image URL to the box below, and hit "Hot Link". An image code will be placed
+   in the post box, above.
+   </p>
+   <input type="url" name="url" /><br />
+   <input type="submit" id="hot-link-form-submit" name="submit" value="Hot Link" />
+</fieldset>
+</form>
+
+<form id="photo-upload-form" action="{% url 'user_photos-upload_ajax' %}" method="post"
+   enctype="multipart/form-data">{% csrf_token %}
+<fieldset>
+   <legend>Upload Photo</legend>
+   <p>
+   You can upload a photo directly from your computer or device with this
+   form. After the photo is uploaded it will be resized and an image code will be
+   placed in the post box, above.
+   </p>
+   <input type="file" name="image_file" /><br />
+   <input type="submit" id="photo-upload-submit" name="submit" value="Upload photo" />
+   <div style="width: 80%;margin:auto;"><div id="photo-upload-progress"
+         style="margin:auto;width:100%;"></div></div>
+</fieldset>
+</form>