Last active
March 11, 2018 11:31
-
-
Save andytwoods/1ce47814980eaa12aa236daafef84003 to your computer and use it in GitHub Desktop.
CommandMixin (frontend buttons do stuff in CBV)
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
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) |
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
@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