Skip to content

Instantly share code, notes, and snippets.

@andytwoods
Last active March 11, 2018 11:31
Show Gist options
  • Save andytwoods/1ce47814980eaa12aa236daafef84003 to your computer and use it in GitHub Desktop.
Save andytwoods/1ce47814980eaa12aa236daafef84003 to your computer and use it in GitHub Desktop.
CommandMixin (frontend buttons do stuff in CBV)
//give the buttons the 'run-command' class. Specify the command they run via data-command parameter. If you want to send data to the
//backend, suffix your command with () and add a js function with the same name as your commmand somewhere (that returns the data for
//backend. E.g. data-command='hello()' and function hello(){return 'hi!'}
<div class="col-md">
<a class="btn btn-sm run-command" data-command="worker_job_complete" id="worker_job_complete_button" role="button">
ongoing</a>
<a class="btn btn-sm run-command" data-command="requester_job_complete" id='employer_job_complete_button' role="button">
ongoing</a>
<a class="btn btn-sm btn-danger run-command" role="button" data-command="lodge_dispute">
<i class="fas fa-question-circle"></i> Dispute?</a>
</div>
{% block javascript %}
{{ block.super }}
<script>
function url_params(){
var search = location.search.substring(1)
if(search.length === 0) return {}
return JSON.parse('{"' + decodeURI(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}')
}
function add_params_url(params, url){
for (var key in params) {
if (!url) {
url = window.location.href.split('?')[0] + "?"
}
else {
url += "&"
}
url += key + "=" + params[key]
}
return url
}
(function(){
$('.run-command').click(function(){
event.preventDefault();
var url = $(this).attr('href');
var command = $(this).attr('data-command');
var params = url_params()
if(command){
if(command.indexOf("()")!==-1){
var f = command.split('()')[0]
if(!window[f]){
throw('devel err: said use a function to send data via button with are-you-sure' +
' but no function: '+f)
}
var result = window[f]()
if(result.length===0)return
params['data'] = window[f]()
params['command'] = f
}
else params['command'] = command
}
url = add_params_url(params, url)
are_you_sure_callback(function(){
location.href = url
})
})
}())
</script>
{% endblock %}
from django.http import HttpResponseRedirect
class UnknownCommandError(BaseException):
pass
class CommandMixin(object):
def run_command(self, command, data):
raise UnknownCommandError('command: '+command)
def check_command(self, request):
if 'command' in request.GET:
raw_qs = request.META.get('QUERY_STRING', '')
url_params = {}
if raw_qs:
for line in raw_qs.split("&"):
key, arg = line.split("=")
url_params[key] = arg
command = url_params.pop('command')
data = url_params.pop('data', None)
url = request.META.get('HTTP_REFERER') + '&'.join([k + '=' + p for k, p in url_params.items()])
self.run_command(command, data)
# lets refresh the page frontend, but remove the command parameters added to the url
return HttpResponseRedirect(url)
def get(self, request, *args, **kwargs):
response = self.check_command(request)
if response:
return response
return super(CommandMixin, self).get(request, *args, **kwargs)
@method_decorator(login_required, name='dispatch')
class AbstractManageJobView(CommandMixin, DetailView):
model = Application
template_name = 'jobsboard/manage_job.html'
def run_command(self, command, data):
if command == 'worker_job_complete':
print('worker_job_complete')
pass
elif command == 'requester_job_complete':
print('requester_job_complete')
pass
else:
return super(AbstractManageJobView, self).run_command(command, data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment