<commit_before>#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4

from models import *
from rapidsms.message import StatusCodes
from apps.reporters.models import *
from apps.form.formslogic import FormsLogic

class NigeriaFormsLogic(FormsLogic):
    ''' This class will hold the nigeria-specific forms logic.
        I'm not sure whether this will be the right structure
        this was just for getting something hooked up '''
    
    # this is a simple structure we use to describe the forms.  
    # maps token names to db names
    _form_lookups = {
                     "nets" : {
                               "class" : NetDistribution,
                               "display" : "nets",
                               "fields" : {
                                           "location" : "location", 
                                           "distributed" : "distributed", 
                                           "expected" : "expected", 
                                           "actual" : "actual",
                                           "discrepancy" : "discrepancy", 
                                           }
                               },
                     "netcards" : {"class" : CardDistribution, 
                              "display" : "net cards",
                              "fields" : {
                                          "location" : "location", 
                                          "settlements" : "settlements", 
                                          "people" : "people", 
                                          "issued" : "distributed",
                                          }
                              }
                     }
        
    _foreign_key_lookups = {"Location" : "code" }

    def validate(self, *args, **kwargs):
        message = args[0]
        form_entry = args[1]
        # in case we need help, build a valid reminder string
        # TODO put this in the db!
        required = ["location", "role", "firstname"]
        help = ("%s register " % form_entry.domain.code.abbreviation.lower()) +\
                " ".join(["<%s>" % t for t in required])
        if form_entry.form.code.abbreviation == "register":
            data = form_entry.to_dict()
            print "\n\n%r\n\n" % data

            # check that ALL FIELDS were provided
            missing = [t for t in required if data[t] is None]
            
            # missing fields! collate them, and
            # send back a friendly non-localized
            # error message, then abort
            if missing:
                mis_str = ", ".join(missing)
                return ["Missing fields: %s" % mis_str, help]
            
            # parse the name via Reporter
            flat_name = data.pop("firstname") + " " + data.pop("lastname") + " " + data.pop("othername")
            data["alias"], data["first_name"], data["last_name"] =\
                Reporter.parse_name(flat_name.strip())
            
            # all fields were present and correct, so copy them into the
            # form_entry, for "actions" to pick up again without re-fetching
            form_entry.rep_data = data
            
            # nothing went wrong. the data structure
            # is ready to spawn a Reporter object
            return None
        elif form_entry.form.code.abbreviation in self._form_lookups.keys():
            # we know all the fields in this form are required, so make sure they're set
            # TODO check the token's required flag
            required_token_names = self._form_lookups[form_entry.form.code.abbreviation]["fields"].keys()
            for token in form_entry.tokenentry_set.all():
                if token.token.abbreviation in required_token_names:
                    # found it, as long as the data isn't empty remove it
                    if token.data:
                        required_token_names.remove(token.token.abbreviation)
            if required_token_names:<commit_msg>small renaming of name tokens<commit_after>#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4

from models import *
from rapidsms.message import StatusCodes
from apps.reporters.models import *
from apps.form.formslogic import FormsLogic

class NigeriaFormsLogic(FormsLogic):
    ''' This class will hold the nigeria-specific forms logic.
        I'm not sure whether this will be the right structure
        this was just for getting something hooked up '''
    
    # this is a simple structure we use to describe the forms.  
    # maps token names to db names
    _form_lookups = {
                     "nets" : {
                               "class" : NetDistribution,
                               "display" : "nets",
                               "fields" : {
                                           "location" : "location", 
                                           "distributed" : "distributed", 
                                           "expected" : "expected", 
                                           "actual" : "actual",
                                           "discrepancy" : "discrepancy", 
                                           }
                               },
                     "netcards" : {"class" : CardDistribution, 
                              "display" : "net cards",
                              "fields" : {
                                          "location" : "location", 
                                          "settlements" : "settlements", 
                                          "people" : "people", 
                                          "issued" : "distributed",
                                          }
                              }
                     }
        
    _foreign_key_lookups = {"Location" : "code" }

    def validate(self, *args, **kwargs):
        message = args[0]
        form_entry = args[1]
        # in case we need help, build a valid reminder string
        # TODO put this in the db!
        required = ["location", "role", "firstname"]
        help = ("%s register " % form_entry.domain.code.abbreviation.lower()) +\
                " ".join(["<%s>" % t for t in required])
        if form_entry.form.code.abbreviation == "register":
            data = form_entry.to_dict()
            print "\n\n%r\n\n" % data

            # check that ALL FIELDS were provided
            missing = [t for t in required if data[t] is None]
            
            # missing fields! collate them, and
            # send back a friendly non-localized
            # error message, then abort
            if missing:
                mis_str = ", ".join(missing)
                return ["Missing fields: %s" % mis_str, help]
            
            # parse the name via Reporter
            flat_name = data.pop("firstname") + " " + data.pop("secondname") + " " + data.pop("thirdname")
            data["alias"], data["first_name"], data["last_name"] =\
                Reporter.parse_name(flat_name.strip())
            
            # all fields were present and correct, so copy them into the
            # form_entry, for "actions" to pick up again without re-fetching
            form_entry.rep_data = data
            
            # nothing went wrong. the data structure
            # is ready to spawn a Reporter object
            return None
        elif form_entry.form.code.abbreviation in self._form_lookups.keys():
            # we know all the fields in this form are required, so make sure they're set
            # TODO check the token's required flag
            required_token_names = self._form_lookups[form_entry.form.code.abbreviation]["fields"].keys()
            for token in form_entry.tokenentry_set.all():
                if token.token.abbreviation in required_token_names:
                    # found it, as long as the data isn't empty remove it
                    if token.data:
                        required_token_names.remove(token.token.abbreviation)
            if required_token_names:
{'fraction_non_alphanumeric': 0.08791208791208792, 'fraction_numerical': 0.0012487512487512488, 'mean_word_length': 1.2746378869639308, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '783509', 'n_tokens_mistral': 1942, 'n_tokens_neox': 1833, 'n_words': 581}