<commit_before>from cog.models import *
from django.forms import ModelForm, ModelMultipleChoiceField, NullBooleanSelect
from django.db import models
from django.contrib.admin.widgets import FilteredSelectMultiple
from django import forms
from django.forms import ModelForm, Textarea, TextInput, Select
from django.core.exceptions import ObjectDoesNotExist
from os.path import basename
import re
from cog.utils import *
from django.db.models import Q

POST_TEMPLATES = (
                   ("cog/post/page_template_sidebar_center_right.html", "Left Menu, Main Content, Right Widgets"),
                   ("cog/post/page_template_sidebar_center.html", "Left Menu, Main Content"),
                   ("cog/post/page_template_center_right.html", "Main Content, Right Widgets"),
                   ("cog/post/page_template_center.html", "Main Content Only"),
                 )


class PostForm(ModelForm):

    # extra field not present in Post model
    newtopic = forms.CharField(max_length=200, required=False)

    # override __init__ method to provide extra arguments to customize the query set
    def __init__(self, type, project, *args,**kwargs):

        super(PostForm, self ).__init__(*args,**kwargs) # populates the post

        # filter parent posts by project and type
        queryset = Q(project=project) & Q(type=type)
        # include only top-level posts
        #queryset = queryset & Q(parent=None)
        if 'instance' in kwargs:
            instance = kwargs.get('instance')
            # exclude this post itself
            queryset = queryset & ~Q(id=instance.id)
        # get list of posts that can be a new post's parent page and alphabetize. 
        self.fields['parent'].queryset =  Post.objects.filter( queryset ).order_by('title')
        self.fields['parent'].empty_label = "Top Level Page (no parent)"
        # limit topic selection to current project and post type
        self.fields['topic'].queryset = Topic.objects.filter( Q(post__project=project) & Q(post__type=type) ).distinct().order_by('name')

    # override form clean() method to execute combined validation on multiple fields
    def clean(self):

        cleaned_data = self.cleaned_data
        topic = cleaned_data.get("topic")
        newtopic = cleaned_data.get("newtopic")
        type  = cleaned_data.get("type")

        # validate URL
        # must be null for home page, not null for other pages<commit_msg>made the home page the first item in the parent page pulldown<commit_after>from cog.models import *
from django.forms import ModelForm, ModelMultipleChoiceField, NullBooleanSelect
from django.db import models
from django.contrib.admin.widgets import FilteredSelectMultiple
from django import forms
from django.forms import ModelForm, Textarea, TextInput, Select
from django.core.exceptions import ObjectDoesNotExist
from os.path import basename
import re
from cog.utils import *
from django.db.models import Q

POST_TEMPLATES = (
                   ("cog/post/page_template_sidebar_center_right.html", "Left Menu, Main Content, Right Widgets"),
                   ("cog/post/page_template_sidebar_center.html", "Left Menu, Main Content"),
                   ("cog/post/page_template_center_right.html", "Main Content, Right Widgets"),
                   ("cog/post/page_template_center.html", "Main Content Only"),
                 )


class PostForm(ModelForm):

    # extra field not present in Post model
    newtopic = forms.CharField(max_length=200, required=False)

    # override __init__ method to provide extra arguments to customize the query set
    def __init__(self, type, project, *args,**kwargs):

        super(PostForm, self ).__init__(*args,**kwargs) # populates the post

        # filter parent posts by project and type
        queryset = Q(project=project) & Q(type=type)
        # include only top-level posts
        #queryset = queryset & Q(parent=None)
        if 'instance' in kwargs:
            instance = kwargs.get('instance')
            # exclude this post itself
            queryset = queryset & ~Q(id=instance.id)
        # get list of posts that can be a new post's parent page and alphabetize. Put the home page first.
        self.fields['parent'].queryset =  Post.objects.filter( queryset ).order_by('-is_home','title')
        print self.fields['parent'].queryset

        #self.fields['parent'].queryset[0]=Q(is_home="true")
        self.fields['parent'].empty_label = "Top Level Page (no parent)"
        # limit topic selection to current project and post type
        self.fields['topic'].queryset = Topic.objects.filter( Q(post__project=project) & Q(post__type=type) ).distinct().order_by('name')

    # override form clean() method to execute combined validation on multiple fields
    def clean(self):

        cleaned_data = self.cleaned_data
        topic = cleaned_data.get("topic")
        newtopic = cleaned_data.get("newtopic")
        type  = cleaned_data.get("type")

        # validate URL
        # must be null for home page, not null for other pages
{'fraction_non_alphanumeric': 0.0954653937947494, 'fraction_numerical': 0.0013922036595067622, 'mean_word_length': 3.1493399339933994, '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': '3271336', 'n_tokens_mistral': 1378, 'n_tokens_neox': 1343, 'n_words': 426}