<commit_before>""" Methods to put or edit data in the database """

import json

from app.api_data_methods import Processor

from app.misc import strng, list_data_schema, get_table_total, \
        deserialise_date, deserialise_transactions
from app.config import LIST_CATEGORIES, E_BAD_PARAMS, \
        E_BAD_FORM, E_NO_FORM, E_NO_ITEM

class UpdateData(Processor):
    """ general class to update data in database """
    def __init__(self, db, uid, form, schema, all_required=False, table=None):
        super(UpdateData, self).__init__(db, uid, form=form)

        self.form_raw = form
        self.all_required = all_required

        self.table = table

        form_valid = False

        try:
            form_valid = self.validate_form(schema)

            if form_valid is False:
                self.error = True
                self.error_text = E_BAD_FORM

        except ValueError as param_error:
            self.error = True
            self.error_text = str(param_error)

    def set_value_from_raw(self, key, raw_value, data_type):
        """ sets individual form items after validating them """
        value = None

        if raw_value is None:
            raise ValueError("Form data for %s is null" % key)

        if data_type == 'date':
            value = deserialise_date(raw_value)

            self.form['year'] = value[0]
            self.form['month'] = value[1]
            self.form['date'] = value[2]

        elif data_type == 'transactions':
            try:
                value = deserialise_transactions(raw_value)
                self.form[key] = json.dumps(value, separators=(',', ':'))
            except ValueError:
                return False

        else:
            if data_type == 'int':
                try:
                    value = int(raw_value)
                except ValueError:
                    return False

            elif data_type == 'float':
                try:
                    value = float(raw_value)
                except ValueError:
                    return False

            elif data_type == 'string':
                try:
                    value = strng(raw_value)
                except:
                    raise ValueError("Error with text value for %s" % key)

            if value is None:
                return False

            self.form[key] = value

        return True

    def validate_form(self, schema):
        """ validate form entries according to schema """
        self.form = {}

        optional_count = 0

        has_optional = False

        for (key, (data_type, optional)) in schema.items():
            if not has_optional and optional:
                has_optional = True

            if key not in self.form_raw:
                if optional and not self.all_required:
                    continue
                else:
                    raise ValueError("Form data for %s not supplied" % key)

            raw_value = str(self.form_raw[key])

            if not self.set_value_from_raw(key, raw_value, data_type):
                raise ValueError("Bad form data for %s" % key)

            if optional:
                optional_count += 1

        if has_optional and optional_count == 0:
            raise ValueError("No optional form data supplied")

        return True

    def get_new_total(self):
        """
        gets the total cost of the table after inserting a new item,
        or editing an item
        """
        return get_table_total(self.table, self.dbx, self.uid)

class UpdateOverview(UpdateData):
    """ update balance data for the overview page """
    def __init__(self, db, uid, form):
        form_schema = {
            'balance':  ('int', False),
            'year':     ('int', False),
            'month':    ('int', False)
        }

        super(UpdateOverview, self).__init__(db, uid, form, form_schema)

    def process(self):<commit_msg>Fix unicode error on backend<commit_after>""" Methods to put or edit data in the database """

import json
from app.api_data_methods import Processor

from app.misc import strng, list_data_schema, get_table_total, \
        deserialise_date, deserialise_transactions
from app.config import LIST_CATEGORIES, E_BAD_PARAMS, \
        E_BAD_FORM, E_NO_FORM, E_NO_ITEM

class UpdateData(Processor):
    """ general class to update data in database """
    def __init__(self, db, uid, form, schema, all_required=False, table=None):
        super(UpdateData, self).__init__(db, uid, form=form)

        self.form_raw = form
        self.all_required = all_required

        self.table = table

        form_valid = False

        try:
            form_valid = self.validate_form(schema)

            if form_valid is False:
                self.error = True
                self.error_text = E_BAD_FORM

        except ValueError as param_error:
            self.error = True
            self.error_text = str(param_error)

    def set_value_from_raw(self, key, raw_value, data_type):
        """ sets individual form items after validating them """
        value = None

        if raw_value is None:
            raise ValueError("Form data for %s is null" % key)

        if data_type == 'date':
            value = deserialise_date(raw_value)

            self.form['year'] = value[0]
            self.form['month'] = value[1]
            self.form['date'] = value[2]

        elif data_type == 'transactions':
            try:
                value = deserialise_transactions(raw_value)
                self.form[key] = json.dumps(value, separators=(',', ':'))
            except ValueError:
                return False

        else:
            if data_type == 'int':
                try:
                    value = int(raw_value)
                except ValueError:
                    return False

            elif data_type == 'float':
                try:
                    value = float(raw_value)
                except ValueError:
                    return False

            elif data_type == 'string':
                try:
                    value = strng(raw_value)
                except:
                    raise ValueError("Error with text value for %s" % key)

            if value is None:
                return False

            self.form[key] = value

        return True

    def validate_form(self, schema):
        """ validate form entries according to schema """
        self.form = {}

        optional_count = 0

        has_optional = False

        for (key, (data_type, optional)) in schema.items():
            if not has_optional and optional:
                has_optional = True

            if key not in self.form_raw:
                if optional and not self.all_required:
                    continue
                else:
                    raise ValueError("Form data for %s not supplied" % key)

            raw_value = self.form_raw[key]

            if not self.set_value_from_raw(key, raw_value, data_type):
                raise ValueError("Bad form data for %s" % key)

            if optional:
                optional_count += 1

        if has_optional and optional_count == 0:
            raise ValueError("No optional form data supplied")

        return True

    def get_new_total(self):
        """
        gets the total cost of the table after inserting a new item,
        or editing an item
        """
        return get_table_total(self.table, self.dbx, self.uid)

class UpdateOverview(UpdateData):
    """ update balance data for the overview page """
    def __init__(self, db, uid, form):
        form_schema = {
            'balance':  ('int', False),
            'year':     ('int', False),
            'month':    ('int', False)
        }

        super(UpdateOverview, self).__init__(db, uid, form, form_schema)

    def process(self):
{'fraction_non_alphanumeric': 0.08609865470852018, 'fraction_numerical': 0.0015374759769378604, 'mean_word_length': 1.697304768486524, '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': '6029102', 'n_tokens_mistral': 2143, 'n_tokens_neox': 2033, 'n_words': 617}