<commit_before>from django.conf import settings
from django.forms.models import model_to_dict

from allauth.account.models import EmailAddress

from djtools.fields import NOW
from djtools.utils.mail import send_mail

from datetime import datetime
import os

PROFESSIONAL_PROGRAMS = [
    "aerospaceoutreach",
    "highereducationinitiatives",
    "industryinternship",
    "nasacompetition",
    "researchinfrastructure",
    "specialinitiatives",
]

# rocket launch required files by competition. this will do until
# we change the data model to have a separate table for their files
MRL_REQUIRED_FILES = [
    "team_roster","preliminary_design_report","flight_readiness_report",
    "education_outreach","post_flight_performance_report",
    "proceeding_paper"
]
FNL_REQUIRED_FILES = [
    "team_roster","budget","flight_demo","preliminary_design_report",
    "final_motor_selection","lodging_list","critical_design_report",
    "oral_presentation","post_flight_performance_report"
]
CRL_REQUIRED_FILES = [
    "budget","flight_demo","interim_progress_report","team_roster",
    "final_design_report","education_outreach","oral_presentation",
    "post_flight_performance_report","proceeding_paper"
]

def upload_to_path(self, filename):
    """
    Generates the path as a string for file field.
    """

    ext = filename.split('.')[-1]
    # set filename as random string
    filename = '{}.{}'.format(self.get_file_name(), ext)
    path = "{}/{}/{}/".format(
        self.get_file_path(), self.get_slug(), self.user.id
    )
    return os.path.join(path, filename)


def files_status(user):

    status = True

    # check for user profile files
    data=model_to_dict(user.user_files)
    for k,v in data.items():
        if not v:
            return False

    # check for application files
    for app in user.profile.applications.all():

        data=model_to_dict(app)

        # all programs
        if not app.award_acceptance:
            return False

        # program specific
        m = app.get_content_type().model
        # professional programs
        if m in PROFESSIONAL_PROGRAMS:
            if not data["interim_report"] or not data["final_report"]:
                return False

        # rocket launch team files
        # (not very elegant but waiting on new data model)
        if m == "rocketlaunchteam":
            if app.competition == "Collegiate Rocket Competition":
                for field in CRL_REQUIRED_FILES:
                    if not getattr(app,field):
                        return False
            elif app.competition == "Midwest High Powered Rocket Competition":
                for field in MRL_REQUIRED_FILES:
                    if not getattr(app,field):
                        return False
            else:
                for field in FNL_REQUIRED_FILES:
                    if not getattr(app,field):
                        return False

    return status


def profile_status(user):
    """
    simple function that compares the user's profile updated datetime
    against the grant cycle start date, which is comprised of the
    current year and the settings value for the month and the first
    day of the month.
    """<commit_msg>wrapped model_to_dict() conversion in try/except in case there is no UserFiles() object of the user; removed interim_report and final_report from required files check<commit_after>from django.conf import settings
from django.forms.models import model_to_dict

from allauth.account.models import EmailAddress

from djtools.fields import NOW
from djtools.utils.mail import send_mail

from datetime import datetime
import os

PROFESSIONAL_PROGRAMS = [
    "aerospaceoutreach",
    "highereducationinitiatives",
    "industryinternship",
    "nasacompetition",
    "researchinfrastructure",
    "specialinitiatives",
]

# rocket launch required files by competition. this will do until
# we change the data model to have a separate table for their files
MRL_REQUIRED_FILES = [
    "team_roster","preliminary_design_report","flight_readiness_report",
    "education_outreach","post_flight_performance_report",
    "proceeding_paper"
]
FNL_REQUIRED_FILES = [
    "team_roster","budget","flight_demo","preliminary_design_report",
    "final_motor_selection","lodging_list","critical_design_report",
    "oral_presentation","post_flight_performance_report"
]
CRL_REQUIRED_FILES = [
    "budget","flight_demo","interim_progress_report","team_roster",
    "final_design_report","education_outreach","oral_presentation",
    "post_flight_performance_report","proceeding_paper"
]

def upload_to_path(self, filename):
    """
    Generates the path as a string for file field.
    """

    ext = filename.split('.')[-1]
    # set filename as random string
    filename = '{}.{}'.format(self.get_file_name(), ext)
    path = "{}/{}/{}/".format(
        self.get_file_path(), self.get_slug(), self.user.id
    )
    return os.path.join(path, filename)


def files_status(user):

    status = True

    # check for user profile files
    try:
        data=model_to_dict(user.user_files)
    except:
        # UserFiles() instance does not exist
        return False
    for k,v in data.items():
        if not v:
            return False

    # check for application files
    for app in user.profile.applications.all():

        data=model_to_dict(app)

        # all programs
        if not app.award_acceptance:
            return False

        # program specific
        m = app.get_content_type().model
        # professional programs
        if m in PROFESSIONAL_PROGRAMS:
            if not data["payment_information"]:
                return False

        # rocket launch team files
        # (not very elegant but waiting on new data model)
        if m == "rocketlaunchteam":
            if app.competition == "Collegiate Rocket Competition":
                for field in CRL_REQUIRED_FILES:
                    if not getattr(app,field):
                        return False
            elif app.competition == "Midwest High Powered Rocket Competition":
                for field in MRL_REQUIRED_FILES:
                    if not getattr(app,field):
                        return False
            else:
                for field in FNL_REQUIRED_FILES:
                    if not getattr(app,field):
                        return False

    return status


def profile_status(user):
    """
    simple function that compares the user's profile updated datetime
    against the grant cycle start date, which is comprised of the
    current year and the settings value for the month and the first
    day of the month.
    """
{'fraction_non_alphanumeric': 0.08378623188405797, 'fraction_numerical': 0.00030193236714975844, 'mean_word_length': 2.7706317586795675, 'pattern_counts': {'":': 6, '<': 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': '880458', 'n_tokens_mistral': 1922, 'n_tokens_neox': 1816, 'n_words': 522}