Add the ability to use templated messages

This commit is contained in:
2011-07-05 12:27:50 +01:00
parent faccd2478e
commit f034c85273
5 changed files with 270 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
from django.contrib import admin
from hr.models import Application, Recommendation, Audit, Blacklist, BlacklistSource, ApplicationConfig
from hr.models import Application, Recommendation, Audit, Blacklist, BlacklistSource, ApplicationConfig, TemplateMessage
class ApplicationAdmin(admin.ModelAdmin):
list_display = ('user', 'character', 'corporation', 'status', 'application_date', 'recommendations')
@@ -45,3 +45,7 @@ class ApplicationConfigAdmin(admin.ModelAdmin):
admin.site.register(ApplicationConfig, ApplicationConfigAdmin)
class TemplateMessageAdmin(admin.ModelAdmin):
list_display = ('config', 'name')
admin.site.register(TemplateMessage, TemplateMessageAdmin)

View File

@@ -5,7 +5,7 @@ from django.conf import settings
from django.forms.extras.widgets import SelectDateWidget
from hr.app_defines import *
from hr.models import Application, Audit
from hr.models import Application, Audit, TemplateMessage
from eve_api.models import EVEPlayerCharacter, EVEPlayerCorporation
@@ -62,6 +62,31 @@ def CreateApplicationForm(user):
class NoteForm(forms.ModelForm):
class Meta:
model = Audit
exclude = ('application', 'user', 'event')
class AdminNoteForm(forms.ModelForm):
template = forms.ModelChoiceField(label='Use Template', queryset=None, required=False, help_text="Use a predefined template text for this message")
def __init__(self, *args, **kwargs):
application = kwargs.pop('application')
super(AdminNoteForm, self).__init__(*args, **kwargs)
self.fields['template'].queryset = TemplateMessage.objects.filter(config=application.corporation.application_config)
self.fields['text'].required = False
def clean(self):
template = self.cleaned_data.get('template', None)
if template:
self.cleaned_data['text'] = template.render_template({'application': self.cleaned_data.get('application')})
elif not self.cleaned_data.get('text', None):
raise forms.ValidationError('You need to either select a template or fill in the message form')
return self.cleaned_data
class Meta:
model = Audit
exclude = ('application', 'user', 'event')

View File

@@ -0,0 +1,209 @@
# encoding: 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 'TemplateMessage'
db.create_table('hr_templatemessage', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('config', self.gf('django.db.models.fields.related.ForeignKey')(related_name='templates', to=orm['hr.ApplicationConfig'])),
('name', self.gf('django.db.models.fields.CharField')(max_length='200')),
('text', self.gf('django.db.models.fields.TextField')()),
))
db.send_create_signal('hr', ['TemplateMessage'])
def backwards(self, orm):
# Deleting model 'TemplateMessage'
db.delete_table('hr_templatemessage')
models = {
'auth.group': {
'Meta': {'object_name': 'Group'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
},
'auth.permission': {
'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
},
'auth.user': {
'Meta': {'object_name': 'User'},
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
},
'contenttypes.contenttype': {
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
},
'eve_api.eveplayeralliance': {
'Meta': {'ordering': "['date_founded']", 'object_name': 'EVEPlayerAlliance'},
'api_last_updated': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
'date_founded': ('django.db.models.fields.DateField', [], {'null': 'True', 'blank': 'True'}),
'executor': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['eve_api.EVEPlayerCorporation']", 'null': 'True', 'blank': 'True'}),
'group': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.Group']", 'null': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'member_count': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
'ticker': ('django.db.models.fields.CharField', [], {'max_length': '15', 'blank': 'True'})
},
'eve_api.eveplayercharacter': {
'Meta': {'object_name': 'EVEPlayerCharacter'},
'api_last_updated': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
'attrib_charisma': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
'attrib_intelligence': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
'attrib_memory': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
'attrib_perception': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
'attrib_willpower': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
'balance': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}),
'corporation': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['eve_api.EVEPlayerCorporation']", 'null': 'True', 'blank': 'True'}),
'corporation_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
'current_location_id': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
'gender': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'last_login': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
'last_logoff': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
'race': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
'roles': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'to': "orm['eve_api.EVEPlayerCharacterRole']", 'null': 'True', 'blank': 'True'}),
'security_status': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}),
'skills': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['eve_api.EVESkill']", 'through': "orm['eve_api.EVEPlayerCharacterSkill']", 'symmetrical': 'False'}),
'total_sp': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'})
},
'eve_api.eveplayercharacterrole': {
'Meta': {'object_name': 'EVEPlayerCharacterRole'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
'roleid': ('django.db.models.fields.CharField', [], {'max_length': '64'})
},
'eve_api.eveplayercharacterskill': {
'Meta': {'object_name': 'EVEPlayerCharacterSkill'},
'character': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['eve_api.EVEPlayerCharacter']"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'in_training': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
'level': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
'skill': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['eve_api.EVESkill']"}),
'skillpoints': ('django.db.models.fields.IntegerField', [], {'default': '0'})
},
'eve_api.eveplayercorporation': {
'Meta': {'object_name': 'EVEPlayerCorporation'},
'alliance': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['eve_api.EVEPlayerAlliance']", 'null': 'True', 'blank': 'True'}),
'alliance_join_date': ('django.db.models.fields.DateField', [], {'null': 'True', 'blank': 'True'}),
'api_last_updated': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
'ceo_character': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['eve_api.EVEPlayerCharacter']", 'null': 'True', 'blank': 'True'}),
'description': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
'group': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.Group']", 'null': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'logo_color1': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
'logo_color2': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
'logo_color3': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
'logo_graphic_id': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
'logo_shape1': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
'logo_shape2': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
'logo_shape3': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
'member_count': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}),
'shares': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
'tax_rate': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}),
'ticker': ('django.db.models.fields.CharField', [], {'max_length': '15', 'null': 'True', 'blank': 'True'}),
'url': ('django.db.models.fields.URLField', [], {'max_length': '200', 'null': 'True', 'blank': 'True'})
},
'eve_api.eveskill': {
'Meta': {'object_name': 'EVESkill'},
'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
'group': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['eve_api.EVESkillGroup']", 'null': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'})
},
'eve_api.eveskillgroup': {
'Meta': {'object_name': 'EVESkillGroup'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'})
},
'hr.application': {
'Meta': {'object_name': 'Application'},
'application_date': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'character': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['eve_api.EVEPlayerCharacter']"}),
'corporation': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['eve_api.EVEPlayerCorporation']"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'status': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"})
},
'hr.applicationconfig': {
'Meta': {'ordering': "['corporation']", 'object_name': 'ApplicationConfig'},
'api_required': ('django.db.models.fields.IntegerField', [], {'default': '1'}),
'api_view': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'corporation': ('django.db.models.fields.related.OneToOneField', [], {'related_name': "'application_config'", 'unique': 'True', 'to': "orm['eve_api.EVEPlayerCorporation']"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'is_accepting': ('django.db.models.fields.BooleanField', [], {'default': 'False'})
},
'hr.audit': {
'Meta': {'object_name': 'Audit'},
'application': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['hr.Application']"}),
'date': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'event': ('django.db.models.fields.IntegerField', [], {}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'text': ('django.db.models.fields.TextField', [], {}),
'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'blank': 'True'})
},
'hr.blacklist': {
'Meta': {'object_name': 'Blacklist'},
'created_by': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"}),
'created_date': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'expiry_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'level': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
'reason': ('django.db.models.fields.TextField', [], {}),
'source': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['hr.BlacklistSource']"}),
'type': ('django.db.models.fields.IntegerField', [], {}),
'value': ('django.db.models.fields.CharField', [], {'max_length': '255'})
},
'hr.blacklistsource': {
'Meta': {'object_name': 'BlacklistSource'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
'ticker': ('django.db.models.fields.CharField', [], {'max_length': '255'})
},
'hr.recommendation': {
'Meta': {'object_name': 'Recommendation'},
'application': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['hr.Application']"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'recommendation_date': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"}),
'user_character': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['eve_api.EVEPlayerCharacter']"})
},
'hr.templatemessage': {
'Meta': {'ordering': "['config', 'name']", 'object_name': 'TemplateMessage'},
'config': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'templates'", 'to': "orm['hr.ApplicationConfig']"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': "'200'"}),
'text': ('django.db.models.fields.TextField', [], {})
}
}
complete_apps = ['hr']

View File

@@ -1,6 +1,7 @@
from datetime import datetime
from django.conf import settings
from django.db import models
from django.template import Template, Context
from django.contrib.auth.models import User
from eve_api.models import EVEPlayerCharacter, EVEPlayerCorporation
from eve_api.app_defines import *
@@ -176,3 +177,22 @@ class ApplicationConfig(models.Model):
verbose_name = 'Application Config'
verbose_name_plural = 'Application Configs'
ordering = ['corporation']
class TemplateMessage(models.Model):
""" A message template for communications in applications """
config = models.ForeignKey(ApplicationConfig, related_name="templates")
name = models.CharField("Template Name", max_length="200", blank=False, null=False)
text = models.TextField("Text", blank=False, null=False)
def render_template(self, values):
return Template(self.text).render(Context(values))
def __unicode__(self):
return self.name
class Meta:
verbose_name = u'Template Message'
verbose_name_plural = u'Template Messages'
ordering = ['config', 'name']

View File

@@ -13,7 +13,7 @@ from django.conf import settings
from utils import installed
from eve_api.models import EVEAccount, EVEPlayerCorporation, EVEPlayerCharacter
from hr.forms import CreateRecommendationForm, CreateApplicationForm, NoteForm, BlacklistUserForm
from hr.forms import CreateRecommendationForm, CreateApplicationForm, NoteForm, BlacklistUserForm, AdminNoteForm
from hr.models import Recommendation, Application, Audit, Blacklist, BlacklistSource
from app_defines import *
@@ -219,18 +219,18 @@ def add_note(request, applicationid):
def add_message(request, applicationid):
""" Send a message to the end user and note it on the application """
app = get_object_or_404(Application, id=applicationid)
if check_permissions(request.user, app):
app = Application.objects.get(id=applicationid)
if request.method == 'POST':
obj = Audit(application=app, user=request.user, event=AUDIT_EVENT_MESSAGE)
form = NoteForm(request.POST, instance=obj)
form = AdminNoteForm(request.POST, instance=obj, application=app)
if form.is_valid():
obj = form.save()
if not app.user == request.user:
send_message(obj.application, 'message', note=obj.text)
return HttpResponseRedirect(reverse('hr.views.view_application', args=[applicationid]))
form = NoteForm()
form = AdminNoteForm(application=app)
return render_to_response('hr/applications/add_message.html', locals(), context_instance=RequestContext(request))
return render_to_response('hr/index.html', locals(), context_instance=RequestContext(request))
@@ -240,11 +240,11 @@ def reject_application(request, applicationid):
""" Reject the application and notify the user """
if check_permissions(request.user) == HR_ADMIN and request.user.has_perm('hr.can_accept'):
app = Application.objects.get(id=applicationid)
if request.method == 'POST':
app = Application.objects.get(id=applicationid)
if check_permissions(request.user, app) == HR_ADMIN:
obj = Audit(application=app, user=request.user, event=AUDIT_EVENT_REJECTION)
form = NoteForm(request.POST, instance=obj)
form = AdminNoteForm(request.POST, instance=obj, application=app)
if form.is_valid():
obj = form.save()
obj.application.status = APPLICATION_STATUS_REJECTED
@@ -252,7 +252,7 @@ def reject_application(request, applicationid):
send_message(obj.application, 'rejected', note=obj.text)
return HttpResponseRedirect(reverse('hr.views.view_application', args=[applicationid]))
form = NoteForm()
form = AdminNoteForm(application=app)
return render_to_response('hr/applications/reject.html', locals(), context_instance=RequestContext(request))
return render_to_response('hr/index.html', locals(), context_instance=RequestContext(request))
@@ -262,11 +262,11 @@ def accept_application(request, applicationid):
""" Accept the application and notify the user """
if check_permissions(request.user) == HR_ADMIN and request.user.has_perm('hr.can_accept'):
app = Application.objects.get(id=applicationid)
if request.method == 'POST':
app = Application.objects.get(id=applicationid)
if check_permissions(request.user, app) == HR_ADMIN:
obj = Audit(application=app, user=request.user, event=AUDIT_EVENT_ACCEPTED)
form = NoteForm(request.POST, instance=obj)
form = AdminNoteForm(request.POST, instance=obj, application=app)
if form.is_valid():
obj = form.save()
obj.application.status = APPLICATION_STATUS_ACCEPTED
@@ -274,7 +274,7 @@ def accept_application(request, applicationid):
send_message(obj.application, 'accepted', note=obj.text)
return HttpResponseRedirect(reverse('hr.views.view_application', args=[applicationid]))
form = NoteForm()
form = AdminNoteForm(application=app)
return render_to_response('hr/applications/accept.html', locals(), context_instance=RequestContext(request))
return render_to_response('hr/index.html', locals(), context_instance=RequestContext(request))