<commit_before>"""
Provides superclass for GenericEditExisting, GenericEditNew, and GenericDelete
"""

import flask
from google.appengine.ext import ndb
from wtforms_appengine.ndb import model_form

from .BetterModelConverter import BetterModelConverter
from .GenericBase import GenericBase


class GenericEditBase(GenericBase):
    template = 'generic-editor.html'

    def get_form(self):
        assert issubclass(self.model, ndb.Model)

        return model_form(self.model, exclude=self.form_exclude, only=self.form_include, base_class=SeaSurfForm, converter=BetterModelConverter(),
                          field_args=self.wtforms_field_args)

    def _pre_put_hook(self, obj):
        pass

    def _post_pook_hook(self, obj):
        pass

    def handle(self, urlsafe=None):
        FormClass = self.get_form()
        form, is_new, obj = self.handle_url(FormClass, urlsafe)

        if form.validate_on_submit():
            if obj is None:
                obj = self.model()
            form.populate_obj(obj)
            self._pre_put_hook(obj)
            obj.put()
            self._post_pook_hook(obj)
            self.flash_message(obj)
            if self.retrieve_view:
                return flask.redirect(flask.url_for(self.retrieve_view, urlsafe=obj.key.urlsafe()))
            else:
                return self.redirect_after_completion()

        context = {
            self.variable_form: form,
            'is_new': is_new,
            'current_object': obj
        }
<commit_msg>Set current object in generic view.<commit_after>"""
Provides superclass for GenericEditExisting, GenericEditNew, and GenericDelete
"""

import flask
from google.appengine.ext import ndb
from wtforms_appengine.ndb import model_form

from .BetterModelConverter import BetterModelConverter
from .GenericBase import GenericBase


class GenericEditBase(GenericBase):
    template = 'generic-editor.html'

    def get_form(self):
        assert issubclass(self.model, ndb.Model)

        return model_form(self.model, exclude=self.form_exclude, only=self.form_include, base_class=SeaSurfForm, converter=BetterModelConverter(),
                          field_args=self.wtforms_field_args)

    def _pre_put_hook(self, obj):
        pass

    def _post_pook_hook(self, obj):
        pass

    def handle(self, urlsafe=None):
        FormClass = self.get_form()
        form, is_new, obj = self.handle_url(FormClass, urlsafe)
        flask.g.current_object = obj

        if form.validate_on_submit():
            if obj is None:
                obj = self.model()
            form.populate_obj(obj)
            self._pre_put_hook(obj)
            obj.put()
            self._post_pook_hook(obj)
            self.flash_message(obj)
            if self.retrieve_view:
                return flask.redirect(flask.url_for(self.retrieve_view, urlsafe=obj.key.urlsafe()))
            else:
                return self.redirect_after_completion()

        context = {
            self.variable_form: form,
            'is_new': is_new,
            'current_object': obj
        }

{'fraction_non_alphanumeric': 0.08585365853658537, 'fraction_numerical': 0.0, 'mean_word_length': 2.4561797752808987, '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': '1760104', 'n_tokens_mistral': 924, 'n_tokens_neox': 891, 'n_words': 183}