# Path: cavedb/middleware.py
# def get_request_uri():
#     return getattr(THREAD_LOCALS, 'request_uri', None)
# 
# def get_valid_bulletins():
#     return getattr(THREAD_LOCALS, 'valid_bulletins', [])


import os
import re
import time
import cavedb.docgen_all
import cavedb.perms
import cavedb.utils
from django.conf import settings
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.db import models
from cavedb.middleware import get_request_uri, get_valid_bulletins


class BulletinRegion(models.Model):
    bulletin = models.ForeignKey(Bulletin)
    region_name = models.CharField(max_length=64)
    map_region_name = models.CharField(max_length=64, blank=True, null=True)
    introduction = models.TextField(blank=True, null=True)
    show_gis_map = models.BooleanField('Show GIS Map', null=False, default=True)
    sort_order = models.IntegerField()

    create_date = models.DateTimeField("Creation Date", auto_now_add=True, editable=False, \
                                       null=True)
    mod_date = models.DateTimeField("Modification Date", auto_now=True, editable=False, null=True)

    def __str__(self):
        return '%s - %s' % (self.bulletin.short_name, self.region_name)

    class Meta:
        verbose_name = 'region'
        ordering = ('bulletin', 'region_name',)


# Only show the user the regions that they are allowed to see.
class RegionChoice(models.ForeignKey):
    #pylint: disable=abstract-method

    def formfield(self, **kwargs):
        #pylint: disable=protected-access

        return super(RegionChoice, self).formfield(queryset=self.rel.to._default_manager \

# Path: cavedb/middleware.py
# def get_request_uri():
#     return getattr(THREAD_LOCALS, 'request_uri', None)
# 
# def get_valid_bulletins():
#     return getattr(THREAD_LOCALS, 'valid_bulletins', [])


# Path: cavedb/models.py
import os
import re
import time
import cavedb.docgen_all
import cavedb.perms
import cavedb.utils
from django.conf import settings
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.db import models
from cavedb.middleware import get_request_uri, get_valid_bulletins



class BulletinRegion(models.Model):
    bulletin = models.ForeignKey(Bulletin)
    region_name = models.CharField(max_length=64)
    map_region_name = models.CharField(max_length=64, blank=True, null=True)
    introduction = models.TextField(blank=True, null=True)
    show_gis_map = models.BooleanField('Show GIS Map', null=False, default=True)
    sort_order = models.IntegerField()

    create_date = models.DateTimeField("Creation Date", auto_now_add=True, editable=False, \
                                       null=True)
    mod_date = models.DateTimeField("Modification Date", auto_now=True, editable=False, null=True)

    def __str__(self):
        return '%s - %s' % (self.bulletin.short_name, self.region_name)

    class Meta:
        verbose_name = 'region'
        ordering = ('bulletin', 'region_name',)


# Only show the user the regions that they are allowed to see.
class RegionChoice(models.ForeignKey):
    #pylint: disable=abstract-method

    def formfield(self, **kwargs):
        #pylint: disable=protected-access

        return super(RegionChoice, self).formfield(queryset=self.rel.to._default_manager \

                   .complex_filter({'bulletin__id__in': get_valid_bulletins()}))