# Path: accounts/models.py
# class UserProfile(models.Model):
#     user = models.OneToOneField(User)
#     first_login = models.BooleanField(default=True)
#     force_logout_date = models.DateTimeField(null=True, blank=True)
#     organization = models.ForeignKey(Organization, blank=True, null=True)
#     telephone = models.CharField(max_length=13, blank=True, null=True)
# 
#     def force_logout(self):
#         self.force_logout_date = datetime.datetime.now()
#         self.save()
# 
#     def is_owner(self, instance):
#         if self.user in instance.users:
#             return True
#         else:
#             for group in self.user.groups.all():
#                 if group in instance.groups:
#                     return True
#         return False
# 
#     def __unicode__(self):
#         return "%s profile" % self.user


from django.contrib import messages
from django.utils.translation import ugettext as _
from django.core.urlresolvers import reverse
from django.utils.safestring import mark_safe
from django.core.exceptions import ObjectDoesNotExist
from accounts.models import UserProfile
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#




class UserMessageMiddleware(object):
    """
    Middleware to display various messages to the users.
    """

    def process_request(self, request):
        if not hasattr(request, "session"):
            return

        if request.user.is_authenticated():
            try:
                first_login = request.session["first_login"]
            except KeyError:
                try:
                    profile = request.user.userprofile
                except ObjectDoesNotExist:

# Path: accounts/models.py
# class UserProfile(models.Model):
#     user = models.OneToOneField(User)
#     first_login = models.BooleanField(default=True)
#     force_logout_date = models.DateTimeField(null=True, blank=True)
#     organization = models.ForeignKey(Organization, blank=True, null=True)
#     telephone = models.CharField(max_length=13, blank=True, null=True)
# 
#     def force_logout(self):
#         self.force_logout_date = datetime.datetime.now()
#         self.save()
# 
#     def is_owner(self, instance):
#         if self.user in instance.users:
#             return True
#         else:
#             for group in self.user.groups.all():
#                 if group in instance.groups:
#                     return True
#         return False
# 
#     def __unicode__(self):
#         return "%s profile" % self.user


# Path: middleware/UserMessages.py
from django.contrib import messages
from django.utils.translation import ugettext as _
from django.core.urlresolvers import reverse
from django.utils.safestring import mark_safe
from django.core.exceptions import ObjectDoesNotExist
from accounts.models import UserProfile

# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#




class UserMessageMiddleware(object):
    """
    Middleware to display various messages to the users.
    """

    def process_request(self, request):
        if not hasattr(request, "session"):
            return

        if request.user.is_authenticated():
            try:
                first_login = request.session["first_login"]
            except KeyError:
                try:
                    profile = request.user.userprofile
                except ObjectDoesNotExist:

                    profile = UserProfile.objects.create(user=request.user)