<commit_before>from django.http import HttpResponseRedirect, HttpResponseBadRequest, HttpResponseNotAllowed

from django.views.generic import FormView

from open_municipio.monitoring.forms import MonitoringForm


class MonitoringToggleBaseView(FormView):
    form_class = MonitoringForm
    
    def form_invalid(self, form):
        msg = "It appears that the monitoring form has been tampered with !"
        return HttpResponseBadRequest(msg)
    
    def get_success_url(self):
        # FIXME: redirects shouldn't rely on the ``Referer`` header,
        # since it might be missing from the HTTP request,
        # depending on client's configuration.  
        return self.request.META['HTTP_REFERER']
    
    def get(self, *args, **kwargs):
        msg = "This view can be accessed only via POST"
        return HttpResponseNotAllowed(msg)
        

class MonitoringStartView(MonitoringToggleBaseView):
    """
    Start the monitoring process of a content object by a given user.
    
    Monitoring user and target content object are taken from POST data.
    
    Redirect to referrer URL after processing.
    """
    def form_valid(self, form):
        form.save()
        return HttpResponseRedirect(self.get_success_url())<commit_msg>Views for starting/stopping a monitoring process should require user login.<commit_after>from django.http import HttpResponseRedirect, HttpResponseBadRequest, HttpResponseNotAllowed
from django.views.generic import FormView
from django.utils.decorators import method_decorator

from django.contrib.auth.decorators import login_required

from open_municipio.monitoring.forms import MonitoringForm


class MonitoringToggleBaseView(FormView):
    form_class = MonitoringForm
    
    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(MonitoringToggleBaseView, self).dispatch(*args, **kwargs)
       
    def form_invalid(self, form):
        msg = "It appears that the monitoring form has been tampered with !"
        return HttpResponseBadRequest(msg)
    
    def get_success_url(self):
        # FIXME: redirects shouldn't rely on the ``Referer`` header,
        # since it might be missing from the HTTP request,
        # depending on client's configuration.  
        return self.request.META['HTTP_REFERER']
    
    def get(self, *args, **kwargs):
        msg = "This view can be accessed only via POST"
        return HttpResponseNotAllowed(msg)
        

class MonitoringStartView(MonitoringToggleBaseView):
    """
    Start the monitoring process of a content object by a given user.
    
    Monitoring user and target content object are taken from POST data.
    
    Redirect to referrer URL after processing.
    """
    def form_valid(self, form):
        form.save()
        return HttpResponseRedirect(self.get_success_url())
{'fraction_non_alphanumeric': 0.06838209376101516, 'fraction_numerical': 0.0, 'mean_word_length': 3.4, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3727078', 'n_tokens_mistral': 749, 'n_tokens_neox': 730, 'n_words': 247}