"""
    Forum conversation views
    ========================

    This module defines views provided by the ``forum_conversation`` application.
    Loads after forum_attachments/views.py

"""

from django.contrib import messages
from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from django.views.generic import DeleteView, FormView, ListView
from django.views.generic.detail import SingleObjectMixin

from machina.apps.forum_conversation.signals import topic_viewed
from machina.conf import settings as machina_settings
from machina.core.db.models import get_model
from machina.core.loading import get_class

#for file check
import os
from django.conf import settings as django_conf_settings

Attachment = get_model('forum_attachments', 'Attachment')
Forum = get_model('forum', 'Forum')
Post = get_model('forum_conversation', 'Post')
Topic = get_model('forum_conversation', 'Topic')
TopicPollOption = get_model('forum_polls', 'TopicPollOption')

AttachmentFormset = get_class('forum_attachments.forms', 'AttachmentFormset')
PostForm = get_class('forum_conversation.forms', 'PostForm')
TopicForm = get_class('forum_conversation.forms', 'TopicForm')
TopicPollOptionFormset = get_class('forum_polls.forms', 'TopicPollOptionFormset')
TopicPollVoteForm = get_class('forum_polls.forms', 'TopicPollVoteForm')

attachments_cache = get_class('forum_attachments.cache', 'cache')

PermissionRequiredMixin = get_class('forum_permission.viewmixins', 'PermissionRequiredMixin')


from django.http import HttpResponse
#def __init__(self, request, *args, **kwargs):
'''def __init__( self,request):
    #super().__init__(*args, **kwargs)
    super().__init__(request,*args, **kwargs)
    print ( 'RENDER TO RESPONSE2?')
    test_setting = 'machina_settings.ATTACHMENT_FILE_UPLOAD_TO'

    return render_to_response(

        request, 'attachments_detail.html', {'test_setting':test_setting}
        #self.get_context_data(request,test_setting=test_setting)
    )'''


''' doesn't work
def __init__(request):
    # ...
    print('HTTP RESPONSE 200')

    # Return a "created" (201) response code.
    return HttpResponse(status=200)
'''

class TopicView(PermissionRequiredMixin, ListView):
    """ Displays a forum topic. """
    #3,1,0,1,2,1,4,1,5
    context_object_name = 'posts'
    paginate_by = machina_settings.TOPIC_POSTS_NUMBER_PER_PAGE
    permission_required = ['can_read_forum', ]
    poll_form_class = TopicPollVoteForm
    template_name = 'forum_conversation/topic_detail.html'
    view_signal = topic_viewed

    def get(self, request, **kwargs):
        print('TESTER0')
        """ Handles GET requests. """
        topic = self.get_topic()

        # Handle pagination
        requested_post = request.GET.get('post', None)
        if requested_post:
            try:
                assert requested_post.isdigit()
                post = topic.posts.get(pk=requested_post)
                requested_page = (
                    ((post.position - 1) // machina_settings.TOPIC_POSTS_NUMBER_PER_PAGE) + 1
                )
                request.GET = request.GET.copy()  # A QueryDict is immutable
                request.GET.update({'page': requested_page})
            except (Post.DoesNotExist, AssertionError):
                pass

        response = super().get(request, **kwargs)
        self.send_signal(request, response, topic)
        return response

    def get_topic(self):
        print('TESTER')
        """ Returns the topic to consider. """
        if not hasattr(self, 'topic'):
            self.topic = get_object_or_404(
                Topic.objects.select_related('forum').all(), pk=self.kwargs['pk'],
            )
        return self.topic

    def get_queryset(self):
        print('TESTER2')
        """ Returns the list of items for this view. """
        self.topic = self.get_topic()
        qs = (
            self.topic.posts
            .all()
            .exclude(approved=False)
            .select_related('poster', 'updated_by')
            .prefetch_related('attachments', 'poster__forum_profile')
        )
        return qs

    def get_controlled_object(self):
        print('TESTER3')
        """ Returns the controlled object. """
        return self.get_topic().forum

    def get_context_data(self, **kwargs):
        print('TESTER4')
        """ Returns the context data to provide to the template. """
        context = super().get_context_data(**kwargs)

        # Insert the considered topic and the associated forum into the context
        topic = self.get_topic()
        context['topic'] = topic
        context['forum'] = topic.forum
        #add this
        context['tester'] = 'TIRED OF TESTING!!'
        context['machina_settings'] = machina_settings
        context['django_conf_settings'] = django_conf_settings

        print(self.request.GET)
        print(self.request.POST)

        # Handles the case when a poll is associated to the topic
        try:
            if hasattr(topic, 'poll') and topic.poll.options.exists():
                context['poll'] = topic.poll
                context['poll_form'] = self.poll_form_class(poll=topic.poll)
                context['view_results_action'] = self.request.GET.get('view_results', None)
                context['change_vote_action'] = self.request.GET.get('change_vote', None)
        except ObjectDoesNotExist:  # pragma: no cover
            pass

        return context

    def send_signal(self, request, response, topic):
        """ Sends the signal associated with the view. """
        print('TESTER5')
        tester = 'DJANGO PARAM TEST!'
        self.view_signal.send(
            sender=self, topic=topic, user=request.user, request=request, response=response,tester=tester
        )




class BasePostFormView(FormView):
    """ A base view for handling post forms. """

    approval_required_message = _('This message will be validated before appearing on the forum.')
    attachment_formset_class = AttachmentFormset
    attachment_formset_general_error_message = _(
        'There are some errors in the attachments you submitted.'
    )
    forum_pk_url_kwarg = None
    post_form_class = PostForm
    post_pk_url_kwarg = None
    success_message = _('This message has been posted successfully.')
    topic_pk_url_kwarg = None
    print ( 'BasePostFormView')

    '''def __init__(self):
        super().__init__( self, self.request, **args )
        print ( 'RENDER TO RESPONSE2?')
        test_setting = 'machina_settings.ATTACHMENT_FILE_UPLOAD_TO'

        return super().render_to_response(

            self.get_context_data(self, self.request, *args, **kwargs, test_setting=test_setting)
            #{request, *args, **kwargs, 'test_setting': test_setting}
        )'''

    def get_fields(self):
      return [(field.name, field.value_to_string(self)) for field in FormView._meta.fields]

    #self.get_attachment_link(self.request) #test call
    #use it like attachment_link.img_link_path
    def index2(request):
        print ( 'RENDER TO RESPONSE?')
        attachment_link={"img_link_path": reverse("'forum_conversation:attachment' pk=attachment.id")}


        #test_setting = machina_settings.ATTACHMENT_FILE_UPLOAD_TO
        test_setting = 'machina_settings.ATTACHMENT_FILE_UPLOAD_TO'
        variables = RequestContext(request, {'attachment_link':attachment_link, 'test_setting':test_setting})


        return render_to_response('forum/<str:forum_slug>-<int:forum_pk>/',
                                    variables  )



    def get(self, request, *args, **kwargs):
        print('TESTER 6')
        """ Handles GET requests. """
        print('get def from base post')
        self.init_attachment_cache()

        # Initializes the forms
        post_form_class = self.get_post_form_class()
        post_form = self.get_post_form(post_form_class)
        attachment_formset_class = self.get_attachment_formset_class()
        attachment_formset = self.get_attachment_formset(attachment_formset_class)

        test_setting = 'machina_settings.ATTACHMENT_FILE_UPLOAD_TO'

        return self.render_to_response(
            self.get_context_data(post_form=post_form, attachment_formset=attachment_formset),
        )

    def post(self, request, *args, **kwargs):
        """ Handles POST requests. """
        print('post def from base post')
        self.init_attachment_cache()
        print('post def from base post2')
        # Stores a boolean indicating if we are considering a preview
        self.preview = 'preview' in self.request.POST

        # Initializes the forms
        post_form_class = self.get_post_form_class()
        post_form = self.get_post_form(post_form_class)
        attachment_formset_class = self.get_attachment_formset_class()
        attachment_formset = self.get_attachment_formset(attachment_formset_class)

        print('self.request.POST')
        print(self.request.POST)
        print('post_form_class')
        print(post_form_class)
        #<class 'machina.apps.forum_conversation.forms.PostForm'>

        print('post_form')
        print(post_form)
        print('attachment_formset_class')
        print(attachment_formset_class)
        #print('attachment_formset')
        #print(attachment_formset)

        '''
        <QueryDict: {'csrfmiddlewaretoken': ['7TsqvXfErMXirhoj0t27blSC0smZpkDKSpU1OJXtJeN46OBTgJ6rYhdcAQ29vnDO'], 'subject': ['Re: Test category topic'], 'content': ['test with attachment'], 'update_reason': [''], 'attachment-TOTAL_FORMS': ['4'], 'attachment-INITIAL_FORMS': ['3'], 'attachment-MIN_NUM_FORMS': ['0'], 'attachment-MAX_NUM_FORMS': ['15'], 'attachment-0-id': ['2'], 'attachment-0-file': [''], 'attachment-0-comment': [''], 'attachment-1-id': ['3'], 'attachment-1-file': [''], 'attachment-1-comment': [''], 'attachment-2-id': ['4'], 'attachment-2-file': [''], 'attachment-2-comment': [''], 'attachment-3-id': [''], 'attachment-3-comment': ['']}>'''

        isAttachmentsInvalid = 0
        ##########
        for filename, file in self.request.FILES.items():
            print('filename key')
            print(filename)

            print('file')
            print(file)

            name = self.request.FILES[filename].name
            print('name')
            print( name )
            print ('size')
            print( str(self.request.FILES[filename].size) + ' vs ' + str(django_conf_settings.DATA_UPLOAD_MAX_MEMORY_SIZE))

            if(self.request.FILES[filename].size > django_conf_settings.DATA_UPLOAD_MAX_MEMORY_SIZE):
                print (' large index: ' + str(filename) + ' file name: ' + str(file))
                isAttachmentsInvalid = 1

            #if (isAttachmentsInvalid ):
                #return self.form_invalid(post_form, attachment_formset)
                #self.request.FILES.pop(filename)
                #nothing = '';
            #i +=1

        self.attachment_preview = (
            self.preview if attachment_formset and attachment_formset.is_valid() else None
        )
        print('attachment preview')
        print(self.attachment_preview)


        post_form_valid = post_form.is_valid()

        print('post_form_valid?')
        print ( post_form_valid)

        #print('attachment_formset validation')
        #print( attachment_formset)

        #print( attachment_formset.is_valid() )

        if (
            (  (post_form_valid and attachment_formset is None) or
            (post_form_valid and attachment_formset.is_valid()) )
            and
            isAttachmentsInvalid == 0
        ):
            print('form is form_valid')
            return self.form_valid(post_form, attachment_formset)
        else:
            print('form is form_invalid')
            return self.form_invalid(post_form, attachment_formset)

    def init_attachment_cache(self):
        """ Initializes the attachment cache for the current view. """
        if self.request.method == 'GET':
            print('cache delete?')
            # Invalidates previous attachments
            attachments_cache.delete(self.get_attachments_cache_key(self.request))
            return

        # Try to restore previous uploaded attachments if applicable
        attachments_cache_key = self.get_attachments_cache_key(self.request)
        restored_attachments_dict = attachments_cache.get(attachments_cache_key)
        print('attachments_cache_key')
        print(attachments_cache_key)

        if restored_attachments_dict:
            #doesn't run on edit form
            restored_attachments_dict.update(self.request.FILES)
            self.request._files = restored_attachments_dict
            print('previous request files')
            print(self.request._files)
        ############ custom
        # Updates the attachment cache if files are available
        if self.request.FILES:
            attachments_cache.set(attachments_cache_key, self.request.FILES)
            print('request.FILES')
            print(self.request.FILES)

        #############


    def get_attachments_cache_key(self, request):
        """ Returns the key used to store attachment files states into the file based cache. """
        return 'attachments_{}'.format(request.session.session_key)

    def get_post_form(self, form_class):
        """ Returns an instance of the post form to be used in the view. """
        return form_class(**self.get_post_form_kwargs())

    def get_post_form_class(self):
        """ Returns the post form class to use for instantiating the form. """
        return self.post_form_class

    def get_post_form_kwargs(self):
        """ Returns the keyword arguments for instantiating the post form. """
        print('handles get_post_form_kwargs first?')
        kwargs = {
            'user': self.request.user,
            'forum': self.get_forum(),
            'topic': self.get_topic(),
        }

        post = self.get_post()
        if post:
            kwargs.update({'instance': post})

        if self.request.method in ('POST', 'PUT'):
            kwargs.update({
                'data': self.request.POST,
                'files': self.request.FILES,
            })
        return kwargs

    def get_attachment_formset(self, formset_class):
        """ Returns an instance of the attachment formset to be used in the view. """
        if (
            self.request.forum_permission_handler.can_attach_files(
                self.get_forum(), self.request.user,
            )
        ):
            return formset_class(**self.get_attachment_formset_kwargs())

    def get_attachment_formset_class(self):
        """ Returns the attachment formset class to use to initialize the attachment formset. """
        return self.attachment_formset_class

    def get_attachment_formset_kwargs(self):
        """ Returns the keyword arguments for instantiating the attachment formset. """
        kwargs = {
            'prefix': 'attachment',
        }

        if self.request.method in ('POST', 'PUT'):
            print('kwargs update')
            kwargs.update({
                'data': self.request.POST,
                'files': self.request.FILES,
            })
        else:
            print('kwargs update queryset')
            post = self.get_post()
            attachment_queryset = Attachment.objects.filter(post=post)
            kwargs.update({
                'queryset': attachment_queryset,
            })
            print(attachment_queryset)

        #print('kwargs2')
        #print(kwargs)
        return kwargs

    def get_context_data(self, **kwargs):
        print('HERE get_context_data')
        """ Returns the context data to provide to the template. """
        context = kwargs
        if 'view' not in context:
            context['view'] = self

        # Insert the considered forum, topic and post into the context
        context['forum'] = self.get_forum()
        context['topic'] = self.get_topic()
        context['post'] = self.get_post()

        # Handles the preview of the attachments
        if context['attachment_formset']:
            if hasattr(self, 'attachment_preview') and self.attachment_preview:
                context['attachment_preview'] = self.attachment_preview
                attachments = []
                # Computes the list of the attachment file names that should be attached
                # to the forum post being created or updated
                for form in context['attachment_formset'].forms:
                    print('attachment formset names')
                    #print(self.request._files[form['file'].html_name].name)
                    if (
                        form['DELETE'].value() or
                        (not form['file'].html_name in self.request._files and not form.instance.pk)
                    ):
                        continue
                    attachments.append(
                        (
                            form,
                            self.request._files[form['file'].html_name].name if not form.instance

                            else form.instance.filename
                        )
                    )
                    print(self.request._files[form['file'].html_name].name)

                context['attachment_file_previews'] = attachments
                print('ATTACHMENTS returned')
                print(attachments)
        print('CONTEXT RETURNED')
        print(context)
        return context

    def get_forum(self):
        """ Returns the considered forum. """
        pk = self.kwargs.get(self.forum_pk_url_kwarg, None)
        if not pk:  # pragma: no cover
            # This should never happen
            return
        if not hasattr(self, '_forum'):
            self._forum = get_object_or_404(Forum, pk=pk)
        return self._forum

    def get_topic(self):
        """ Returns the considered topic if applicable. """
        pk = self.kwargs.get(self.topic_pk_url_kwarg, None)
        if not pk:
            return
        if not hasattr(self, '_topic'):
            self._topic = get_object_or_404(Topic, pk=pk)
        return self._topic

    def get_post(self):
        """ Returns the considered post if applicable. """
        pk = self.kwargs.get(self.post_pk_url_kwarg, None)
        if not pk:
            return
        if not hasattr(self, '_forum_post'):
            self._forum_post = get_object_or_404(Post, pk=pk)
        return self._forum_post

    def form_valid(self, post_form, attachment_formset, **kwargs):
        """ Processes valid forms.

        Called if all forms are valid. Creates a Post instance along with associated attachments if
        required and then redirects to a success page.

        """
        save_attachment_formset = attachment_formset is not None \
            and not self.preview

        if self.preview:
            return self.render_to_response(
                self.get_context_data(
                    preview=True, post_form=post_form, attachment_formset=attachment_formset,
                    **kwargs
                ),
            )

        #print('the post form save')
        #print(post_form)
        ''' post_form
        <tr><th><label for="id_subject">Subject:</label></th><td><input type="text" name="subject" value="Re: Test category topic" maxlength="
        255" placeholder="Enter your subject" required id="id_subject"></td></tr>
        <tr><th><label for="id_content">Message:</label></th><td><textarea name="content" cols="40" rows="10" placeholder="Enter your message"
         required id="id_content" class=" machina-mde-markdown">
        test with attachment</textarea></td></tr>
        <tr><th><label for="id_update_reason">Update reason:</label></th><td><input type="text" name="update_reason" maxlength="255" id="id_up
        date_reason"></td></tr>
        <tr><th><label for="id_enable_signature">Attach a signature:</label></th><td><input type="checkbox" name="enable_signature" id="id_ena
        ble_signature"></td></tr>
        <tr><th><label for="id_lock_topic">Lock topic:</label></th><td><input type="checkbox" name="lock_topic" id="id_lock_topic"></td></tr>

        '''
        # This is not a preview ; the object is going to be saved
        self.forum_post = post_form.save()

        if save_attachment_formset:
            #print ('save_attachment_formset')
            #print (save_attachment_formset)
            '''<input type="hidden" name="attachment-TOTAL_FORMS" value="15" id="id_attachment-TOTAL_FORMS"><input type="hidden" name="attachment-INI
            TIAL_FORMS" value="14" id="id_attachment-INITIAL_FORMS"><input type="hidden" name="attachment-MIN_NUM_FORMS" value="0" id="id_attachme
            nt-MIN_NUM_FORMS"><input type="hidden" name="attachment-MAX_NUM_FORMS" value="15" id="id_attachment-MAX_NUM_FORMS">
            <tr><th><label for="id_attachment-0-file">File:</label></th><td>Currently: <a href="/machina/attachments/2022_Scan_tigerdrawing_hyminO
            K.png">machina/attachments/2022_Scan_tigerdrawing_hyminOK.png</a><br>
            Change:
            <input type="file" name="attachment-0-file" id="id_attachment-0-file"></td></tr>
            <tr><th><label for="id_attachment-0-comment">Comment:</label></th><td><input type="text" name="attachment-0-comment" maxlength="255" i
            d="id_attachment-0-comment"></td></tr>
            <tr><th><label for="id_attachment-0-DELETE">Delete:</label></th><td><input type="checkbox" name="attachment-0-DELETE" id="id_attachmen
            t-0-DELETE"><input type="hidden" name="attachment-0-id" value="21" id="id_attachment-0-id"></td></tr> <tr><th><label for="id_attachmen
            t-1-file">File:</label></th><td>Currently: <a href="/machina/attachments/2022_Scan_tigerdrawing_IwOhiXA.png">machina/attachments/2022_
            Scan_tigerdrawing_IwOhiXA.png</a><br>
            Change:
            <input type="file" name="attachment-1-file" id="id_attachment-1-file"></td></tr>
            <tr><th><label for="id_attachment-1-comment">Comment:</label></th><td><input type="text" name="attachment-1-comment" maxlength="255" i
            d="id_attachment-1-comment"></td></tr>
            <tr><th><label for="id_attachment-1-DELETE">Delete:</label></th><td><input type="checkbox" name="attachment-1-DELETE" id="id_attachmen
            t-1-DELETE"><input type="hidden" name="attachment-1-id" value="36" id="id_attachment-1-id"></td></tr> <tr><th><label for="id_attachmen
            t-2-file">File:</label></th><td>Currently: <a href="/machina/attachments/2022_Scan_tigerdrawing_iEebZNC.png">machina/attachments/2022_
            Scan_tigerdrawing_iEebZNC.png</a><br>

            '''


            print('attachment_formset.post')
            print(attachment_formset.post)
            attachment_formset.post = self.forum_post

            #print('attachment_formset saved #############')
            #print(attachment_formset)
            attachment_formset.save()

            #print('check kwargs.files')
            #print(self.kwargs)
            #{'forum_slug': 'what-do-black-people-eat', 'forum_pk': 3, 'topic_slug': 'test-category-topic', 'topic_pk': 5, 'pk': 8}

        #f = open(machina_settings.ATTACHMENT_FILE_UPLOAD_TO + "/to_be_resized_image.png", "rb")
        #images_dict['to_be_resized_image'] = File(f)
        '''
        field = test._meta.get_field('resized_image')
        field.save_form_data(test, self.images_dict['to_be_resized_image'])
        test.full_clean()
        test.save()

        attachment_formset

        '''
        #for  att_field in attachment_formset:
        #    print('att_field')

        #    print(att_field)
        '''one of them <tr><th><label for="id_attachment-7-file">File:</label></th><td>Currently: <a href="/machina/attachments/1_rt_oZxDlYW.jpg">machina/att
        achments/1_rt_oZxDlYW.jpg</a><br>
        Change:

        <input type="file" name="attachment-7-file" id="id_attachment-7-file"></td></tr>
        <tr><th><label for="id_attachment-7-comment">Comment:</label></th><td><input type="text" name="attachment-7-comment" maxlength="255" i
        d="id_attachment-7-comment"></td></tr>
        <tr><th><label for="id_attachment-7-DELETE">Delete:</label></th><td><input type="checkbox" name="attachment-7-DELETE" id="id_attachmen
        t-7-DELETE"><input type="hidden" name="attachment-7-id" value="54" id="id_attachment-7-id"></td></tr>
        '''
        # boolean for saved_att in save_attachment_formset:
        #    print('in save_attachment_formset')
        #    print(saved_att)

        messages.success(self.request, self.success_message)
        if not self.forum_post.approved:
            messages.warning(self.request, self.approval_required_message)

        return HttpResponseRedirect(self.get_success_url())

    def form_invalid(self, post_form, attachment_formset, **kwargs):
        """ Processes invalid forms.

        Called if one of the forms is invalid. Re-renders the context data with the data-filled
        forms and errors.

        """
        print('attachment errors and length')
        print(attachment_formset.errors)
        print( len( attachment_formset.errors) )
        print(self.attachment_formset_general_error_message)
        attachment_formset.error_msg = self.attachment_formset_general_error_message
        kwargs.update({
            'test': 'something'
        })
        #added
        #attachment_formset.
        #There are some errors in the attachments you submitted.

        if (
            attachment_formset and
            not attachment_formset.is_valid() and
            len(attachment_formset.errors)
        ):

            messages.error(self.request, self.attachment_formset_general_error_message)

        return self.render_to_response(
            self.get_context_data(
                post_form=post_form, attachment_formset=attachment_formset, **kwargs
            ),
        )


class BaseTopicFormView(BasePostFormView):
    """ A base view for handling topic forms. """
    print('BaseTopicFormView')
    poll_option_formset_class = TopicPollOptionFormset
    poll_option_formset_general_error_message = _(
        'There are some errors in the poll options you submitted.'
    )
    # A specific form class is used here in order to fill the Topic-related date in addition to the
    # ones related to Post instances.
    post_form_class = TopicForm


    def get(self, request, *args, **kwargs):
        """ Handles GET requests. """
        self.init_attachment_cache()
        print('get def')
        # Initializes the forms
        post_form_class = self.get_post_form_class()
        post_form = self.get_post_form(post_form_class)
        attachment_formset_class = self.get_attachment_formset_class()
        attachment_formset = self.get_attachment_formset(attachment_formset_class)
        poll_option_formset_class = self.get_poll_option_formset_class()
        poll_option_formset = self.get_poll_option_formset(poll_option_formset_class)

        return self.render_to_response(
            self.get_context_data(
                post_form=post_form, attachment_formset=attachment_formset,
                poll_option_formset=poll_option_formset,
            ),
        )

    def post(self, request, *args, **kwargs):
        """ Handles POST requests. """
        self.init_attachment_cache()

        # Stores a boolean indicating if we are considering a preview
        self.preview = 'preview' in self.request.POST

        # Initializes the forms
        post_form_class = self.get_post_form_class()
        post_form = self.get_post_form(post_form_class)
        attachment_formset_class = self.get_attachment_formset_class()
        attachment_formset = self.get_attachment_formset(attachment_formset_class)
        poll_option_formset_class = self.get_poll_option_formset_class()
        poll_option_formset = self.get_poll_option_formset(poll_option_formset_class)

        post_form_valid = post_form.is_valid()

        attachment_formset_valid = (
            attachment_formset.is_valid() if attachment_formset else None
        )
        poll_option_formset_valid = (
            poll_option_formset.is_valid()
            if poll_option_formset and len(post_form.cleaned_data['poll_question']) else None
        )

        #added
        isAttachmentsInvalid = 0
        ##########
        for filename, file in self.request.FILES.items():
            print('filename key2')
            print(filename)

            print('file2')
            print(file)

            name = self.request.FILES[filename].name
            print('name2')
            print( name )
            print ('size2')
            print( self.request.FILES[filename].size)

            if(self.request.FILES[filename].size > django_conf_settings.DATA_UPLOAD_MAX_MEMORY_SIZE):
                print (' large index2: ' + str(filename) + ' file name: ' + str(file))
                isAttachmentsInvalid = 1

        self.attachment_preview = self.preview if attachment_formset_valid else None
        self.poll_preview = self.preview if poll_option_formset_valid else None

        poll_options_validated = poll_option_formset_valid is not None
        if (
            post_form_valid and
            attachment_formset_valid is not False and
            poll_option_formset_valid is not False

            and
            isAttachmentsInvalid == 0
        ):
            return self.form_valid(
                post_form, attachment_formset, poll_option_formset,
                poll_options_validated=poll_options_validated,
            )
        else:
            return self.form_invalid(
                post_form, attachment_formset, poll_option_formset,
                poll_options_validated=poll_options_validated,
            )

    def get_poll_option_formset(self, formset_class):
        """ Returns an instance of the poll option formset to be used in the view. """
        if self.request.forum_permission_handler.can_create_polls(
            self.get_forum(), self.request.user,
        ):
            return formset_class(**self.get_poll_option_formset_kwargs())

    def get_poll_option_formset_class(self):
        """ Returns the poll option formset class to use to initialize the poll option formset. """
        return self.poll_option_formset_class

    def get_poll_option_formset_kwargs(self):
        """ Returns the keyword arguments for instantiating the poll option formset. """
        kwargs = {
            'prefix': 'poll',
        }

        if self.request.method in ('POST', 'PUT'):
            kwargs.update({
                'data': self.request.POST,
                'files': self.request.FILES,
            })
        else:
            topic = self.get_topic()
            poll_option_queryset = TopicPollOption.objects.filter(poll__topic=topic)
            kwargs.update({
                'queryset': poll_option_queryset,
            })

        print ('kwargs')
        print (kwargs)
        return kwargs

    def get_context_data(self, **kwargs):
        """ Returns the context data to provide to the template. """
        context = super().get_context_data(**kwargs)
        print('get_context_data')
        print(context)
        # Handles the preview of the poll
        if context['poll_option_formset']:
            if hasattr(self, 'poll_preview') and self.poll_preview:
                context['poll_preview'] = self.poll_preview
                context['poll_options_previews'] = filter(
                    lambda f: f['text'].value() and not f['DELETE'].value(),
                    context['poll_option_formset'].forms,
                )
            print('poll_option_formset context')
            print(context)
        return context

    def form_valid(self, post_form, attachment_formset, poll_option_formset, **kwargs):
        """ Processes valid forms. """
        save_poll_option_formset = (
            poll_option_formset is not None and
            not self.preview and
            kwargs['poll_options_validated']
        )

        valid = super().form_valid(
            post_form,
            attachment_formset,
            poll_option_formset=poll_option_formset, **kwargs
        )

        if save_poll_option_formset:
            poll_option_formset.topic = self.forum_post.topic
            poll_option_formset.save(
                poll_question=post_form.cleaned_data.pop('poll_question', None),
                poll_max_options=post_form.cleaned_data.pop('poll_max_options', None),
                poll_duration=post_form.cleaned_data.pop('poll_duration', None),
                poll_user_changes=post_form.cleaned_data.pop('poll_user_changes', None),
            )

        return valid

    def form_invalid(self, post_form, attachment_formset, poll_option_formset, **kwargs):
        """ Processes invalid forms. """
        poll_errors = [k for k in post_form.errors.keys() if k.startswith('poll_')]
        if (
            poll_errors or
            (
                poll_option_formset and
                not poll_option_formset.is_valid() and
                len(post_form.cleaned_data['poll_question'])
            )
        ):
            messages.error(self.request, self.poll_option_formset_general_error_message)

        return super().form_invalid(
            post_form, attachment_formset, poll_option_formset=poll_option_formset, **kwargs)


class PostFormView(SingleObjectMixin, BasePostFormView):
    """ A base view for manipulating post forms. """
    print('PostFormView')
    forum_pk_url_kwarg = 'forum_pk'
    post_pk_url_kwarg = 'pk'
    topic_pk_url_kwarg = 'topic_pk'


class TopicFormView(SingleObjectMixin, BaseTopicFormView):
    """ A base view for manipulating topic forms. """
    print('TopicFormView')
    forum_pk_url_kwarg = 'forum_pk'
    topic_pk_url_kwarg = 'pk'


class TopicCreateView(PermissionRequiredMixin, TopicFormView):
    """ Allows users to create forum topics. """

    model = Topic
    permission_required = ['can_start_new_topics', ]
    template_name = 'forum_conversation/topic_create.html'

    def get(self, request, *args, **kwargs):
        """ Handles GET requests. """
        self.object = None
        return super().get(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        """ Handles POST requests. """
        self.object = None
        return super().post(request, *args, **kwargs)

    def get_controlled_object(self):
        """ Returns the controlled object. """
        return self.get_forum()

    def get_success_url(self):
        """ Returns the URL to redirect the user to upon valid form processing. """
        if not self.forum_post.approved:
            return reverse(
                'forum:forum',
                kwargs={
                    'slug': self.forum_post.topic.forum.slug,
                    'pk': self.forum_post.topic.forum.pk,
                },
            )
        return reverse(
            'forum_conversation:topic',
            kwargs={
                'forum_slug': self.forum_post.topic.forum.slug,
                'forum_pk': self.forum_post.topic.forum.pk,
                'slug': self.forum_post.topic.slug,
                'pk': self.forum_post.topic.pk,
            },
        )


class TopicUpdateView(PermissionRequiredMixin, TopicFormView):
    """ Allows users to update forum topics. """

    model = Topic
    success_message = _('This message has been edited successfully.')
    template_name = 'forum_conversation/topic_update.html'

    def get(self, request, *args, **kwargs):
        """ Handles GET requests. """
        self.object = self.get_object()
        return super().get(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        """ Handles POST requests. """
        self.object = self.get_object()
        return super().post(request, *args, **kwargs)

    def get_controlled_object(self):
        """ Returns the controlled object. """
        return self.get_topic().first_post

    def get_object(self, queryset=None):
        """ Returns the considered object. """
        return self.get_topic()

    def get_post(self):
        """ Returns the considered post if applicable. """
        return self.get_topic().first_post

    def get_success_url(self):
        """ Returns the URL to redirect the user to upon valid form processing. """
        return reverse(
            'forum_conversation:topic', kwargs={
                'forum_slug': self.forum_post.topic.forum.slug,
                'forum_pk': self.forum_post.topic.forum.pk,
                'slug': self.forum_post.topic.slug,
                'pk': self.forum_post.topic.pk,
            },
        )

    def perform_permissions_check(self, user, obj, perms):
        """ Performs the permission check. """
        return self.request.forum_permission_handler.can_edit_post(obj, user)


class PostCreateView(PermissionRequiredMixin, PostFormView):
    """ Allows users to create forum posts. """

    model = Post
    template_name = 'forum_conversation/post_create.html'

    def get(self, request, *args, **kwargs):
        """ Handles GET requests. """
        self.object = None
        return super().get(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        """ Handles POST requests. """
        self.object = None
        return super().post(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        """ Returns the context data to provide to the template. """
        context = super().get_context_data(**kwargs)
        topic = self.get_topic()

        # Add the previous posts to the context
        previous_posts = (
            topic.posts.filter(approved=True)
            .select_related('poster', 'updated_by')
            .prefetch_related('attachments', 'poster__forum_profile')
            .order_by('-created')
        )
        previous_posts = previous_posts[:machina_settings.TOPIC_REVIEW_POSTS_NUMBER]
        context['previous_posts'] = previous_posts

        return context

    def get_controlled_object(self):
        """ Returns the controlled object. """
        return self.get_topic()

    def get_success_url(self):
        """ Returns the URL to redirect the user to upon valid form processing. """
        return '{0}?post={1}#{1}'.format(
            reverse(
                'forum_conversation:topic',
                kwargs={
                    'forum_slug': self.forum_post.topic.forum.slug,
                    'forum_pk': self.forum_post.topic.forum.pk,
                    'slug': self.forum_post.topic.slug,
                    'pk': self.forum_post.topic.pk,
                },
            ),
            self.forum_post.pk,
        )

    def perform_permissions_check(self, user, obj, perms):
        """ Performs the permission check. """
        return self.request.forum_permission_handler.can_add_post(obj, user)


class PostUpdateView(PermissionRequiredMixin, PostFormView):
    """ Allows users to update forum topics. """

    model = Post
    success_message = _('This message has been edited successfully.')
    template_name = 'forum_conversation/post_update.html'

    def get(self, request, *args, **kwargs):
        """ Handles GET requests. """
        self.object = self.get_object()
        return super().get(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        """ Handles POST requests. """
        self.object = self.get_object()
        return super().post(request, *args, **kwargs)

    def get_controlled_object(self):
        """ Returns the controlled object. """
        return self.get_post()

    def get_object(self, queryset=None):
        """ Returns the considered object. """
        return self.get_post()

    def get_success_url(self):
        """ Returns the URL to redirect the user to upon valid form processing. """
        return '{0}?post={1}#{1}'.format(
            reverse(
                'forum_conversation:topic',
                kwargs={
                    'forum_slug': self.forum_post.topic.forum.slug,
                    'forum_pk': self.forum_post.topic.forum.pk,
                    'slug': self.forum_post.topic.slug,
                    'pk': self.forum_post.topic.pk,
                },
            ),
            self.forum_post.pk,
        )

    def perform_permissions_check(self, user, obj, perms):
        """ Performs the permission check. """
        return self.request.forum_permission_handler.can_edit_post(obj, user)


class PostDeleteView(PermissionRequiredMixin, DeleteView):
    """ Allows users to delete forum topics. """

    model = Post
    success_message = _('This message has been deleted successfully.')
    template_name = 'forum_conversation/post_delete.html'

    def get_context_data(self, **kwargs):
        """ Returns the context data to provide to the template. """
        context = super().get_context_data(**kwargs)

        # Append the topic and the forum associated with the post being deleted
        # to the context
        post = self.get_object()
        context['topic'] = post.topic
        context['forum'] = post.topic.forum

        return context

    def get_controlled_object(self):
        """ Returns the controlled object. """
        return self.get_object()

    def get_success_url(self):
        """ Returns the URL to redirect the user to upon valid form processing. """
        messages.success(self.request, self.success_message)

        if self.object.is_topic_head and self.object.is_topic_tail:
            return reverse(
                'forum:forum',
                kwargs={
                    'slug': self.object.topic.forum.slug, 'pk': self.object.topic.forum.pk,
                },
            )

        return reverse(
            'forum_conversation:topic',
            kwargs={
                'forum_slug': self.object.topic.forum.slug,
                'forum_pk': self.object.topic.forum.pk,
                'slug': self.object.topic.slug,
                'pk': self.object.topic.pk,
            },
        )

    def perform_permissions_check(self, user, obj, perms):
        """ Performs the permission check. """
        return self.request.forum_permission_handler.can_delete_post(obj, user)
