-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fixes #10] Feature: bulk creation events
- Loading branch information
1 parent
22915ff
commit 912a63c
Showing
6 changed files
with
157 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{% include 'header.html' %} | ||
|
||
|
||
{% include 'top_nav_bar.html' %} | ||
|
||
<div class="container-fluid"> | ||
{% include 'noscript.html' %} | ||
<div id="content" class="span12"> | ||
<!-- content starts --> | ||
{% if not errors %} | ||
<div class="row-fluid sortable"> | ||
<div class="box span12"> | ||
<div class="box-header well"> | ||
<h2><i class="icon-plus"></i>{{ box_title }}</h2> | ||
</div> | ||
|
||
<div class="box-content"> | ||
<form class="form-horizontal" method="POST" accept-charset="." {% if form_action %} action="{{ form_action }}"{% endif %}> | ||
{% csrf_token %} | ||
{{ formset.management_form }} | ||
{% for form in formset %} | ||
<p class="badge badge-success">Event {{ forloop.counter }}</p> | ||
<table class="table table-bordered table-striped table-condensed table-fixed-header table-hover"> | ||
{{ form }} | ||
</table> | ||
|
||
{% endfor %} | ||
<div> | ||
<button type="submit" class="btn btn-primary btn-small">Save Button</button> | ||
<button type="button" class="btn btn-small" onclick="javascript:history.go(-1)">Cancel</button> | ||
</div> | ||
</form> | ||
|
||
</div> | ||
</div> | ||
</div> | ||
{% elif errors %} | ||
{# <div class="btn-group span12">#} | ||
<div class="alert alert-error span12"> | ||
<ul> | ||
{% for error in errors %} | ||
<li>{{ error }}</li> | ||
{% endfor %} | ||
</ul> | ||
</div> | ||
<button type="button" class="btn btn-danger" onclick="javascript:history.go(-1)">Go Back</button> | ||
{# </div>#} | ||
{% endif %} | ||
</div> | ||
</div> | ||
{% include 'footer.html' %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from django import forms | ||
|
||
|
||
class BulkUploadForm(forms.Form): | ||
|
||
list_of_items = forms.CharField(widget=forms.Textarea, label='Paste items here') | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(BulkUploadForm, self).__init__(*args, **kwargs) | ||
self.fields['list_of_items'].widget.attrs['rows'] = "15" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
"""Copyright 2014 Cyrus Dasadia | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
""" | ||
|
||
from django.shortcuts import get_object_or_404, redirect, render_to_response | ||
from django.forms.formsets import formset_factory | ||
from django.forms import ValidationError | ||
from django.contrib.auth.decorators import login_required | ||
from django.template import RequestContext | ||
from cito_engine.forms import tools_form, events | ||
|
||
|
||
@login_required(login_url='/login/') | ||
def bulk_upload_events(request): | ||
BulkEventsFormset = formset_factory(events.EventForm, extra=0) | ||
render_vars = dict() | ||
if request.method != 'POST': | ||
return redirect('/incidents/') | ||
else: | ||
print request.POST.keys() | ||
# list_of_events = request.POST.get('list_of_items') | ||
# If we got a bunch of lines for events | ||
if 'list_of_items' in request.POST: | ||
list_of_events = request.POST.get('list_of_items') | ||
initial_data = [] | ||
if list_of_events: | ||
for event_summary in list_of_events.splitlines(): | ||
initial_data.append({'summary': event_summary, | ||
'description': event_summary, | ||
'severity': 'S3', | ||
'team': request.user.team_set.all()[0], | ||
}) | ||
render_vars['formset'] = BulkEventsFormset(initial=initial_data) | ||
else: | ||
render_vars['errors'] = ['List was empty!'] | ||
|
||
# We got the formset | ||
elif 'form-INITIAL_FORMS' in request.POST: | ||
render_vars['formset'] = BulkEventsFormset(request.POST) | ||
user_teams = request.user.team_set.all() | ||
if render_vars['formset'].is_valid(): | ||
for form in render_vars['formset']: | ||
if form.cleaned_data.get('team') not in user_teams: | ||
render_vars['errors'] = ['Cannot add event for %s, you are not a member of this team.' % | ||
form.cleaned_data.get('team')] | ||
return render_to_response('bulk_upload.html', render_vars, | ||
context_instance=RequestContext(request)) | ||
else: | ||
form.save() | ||
return redirect('/events/') | ||
return render_to_response('bulk_upload.html', render_vars, context_instance=RequestContext(request)) | ||
|
||
|
||
|
||
@login_required(login_url='/login/') | ||
def show_bulk_upload_form(request, upload_item): | ||
form = tools_form.BulkUploadForm() | ||
render_vars = dict() | ||
render_vars['form'] = form | ||
if upload_item == 'events': | ||
render_vars['form_action'] = '/tools/bulkupload/events/confirm/' | ||
return render_to_response('generic_form.html', render_vars, context_instance=RequestContext(request)) |