<commit_before>from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from django.db import connection

from domain.models import Membership

from intel.schema_models import *


# Adding roles and clinics to the user profile

class Role(models.Model):
    name = models.CharField(max_length=255)    

    def __unicode__(self):
        return self.name
    class Meta:
        verbose_name = _("Role")


class Clinic(models.Model):
    name = models.CharField(max_length=255)

    def __unicode__(self):
        return self.name
    class Meta:
        verbose_name = _("Clinic")


class UserProfile(models.Model):
    user    = models.ForeignKey(User, unique=True)
    clinic  = models.ForeignKey(Clinic, null=True)
    role    = models.ForeignKey(Role, null=True)


# schema specific methods - these use the inspectdb general schema_models.py which in turn dumps the models generated per the domain's xforms

REGISTRATION_TABLE = IntelGrameenMotherRegistration._meta.db_table
FOLLOWUP_TABLE     = IntelGrameenSafeMotherhoodFollowup._meta.db_table

def registrations():
    return IntelGrameenMotherRegistration.objects.filter(sampledata_meta_userid__gt=0)

def hi_risk():
    return IntelGrameenMotherRegistration.objects.filter(sampledata_meta_userid__gt=0, sampledata_hi_risk="yes")

def follow_up():
    return IntelGrameenSafeMotherhoodFollowup.objects.all()


# this is adapted from the various SqlReport stuff, which wasn't accurate (references xforms_ tables instead of actual data)
# and was a pain to use
# 
# TODO: see if Django GROUP BY equivalent is powerful enough to turn this to ORM code instead of SQL
def registrations_by_clinic():
    sql = ''' 
        select clinic_id, count(sampledata_case_id)
        from %s, intel_userprofile
        where sampledata_meta_userid > 0
        and sampledata_meta_userid = intel_userprofile.user_id
        group by clinic_id
    ''' % REGISTRATION_TABLE<commit_msg>allow blank clinic/role, add get_role_for()<commit_after>from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from django.db import connection

from domain.models import Membership

from intel.schema_models import *


# Adding roles and clinics to the user profile

class Role(models.Model):
    name = models.CharField(max_length=255)    

    def __unicode__(self):
        return self.name
    class Meta:
        verbose_name = _("Role")


class Clinic(models.Model):
    name = models.CharField(max_length=255)

    def __unicode__(self):
        return self.name
    class Meta:
        verbose_name = _("Clinic")


class UserProfile(models.Model):
    user    = models.ForeignKey(User, unique=True)
    clinic  = models.ForeignKey(Clinic, null=True, blank=True)
    role    = models.ForeignKey(Role, null=True, blank=True)



def get_role_for(user):
    # this is ugly. UserProfile is supposed to take of this, and just provide User.get_profile().role
    # but it doesn't work. I suspect RapidSMS ignores AUTH_PROFILE_MODULE in local.ini
    # TODO: check later. we might change the model in any case.
    # role = UserProfile.objects.get(user=user.id).role
    try:
        role = UserProfile.objects.get(user=user.id).role
    except UserProfile.DoesNotExist:
        role = Role.objects.all()[0]

    return role

# schema specific methods - these use the inspectdb general schema_models.py which in turn dumps the models generated per the domain's xforms

REGISTRATION_TABLE = IntelGrameenMotherRegistration._meta.db_table
FOLLOWUP_TABLE     = IntelGrameenSafeMotherhoodFollowup._meta.db_table

def registrations():
    return IntelGrameenMotherRegistration.objects.filter(sampledata_meta_userid__gt=0)

def hi_risk():
    return IntelGrameenMotherRegistration.objects.filter(sampledata_meta_userid__gt=0, sampledata_hi_risk="yes")

def follow_up():
    return IntelGrameenSafeMotherhoodFollowup.objects.all()


# this is adapted from the various SqlReport stuff, which wasn't accurate (references xforms_ tables instead of actual data)
# and was a pain to use
# 
# TODO: see if Django GROUP BY equivalent is powerful enough to turn this to ORM code instead of SQL
def registrations_by_clinic():
    sql = ''' 
        select clinic_id, count(sampledata_case_id)
        from %s, intel_userprofile
        where sampledata_meta_userid > 0
        and sampledata_meta_userid = intel_userprofile.user_id
        group by clinic_id
    ''' % REGISTRATION_TABLE
{'fraction_non_alphanumeric': 0.07285934404578473, 'fraction_numerical': 0.004182258419546555, 'mean_word_length': 4.054505005561735, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3668262', 'n_tokens_mistral': 1399, 'n_tokens_neox': 1317, 'n_words': 386}