# Path: instance/models.py
# class WriteItInstanceConfig(models.Model):
#     writeitinstance = AutoOneToOneField(WriteItInstance, related_name='config')
#     testing_mode = models.BooleanField(default=True)
#     moderation_needed_in_all_messages = models.BooleanField(
#         help_text=_("Every message is going to \
#         have a moderation mail"), default=False)
#     allow_messages_using_form = models.BooleanField(
#         help_text=_("Allow the creation of new messages \
#         using the web"), default=True)
#     rate_limiter = models.IntegerField(default=0)
#     notify_owner_when_new_answer = models.BooleanField(
#         help_text=_("The owner of this instance \
#         should be notified \
#         when a new answer comes in"), default=False)
#     autoconfirm_api_messages = models.BooleanField(
#         help_text=_("Messages pushed to the api should \
#             be confirmed automatically"), default=True)
# 
#     custom_from_domain = models.CharField(max_length=512, null=True, blank=True)
#     email_host = models.CharField(max_length=512, null=True, blank=True)
#     email_host_password = models.CharField(max_length=512, null=True, blank=True)
#     email_host_user = models.CharField(max_length=512, null=True, blank=True)
#     email_port = models.IntegerField(null=True, blank=True)
#     email_use_tls = models.NullBooleanField()
#     email_use_ssl = models.NullBooleanField()
#     can_create_answer = models.BooleanField(default=False, help_text="Can create an answer using the WebUI")
#     maximum_recipients = models.PositiveIntegerField(default=5)
#     default_language = models.CharField(max_length=10, choices=settings.LANGUAGES)
# 
#     def get_mail_connection(self):
#         connection = mail.get_connection()
#         if self.custom_from_domain:
#             connection.host = self.email_host
#             connection.password = self.email_host_password
#             connection.username = self.email_host_user
#             connection.port = self.email_port
#             connection.use_tls = self.email_use_tls
#         return connection


from collections import OrderedDict
from django.middleware.locale import LocaleMiddleware
from django.utils import translation
from django.conf import settings
from instance.models import WriteItInstanceConfig




def get_language_from_request(request, check_path=False):
    if check_path:
        lang_code = translation.get_language_from_path(request.path_info)
        if lang_code is not None:
            return lang_code

    try:
        lang_code = WriteItInstanceConfig.objects.get(writeitinstance__slug=request.subdomain).default_language

# Path: instance/models.py
# class WriteItInstanceConfig(models.Model):
#     writeitinstance = AutoOneToOneField(WriteItInstance, related_name='config')
#     testing_mode = models.BooleanField(default=True)
#     moderation_needed_in_all_messages = models.BooleanField(
#         help_text=_("Every message is going to \
#         have a moderation mail"), default=False)
#     allow_messages_using_form = models.BooleanField(
#         help_text=_("Allow the creation of new messages \
#         using the web"), default=True)
#     rate_limiter = models.IntegerField(default=0)
#     notify_owner_when_new_answer = models.BooleanField(
#         help_text=_("The owner of this instance \
#         should be notified \
#         when a new answer comes in"), default=False)
#     autoconfirm_api_messages = models.BooleanField(
#         help_text=_("Messages pushed to the api should \
#             be confirmed automatically"), default=True)
# 
#     custom_from_domain = models.CharField(max_length=512, null=True, blank=True)
#     email_host = models.CharField(max_length=512, null=True, blank=True)
#     email_host_password = models.CharField(max_length=512, null=True, blank=True)
#     email_host_user = models.CharField(max_length=512, null=True, blank=True)
#     email_port = models.IntegerField(null=True, blank=True)
#     email_use_tls = models.NullBooleanField()
#     email_use_ssl = models.NullBooleanField()
#     can_create_answer = models.BooleanField(default=False, help_text="Can create an answer using the WebUI")
#     maximum_recipients = models.PositiveIntegerField(default=5)
#     default_language = models.CharField(max_length=10, choices=settings.LANGUAGES)
# 
#     def get_mail_connection(self):
#         connection = mail.get_connection()
#         if self.custom_from_domain:
#             connection.host = self.email_host
#             connection.password = self.email_host_password
#             connection.username = self.email_host_user
#             connection.port = self.email_port
#             connection.use_tls = self.email_use_tls
#         return connection


# Path: nuntium/middleware.py
from collections import OrderedDict
from django.middleware.locale import LocaleMiddleware
from django.utils import translation
from django.conf import settings
from instance.models import WriteItInstanceConfig





def get_language_from_request(request, check_path=False):
    if check_path:
        lang_code = translation.get_language_from_path(request.path_info)
        if lang_code is not None:
            return lang_code

    try:
        lang_code = WriteItInstanceConfig.objects.get(writeitinstance__slug=request.subdomain).default_language

    except WriteItInstanceConfig.DoesNotExist: