<commit_before>import hashlib

from django.db import models, migrations, connection
from django.db.backends.postgresql import schema
from django.apps import apps
from django.utils.encoding import force_bytes

from django.db.utils import ProgrammingError

from aldjemy.core import get_meta
from aldjemy.orm import get_session

from sqlalchemy import Table
from sqlalchemy_views import CreateView, DropView


class ViewOptions(object):
    from_models = []
    refresh_automatically = True

    def __init__(self, opts):
        if opts:
            for key, value in opts.__dict__.items():
                if key in ['from_models', 'refresh_automatically']:
                    setattr(self, key, value)


class ViewBase(type(models.Model)):
    view_models = []
    operations = []

    def __new__(mcs, name, bases, attrs):
        new = super(ViewBase, mcs).__new__(mcs, name, bases, attrs)

        opts = attrs.pop('ViewMeta', None)
        setattr(new, '_view_meta', ViewOptions(opts))
        new._meta.managed = False
        return new

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if not self._meta.abstract:
            self._add_view_model()

    def _add_view_model(self):
        if self not in ViewBase.view_models:
            ViewBase.view_models.append(self)

    def _drop_table_sql(self):
        return "DROP TABLE IF EXISTS %s CASCADE" % self._meta.db_table

    def _drop_view_sql(self):
        return "DROP VIEW IF EXISTS %s CASCADE" % self._meta.db_table

    def _create_view_sql(self):
        sql = self.view()

        if isinstance(sql, str):
            return sql

        return CreateView(
            Table(self._meta.db_table, get_meta()),
            sql.subquery(),
            or_replace=True)

    def _from_tables(self):
        if hasattr(self._view_meta, "from_tables"):
            return self._view_meta.from_tables
<commit_msg>Fix view SQL projection to Django model fields mismatch bug.<commit_after>import hashlib

from django.db import models, migrations, connection
from django.db.backends.postgresql import schema
from django.apps import apps
from django.utils.encoding import force_bytes

from django.db.utils import ProgrammingError

from aldjemy.orm import get_session

from sqlalchemy import Table, MetaData
from sqlalchemy_views import CreateView, DropView


class ViewOptions(object):
    from_models = []
    refresh_automatically = True

    def __init__(self, opts):
        if opts:
            for key, value in opts.__dict__.items():
                if key in ['from_models', 'refresh_automatically']:
                    setattr(self, key, value)


class ViewBase(type(models.Model)):
    view_models = []
    operations = []

    def __new__(mcs, name, bases, attrs):
        new = super(ViewBase, mcs).__new__(mcs, name, bases, attrs)

        opts = attrs.pop('ViewMeta', None)
        setattr(new, '_view_meta', ViewOptions(opts))
        new._meta.managed = False
        return new

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if not self._meta.abstract:
            self._add_view_model()

    def _add_view_model(self):
        if self not in ViewBase.view_models:
            ViewBase.view_models.append(self)

    def _drop_table_sql(self):
        return "DROP TABLE IF EXISTS %s CASCADE" % self._meta.db_table

    def _drop_view_sql(self):
        return "DROP VIEW IF EXISTS %s CASCADE" % self._meta.db_table

    def _create_view_sql(self):
        sql = self.view()

        if isinstance(sql, str):
            return sql

        return CreateView(
            Table(self._meta.db_table, MetaData()),
            self.view().subquery(),
            or_replace=True)

    def _from_tables(self):
        if hasattr(self._view_meta, "from_tables"):
            return self._view_meta.from_tables

{'fraction_non_alphanumeric': 0.09459808736107521, 'fraction_numerical': 0.0, 'mean_word_length': 2.714011516314779, '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': '5360258', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1138, 'n_words': 304}