# Path: app/models.py
# class TheUser(models.Model):
#     """
#     Class for user objects in database.
#     """
#     REMINDER_TEMPLATE = json.dumps({
#         "common": {
#             "fb_page": True,
#             "fb_group": True,
#             "twitter": True,
#             "vk": True,
#             "disabled_all": False
#         },
#         "api": {
#             "app_rate": True
#         },
#         "web": {
#             "app_download": True,
#         },
#     })
# 
#     id_user = models.OneToOneField(User)
#     user_photo = models.ImageField(blank=True, upload_to='user', storage=OverwriteStorage())
#     auth_token = models.CharField(max_length=50, null=True, blank=True)
#     subscription = models.BooleanField(default=True)
#     reminder = models.CharField(max_length=256, null=False, default=REMINDER_TEMPLATE)
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def __str__(self):
#         return str(self.id_user)
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def get_api_reminders(self):
#         """
#         Returns the reminders only necessary for API endpoints.
#         """
#         data = json.loads(self.reminder)
# 
#         mobile_data = dict(data['common'])
#         mobile_data.update(data['api'])
# 
#         return mobile_data
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def get_web_reminders(self):
#         """
#         Returns the reminders only necessary for web part.
#         """
#         data = json.loads(self.reminder)
#         
#         web_data = dict(data['common'])
#         web_data.update(data['web'])
# 
#         return web_data
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def update_reminder(self, field, value):
#         """
#         Updates the reminder status.
#         """
#         data = json.loads(self.reminder)
# 
#         for key in data:
#             if field in data[key]:
#                 data[key][field] = value
# 
#         self.reminder = json.dumps(data)
#         self.save()
# 
# Path: app/views/additional_views.py
# @process_method('POST', 404)
# def user_logout(request):
#     """
#     Closes session, returns index page.
#     """
#     logger.info("User '{}' logged out.".format(request.user))
#     logout(request)
#     return redirect('index')
# 
# def share_txt(request, file):
#     """
#     Returns the shared .txt files.
#     """
#     path = settings.BASE_DIR + '/Plamber/additional/{}'.format(file)
# 
#     if os.path.exists(path):
#         with open(path, 'r', encoding='utf-8') as data:
#             return HttpResponse(data.read(), content_type='text/plain')
# 
#     return HttpResponse(status=404)
# 
# def share_xml(request, file):
#     """
#     Returns the shared .xml files.
#     """
#     path = settings.BASE_DIR + '/Plamber/additional/{}'.format(file)
# 
#     if os.path.exists(path):
#         with open(path, 'r', encoding='utf-8') as data:
#             return HttpResponse(data.read(), content_type='application/xml')
# 
#     return HttpResponse(status=404)
# 
# def unsubscribe(request, token):
#     """
#     Removes the user from the list of blog subscribers.
#     """
#     username, date = token.split('-')
#     user = get_object_or_404(TheUser, id_user__username=username,
#                              id_user__date_joined=datetime.fromtimestamp(float(date)))
#     user.subscription = False
#     user.save()
# 
#     return render(request, 'additional/unsubscribe.html', context={'user': user})


import os
import shutil
from django.conf import settings
from django.contrib import auth
from django.contrib.auth.models import User, AnonymousUser
from django.shortcuts import reverse
from django.test import TestCase, Client
from ...models import TheUser
from ...views.additional_views import user_logout, share_txt, share_xml, unsubscribe
DESTINATION_DIR = os.path.join(settings.BASE_DIR, 'Plamber/additional/{}')


# ----------------------------------------------------------------------------------------------------------------------
class AdditionalViewsTest(TestCase):

    # ------------------------------------------------------------------------------------------------------------------
    @classmethod
    def setUpTestData(cls):
        shutil.copy(os.path.join(TEST_DATA_DIR, 'test.txt'), DESTINATION_DIR.format('test.txt'))
        shutil.copy(os.path.join(TEST_DATA_DIR, 'test.xml'), DESTINATION_DIR.format('test.xml'))

        cls.user = User.objects.create_user(username='additional', email='logout@user.com', password='Dummy#password')
        cls.user.date_joined = cls.user.date_joined.replace(microsecond=0)
        cls.user.save()

        cls.anonymous_client = Client()
        cls.logged_client = Client()
        cls.logged_client.login(username='additional', password='Dummy#password')

    # ------------------------------------------------------------------------------------------------------------------
    @classmethod
    def tearDownClass(cls):
        os.remove(DESTINATION_DIR.format('test.txt'))
        os.remove(DESTINATION_DIR.format('test.xml'))

    # ------------------------------------------------------------------------------------------------------------------
    def test_user_logout_not_post(self):
        response = self.logged_client.get(reverse('logout'))


# Path: app/models.py
# class TheUser(models.Model):
#     """
#     Class for user objects in database.
#     """
#     REMINDER_TEMPLATE = json.dumps({
#         "common": {
#             "fb_page": True,
#             "fb_group": True,
#             "twitter": True,
#             "vk": True,
#             "disabled_all": False
#         },
#         "api": {
#             "app_rate": True
#         },
#         "web": {
#             "app_download": True,
#         },
#     })
# 
#     id_user = models.OneToOneField(User)
#     user_photo = models.ImageField(blank=True, upload_to='user', storage=OverwriteStorage())
#     auth_token = models.CharField(max_length=50, null=True, blank=True)
#     subscription = models.BooleanField(default=True)
#     reminder = models.CharField(max_length=256, null=False, default=REMINDER_TEMPLATE)
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def __str__(self):
#         return str(self.id_user)
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def get_api_reminders(self):
#         """
#         Returns the reminders only necessary for API endpoints.
#         """
#         data = json.loads(self.reminder)
# 
#         mobile_data = dict(data['common'])
#         mobile_data.update(data['api'])
# 
#         return mobile_data
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def get_web_reminders(self):
#         """
#         Returns the reminders only necessary for web part.
#         """
#         data = json.loads(self.reminder)
#         
#         web_data = dict(data['common'])
#         web_data.update(data['web'])
# 
#         return web_data
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def update_reminder(self, field, value):
#         """
#         Updates the reminder status.
#         """
#         data = json.loads(self.reminder)
# 
#         for key in data:
#             if field in data[key]:
#                 data[key][field] = value
# 
#         self.reminder = json.dumps(data)
#         self.save()
# 
# Path: app/views/additional_views.py
# @process_method('POST', 404)
# def user_logout(request):
#     """
#     Closes session, returns index page.
#     """
#     logger.info("User '{}' logged out.".format(request.user))
#     logout(request)
#     return redirect('index')
# 
# def share_txt(request, file):
#     """
#     Returns the shared .txt files.
#     """
#     path = settings.BASE_DIR + '/Plamber/additional/{}'.format(file)
# 
#     if os.path.exists(path):
#         with open(path, 'r', encoding='utf-8') as data:
#             return HttpResponse(data.read(), content_type='text/plain')
# 
#     return HttpResponse(status=404)
# 
# def share_xml(request, file):
#     """
#     Returns the shared .xml files.
#     """
#     path = settings.BASE_DIR + '/Plamber/additional/{}'.format(file)
# 
#     if os.path.exists(path):
#         with open(path, 'r', encoding='utf-8') as data:
#             return HttpResponse(data.read(), content_type='application/xml')
# 
#     return HttpResponse(status=404)
# 
# def unsubscribe(request, token):
#     """
#     Removes the user from the list of blog subscribers.
#     """
#     username, date = token.split('-')
#     user = get_object_or_404(TheUser, id_user__username=username,
#                              id_user__date_joined=datetime.fromtimestamp(float(date)))
#     user.subscription = False
#     user.save()
# 
#     return render(request, 'additional/unsubscribe.html', context={'user': user})


# Path: app/tests/test_views/test_additional.py
import os
import shutil
from django.conf import settings
from django.contrib import auth
from django.contrib.auth.models import User, AnonymousUser
from django.shortcuts import reverse
from django.test import TestCase, Client
from ...models import TheUser
from ...views.additional_views import user_logout, share_txt, share_xml, unsubscribe

DESTINATION_DIR = os.path.join(settings.BASE_DIR, 'Plamber/additional/{}')


# ----------------------------------------------------------------------------------------------------------------------
class AdditionalViewsTest(TestCase):

    # ------------------------------------------------------------------------------------------------------------------
    @classmethod
    def setUpTestData(cls):
        shutil.copy(os.path.join(TEST_DATA_DIR, 'test.txt'), DESTINATION_DIR.format('test.txt'))
        shutil.copy(os.path.join(TEST_DATA_DIR, 'test.xml'), DESTINATION_DIR.format('test.xml'))

        cls.user = User.objects.create_user(username='additional', email='logout@user.com', password='Dummy#password')
        cls.user.date_joined = cls.user.date_joined.replace(microsecond=0)
        cls.user.save()

        cls.anonymous_client = Client()
        cls.logged_client = Client()
        cls.logged_client.login(username='additional', password='Dummy#password')

    # ------------------------------------------------------------------------------------------------------------------
    @classmethod
    def tearDownClass(cls):
        os.remove(DESTINATION_DIR.format('test.txt'))
        os.remove(DESTINATION_DIR.format('test.xml'))

    # ------------------------------------------------------------------------------------------------------------------
    def test_user_logout_not_post(self):
        response = self.logged_client.get(reverse('logout'))


        self.assertEqual(response.resolver_match.func, user_logout)