1. Konrad Rymczak
  2. cmsplugin-contact

Commits

Konrad Rymczak  committed 2bdc15d

Extra fields (model, form, template), migration
Contact data (access from admin)
Store message in database

Comments (0)

Files changed (8)

File .hgignore Added

View file
  • Ignore whitespace
  • Hide word diff
+syntax: glob
+.idea/*
+*.pyc
+*.egg-info/*

File cmsplugin_contact/admin.py Modified

View file
  • Ignore whitespace
  • Hide word diff
+# -*- coding: utf-8 -*-
+
 from django.forms import ModelForm, Field, CharField, HiddenInput
 from django.forms.util import ErrorList
 from django.core.exceptions import ValidationError
 from django.contrib.sites.models import Site
 from django.conf import settings
 from django.utils.translation import ugettext_lazy as _
+from django.contrib import admin
+from models import Contact, ContactData
 
-from models import Contact
+class ContactDataAdmin(admin.ModelAdmin):
+    list_display = ('username', 'email', 'phone', 'content', 'created')
+    date_hierarchy = 'created'
+    search_fields = ('username', 'email', 'phone', 'content', 'created')
+
+admin.site.register(ContactData, ContactDataAdmin)
 
 class KeyField(CharField):
     def validate(self, value):
         # valdates always. validation is done in the form (see below)
         pass
 
+
+
 class ContactAdminForm(ModelForm):
     akismet_api_key = KeyField(max_length=255, label=_("Akismet API Key"), help_text=_('Get a Wordpress Key from http://akismet.com/'))
 

File cmsplugin_contact/cms_plugins.py Modified

View file
  • Ignore whitespace
  • Hide word diff
 from cms.plugin_pool import plugin_pool
 from cms.plugins.text.settings import USE_TINYMCE
 from cms.plugins.text.widgets.wymeditor_widget import WYMEditor
-from models import Contact
+from models import Contact, ContactData
 from forms import ContactForm, AkismetContactForm, RecaptchaContactForm, HoneyPotContactForm
 from admin import ContactAdminForm
 
+
 class ContactPlugin(CMSPluginBase):
     model = Contact
     name = _("Contact Form")
     
     fieldsets = (
         (None, {
-            'fields': ('site_email', 'email_label', 'subject_label', 'content_label', 'thanks', 'submit'),
+            'fields': (
+                'site_email',
+                'email_label',
+                'subject_label',
+                'content_label',
+                'phone_label',
+                'username_label',
+                'extra_info_content',
+                'thanks',
+                'submit'),
         }),
         (_('Spam Protection'), {
             'fields': ('spam_protection_method', 'akismet_api_key', 'recaptcha_public_key', 'recaptcha_private_key', 'recaptcha_theme')
 
 
     def send(self, form, site_email, attachments=None):
-        subject = form.cleaned_data['subject']
+        cd = form.cleaned_data
+        subject = cd['subject']
+
+        contact_data = ContactData()
+        contact_data.content = cd['content']
+        contact_data.subject = cd['subject']
+        contact_data.username = cd['username']
+        contact_data.phone = cd['phone']
+        contact_data.email = cd['email']
+        contact_data.save()
+
         if not subject:
             subject = _('No subject')
         email_message = EmailMessage(
                 'subject': subject,
             }).splitlines()[0],
             render_to_string(self.email_template, {
-                'data': form.cleaned_data,
+                'data': cd,
             }),
-            getattr(settings, 'DEFAULT_FROM_EMAIL', form.cleaned_data['email']),
+            getattr(settings, 'DEFAULT_FROM_EMAIL', cd['email']),
             [site_email],
             headers = {
-                'Reply-To': form.cleaned_data['email']
+                'Reply-To': cd['email']
             },)
         if attachments:
             for var_name, data in attachments.iteritems():

File cmsplugin_contact/forms.py Modified

View file
  • Ignore whitespace
  • Hide word diff
   
 class ContactForm(forms.Form):
     email = forms.EmailField()
+    username = forms.CharField(required=False)
+    phone = forms.CharField(required=False)
     subject = forms.CharField(required=False)
     content = forms.CharField(widget=forms.Textarea())
 

File cmsplugin_contact/migrations/0003_auto__add_contactdata__add_field_contact_username_label__add_field_con.py Added

View file
  • Ignore whitespace
  • Hide word diff
+# -*- coding: utf-8 -*-
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+
+class Migration(SchemaMigration):
+
+    def forwards(self, orm):
+        # Adding model 'ContactData'
+        db.create_table('cmsplugin_contact_contactdata', (
+            ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+            ('username', self.gf('django.db.models.fields.CharField')(max_length=128)),
+            ('email', self.gf('django.db.models.fields.EmailField')(max_length=75)),
+            ('phone', self.gf('django.db.models.fields.CharField')(max_length=30, blank=True)),
+            ('subject', self.gf('django.db.models.fields.TextField')(blank=True)),
+            ('content', self.gf('django.db.models.fields.TextField')(blank=True)),
+            ('created', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime.now, db_index=True)),
+        ))
+        db.send_create_signal('cmsplugin_contact', ['ContactData'])
+
+        # Adding field 'Contact.username_label'
+        db.add_column('cmsplugin_contact', 'username_label',
+                      self.gf('django.db.models.fields.CharField')(default=u'Your username', max_length=100),
+                      keep_default=False)
+
+        # Adding field 'Contact.phone_label'
+        db.add_column('cmsplugin_contact', 'phone_label',
+                      self.gf('django.db.models.fields.CharField')(default=u'Your phone', max_length=100),
+                      keep_default=False)
+
+        # Adding field 'Contact.extra_info_content'
+        db.add_column('cmsplugin_contact', 'extra_info_content',
+                      self.gf('django.db.models.fields.TextField')(default='', max_length=400),
+                      keep_default=False)
+
+
+    def backwards(self, orm):
+        # Deleting model 'ContactData'
+        db.delete_table('cmsplugin_contact_contactdata')
+
+        # Deleting field 'Contact.username_label'
+        db.delete_column('cmsplugin_contact', 'username_label')
+
+        # Deleting field 'Contact.phone_label'
+        db.delete_column('cmsplugin_contact', 'phone_label')
+
+        # Deleting field 'Contact.extra_info_content'
+        db.delete_column('cmsplugin_contact', 'extra_info_content')
+
+
+    models = {
+        'cms.cmsplugin': {
+            'Meta': {'object_name': 'CMSPlugin'},
+            'creation_date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'language': ('django.db.models.fields.CharField', [], {'max_length': '15', 'db_index': 'True'}),
+            'level': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+            'lft': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+            'parent': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cms.CMSPlugin']", 'null': 'True', 'blank': 'True'}),
+            'placeholder': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cms.Placeholder']", 'null': 'True'}),
+            'plugin_type': ('django.db.models.fields.CharField', [], {'max_length': '50', 'db_index': 'True'}),
+            'position': ('django.db.models.fields.PositiveSmallIntegerField', [], {'null': 'True', 'blank': 'True'}),
+            'rght': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+            'tree_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'})
+        },
+        'cms.placeholder': {
+            'Meta': {'object_name': 'Placeholder'},
+            'default_width': ('django.db.models.fields.PositiveSmallIntegerField', [], {'null': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'slot': ('django.db.models.fields.CharField', [], {'max_length': '50', 'db_index': 'True'})
+        },
+        'cmsplugin_contact.contact': {
+            'Meta': {'object_name': 'Contact', 'db_table': "'cmsplugin_contact'"},
+            'akismet_api_key': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
+            'cmsplugin_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['cms.CMSPlugin']", 'unique': 'True', 'primary_key': 'True'}),
+            'content_label': ('django.db.models.fields.CharField', [], {'default': "u'Message'", 'max_length': '100'}),
+            'email_label': ('django.db.models.fields.CharField', [], {'default': "u'Your email address'", 'max_length': '100'}),
+            'extra_info_content': ('django.db.models.fields.TextField', [], {'default': "''", 'max_length': '400'}),
+            'phone_label': ('django.db.models.fields.CharField', [], {'default': "u'Your phone'", 'max_length': '100'}),
+            'recaptcha_private_key': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
+            'recaptcha_public_key': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
+            'recaptcha_theme': ('django.db.models.fields.CharField', [], {'default': "'clean'", 'max_length': '20'}),
+            'site_email': ('django.db.models.fields.EmailField', [], {'max_length': '75'}),
+            'spam_protection_method': ('django.db.models.fields.SmallIntegerField', [], {'default': '0'}),
+            'subject_label': ('django.db.models.fields.CharField', [], {'default': "u'Subject'", 'max_length': '200'}),
+            'submit': ('django.db.models.fields.CharField', [], {'default': "u'Submit'", 'max_length': '30'}),
+            'thanks': ('django.db.models.fields.TextField', [], {'default': "u'Thank you for your message.'", 'max_length': '200'}),
+            'username_label': ('django.db.models.fields.CharField', [], {'default': "u'Your username'", 'max_length': '100'})
+        },
+        'cmsplugin_contact.contactdata': {
+            'Meta': {'object_name': 'ContactData'},
+            'content': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+            'created': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now', 'db_index': 'True'}),
+            'email': ('django.db.models.fields.EmailField', [], {'max_length': '75'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'phone': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+            'subject': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+            'username': ('django.db.models.fields.CharField', [], {'max_length': '128'})
+        }
+    }
+
+    complete_apps = ['cmsplugin_contact']

File cmsplugin_contact/models.py Modified

View file
  • Ignore whitespace
  • Hide word diff
 from django.db import models
 from django.utils.translation import ugettext_lazy as _
 from cms.models import CMSPlugin
+from django.db import models
+import datetime
+
+class ContactData(models.Model):
+    username = models.CharField(_(u'Forename Name / Company'), max_length=128, blank=False)
+    email = models.EmailField(_(u'Email'), blank=False)
+    phone = models.CharField(_(u'Phone'), max_length=30, blank=True)
+    subject = models.TextField(_(u'Message'), blank=True)
+    content = models.TextField(_(u'Message'), blank=True)
+    created = models.DateTimeField(_('Created'), default=datetime.datetime.now, db_index=True)
 
 # Feel free to extend this class instead of Contact.
 class BaseContact(CMSPlugin):
     )
     
     site_email = models.EmailField(_('Email recipient'))
+    username_label = models.CharField(_('Username sender label'),
+                                   default=_('Your username'),
+                                   max_length=100)
     email_label = models.CharField(_('Email sender label'),
                                    default=_('Your email address'),
                                    max_length=100)
+    phone_label = models.CharField(_('Phone sender label'),
+                                   default=_('Your phone'),
+                                   max_length=100)
     subject_label = models.CharField(_('Subject label'),
                                      default=_('Subject'), max_length=200)
     content_label = models.CharField(_('Message content label'),
                                      default=_('Message'), max_length=100)
+    extra_info_content = models.TextField(verbose_name=_("Extra info"),
+        help_text=_('Message displayed before buttons'),
+        default='', max_length=400)
     thanks = models.TextField(
         verbose_name=_("Thanks message"),
         help_text=_('Message displayed on successful submit'),

File cmsplugin_contact/templates/cmsplugin_contact/contact.html Modified

View file
  • Ignore whitespace
  • Hide word diff
 
 {% if form %}
 	<form action="." method="post">
+		<p{% if form.username.errors %} class="error"{% endif %}>
+			<label class="contact_field">{{ contact.username_label }}</label>
+				{{ form.username }}
+			{% if form.username.errors %}
+				{% for error in form.username.errors %}
+					<span class="error_msg">{{ error }}</span>
+				{% endfor %}
+			{% endif %}
+		</p>
+		<p{% if form.phone.errors %} class="error"{% endif %}>
+			<label class="contact_field">{{ contact.phone_label }}</label>
+				{{ form.phone }}
+			{% if form.phone.errors %}
+				{% for error in form.phone.errors %}
+					<span class="error_msg">{{ error }}</span>
+				{% endfor %}
+			{% endif %}
+		</p>
 		<p{% if form.email.errors %} class="error"{% endif %}>
 			<label class="contact_field">{{ contact.email_label }}</label>
 				{{ form.email }}
 		{% if form.accept_terms %}
 			{{ form.accept_terms }}
 		{% endif %}
+        {% if contact.extra_info_content %}
+            <p>{{ contact.extra_info_content }}</p>
+        {% endif %}
 		<p>
 			<input type="submit"{% if contact.submit %} value="{{ contact.submit }}"{% endif %} />
 		</p>

File cmsplugin_contact/templates/cmsplugin_contact/subject.txt Modified

View file
  • Ignore whitespace
  • Hide word diff
-[Contactform] {{ subject }}
+[ContactForm] {{ subject }}