# HG changeset patch # User Brian Neal # Date 1447815680 21600 # Node ID 19b86e684cc2f3da1c49f6b7a01233598a5cbe25 # Parent ec28e9d1e82a860a8382721891b3536f1329a75a WIP on news v2.0. Initial model changes and submit news functions. diff -r ec28e9d1e82a -r 19b86e684cc2 news/forms.py --- 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'] diff -r ec28e9d1e82a -r 19b86e684cc2 news/migrations/0002_auto_20151117_2029.py --- /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, + ), + ] diff -r ec28e9d1e82a -r 19b86e684cc2 news/migrations/0003_auto_20151117_2032.py --- /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, + ), + ] diff -r ec28e9d1e82a -r 19b86e684cc2 news/models.py --- 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 diff -r ec28e9d1e82a -r 19b86e684cc2 news/views.py --- 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() diff -r ec28e9d1e82a -r 19b86e684cc2 sg101/templates/forums/show_form.html --- 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 @@ - - -
{% csrf_token %} -
- Upload Photo -

- 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. -

-
- -
-
-
+{% include 'user_photos/image_forms.html' %} diff -r ec28e9d1e82a -r 19b86e684cc2 sg101/templates/news/submit_news.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. -
  • If you wish to start a discussion, please use the forums, you'll get - a much better response there.
  • For-sale or wanted-to-buy ads should go in the forums.
  • 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.
  • +
  • To add images to your news item, use the forms at the bottom of the + page.
  • Thank you for contributing content to SG101!

    {% csrf_token %} @@ -30,6 +30,9 @@  Cancel
    + + {% include 'user_photos/image_forms.html' %} + {% else %}

    Thank you for submitting a news item!

    Your news item has been submitted for review to the site staff. Your item may be edited for content, diff -r ec28e9d1e82a -r 19b86e684cc2 sg101/templates/user_photos/image_forms.html --- /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 @@ +

    + +
    {% csrf_token %} +
    + Upload Photo +

    + 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. +

    +
    + +
    +
    +