Example #1
0
def api_commentsubmission(request, output_format = 'json'):
    
    data = {} # response data
    status = 200 # OK

    try:
        logger.info("core.views.api_commentsubmission(request, output_format=\
            'json')")
        if request.method:
            form = CommentSubmitForm(request.REQUEST)
            if request.user.is_anonymous():

                logger.info("core.views.api_commentsubmission(request, output_format=\
                    'json'): user is anonymous")
                status = 401 # Unauthorized
                data['error'] = "You need to log in"
                data['field_errors'] = form.errors
                data['error_html'] = core.utils.build_readable_errors(form.errors)

            userprofile = UserProfile.objects.get(user=request.user)

            if not form.is_valid():
                # Raise exceptions
                
                logger.info("core.views.api_commentsubmission(request, output_format=\
                    'json'): form not valid, errors= %s", form.errors)
                status = 400 # Bad request
                data['error'] = "Some required fields are missing or invalid."
                data['field_errors'] = form.errors
                data['error_html'] = core.utils.build_readable_errors(form.errors)


            else:
                # Form is good
                
                f_comment_type = form.cleaned_data['comment_type_str'] # a comment type
                f_text = form.cleaned_data['text'] # Text
                f_sources = form.cleaned_data['sources']
                f_topic = form.cleaned_data['topic']
                logger.info("core.views.api_commentsubmission(request, output_format=\
                    'json'): form is good, topic=%s, comment=%s, \
                    comment_type=%s", f_topic, f_text, f_comment_type)
                try: f_topic = Topic.objects.get(title = f_topic) # a topic name
                except:
                    raise InvalidTopic()

                f_in_reply_to = form.cleaned_data['in_reply_to'] # a comment

                comment = Comment(text = form.cleaned_data['text'], user = userprofile)
                comment.comment_type = f_comment_type
                comment.save()
                comment.topics = [f_topic]
                if f_sources:
                    comment.sources = [f_sources]
                comment.save()
                data = comment.id

                if f_in_reply_to: # Comment is in reply to another comment
                    reply_relation = CommentRelation(left_comment = comment, \
                        right_comment = f_in_reply_to, relation_type = 'reply')
                    reply_relation.save()


        else: # Not a post
            
            logger.info("core.views.api_commentsubmission(request, output_format=\
                'json'): not a post")
            raise MethodUnsupported("This endpoint only accepts POSTs, you used:" + request.method)
        
    except InvalidTopic:

        logger.info("core.views.api_commentsubmission(request, output_format=\
            'json'): Invalid topic")
        status = 400 # Bad Request
        data['error'] = "Invalid topic"

    logger.info("core.views.api_commentsubmission(request, output_format=\
        'json'): returning data=%s", data)
    return HttpResponse(content = json.dumps(data), mimetype='text/html', status=status)
Example #2
0
def topic(request, whichtopic=1):
    """ Display a topic page for a given topic. """

    clipper_url_form = UrlSubmitForm()

    if settings.PREVIEW_MODE and request.user.is_anonymous():
        # In preview mode and user not logged in.  Redirect to about page.
        response =  HttpResponseRedirect(reverse('about')) 

    else: 
        # Determine if there is an authenticated user and get a little information
        # about that user.
        if not request.user.is_anonymous():
            # Logged in user
            # Assumes consistency between users, UserProfiles
            userprofile = UserProfile.objects.get(user = request.user) 

            is_reporter = userprofile.is_reporter()
        else:
            is_reporter = False
        
        if request.method == 'POST':
            if request.user.is_anonymous():
                # This scenario should be handled more gracefully in JavaScript
                return HttpResponseRedirect("/authenticate")
            
            # If someone just submitted a comment, load the form
            form = CommentSubmitForm(request.POST)
            
            if form.is_valid():
                # Validate the form

                # look up comment by name
                comment_type = form.cleaned_data['comment_type_str'] 


                comment = Comment(text = form.cleaned_data['text'], \
                                  user = userprofile)
                comment.comment_type = comment_type
                # We have to save the comment object so it has a primary key, 
                # before we can link tags to it.
                comment.save() 

                topic = form.cleaned_data['topic']
                in_reply_to = form.cleaned_data['in_reply_to']
                
                # See forms for simplification possibilities
                comment.topics = [Topic.objects.get(title=topic)] 
                if form.cleaned_data['sources']:
                    comment.sources = [form.cleaned_data['sources']]
                comment.save()
                # successfully submitted, give them a new form
                form = CommentSubmitForm() 

                if in_reply_to: # Comment is in reply to another comment
                    reply_relation = CommentRelation(left_comment = comment, \
                                                     right_comment = in_reply_to, \
                                                     relation_type = 'reply')
                    reply_relation.save()

        else: 
            # Give them a new form if have either a valid submission, or no 
            # submission
            form = CommentSubmitForm() 

        
        if Comment.objects.count() > 0:
            reply_form = CommentSubmitForm(initial = { \
                'in_reply_to' : Comment.objects.all()[0]})
        else:
            reply_form = None

        template_dict = { 'site_name':settings.SITE_NAME, \
            'body_classes':settings.SITE_BODY_CLASSES }

        # Will want to filter, order in later versions
        topics = Topic.objects.filter(is_deleted=False)[:5] 

        template_dict['topics'] = topics
        try: 
            topic =  Topic.objects.get(id=whichtopic)
            template_dict['topic'] = topic 
            template_dict['comments_to_show'] = topic.comments_to_show()
           
            # Get a list of comment ids of comments that a user has voted on. 
            if request.user.is_anonymous():
                template_dict['user_voted_comment_ids'] = None
            else:
                template_dict['user_voted_comment_ids'] = \
                    topic.user_voted_comment_ids(user_profile)

        except: 
            # No topic loaded
            pass

        template_dict['comment_form'] = form
        template_dict['reply_form'] = reply_form
        template_dict['comments'] = {}
        template_dict['clipper_url_form'] = clipper_url_form
        template_dict['fb_app_id']=settings.FB_API_ID
        template_dict['is_reporter'] = is_reporter
            
        template_dict.update(csrf(request)) # Required for csrf system

        response = render_to_response('core-topic.html', template_dict, \
                                  context_instance=RequestContext(request))

    return response
Example #3
0
def api_commentsubmission(request, output_format = 'json'):
    
    data = {} # response data
    status = 200 # OK

    try:
        if request.method:

            form = CommentSubmitForm(request.REQUEST)

            if request.user.is_anonymous():
                status = 401 # Unauthorized
                data['error'] = "You need to log in"
                data['field_errors'] = form.errors
                data['error_html'] = core.utils.build_readable_errors(form.errors)

            userprofile = UserProfile.objects.get(user=request.user)

            if not form.is_valid():
                # Raise exceptions

                status = 400 # Bad request
                data['error'] = "Some required fields are missing or invalid."
                data['field_errors'] = form.errors
                data['error_html'] = core.utils.build_readable_errors(form.errors)


            else:
                # Form is good
                
                f_comment_type = form.cleaned_data['comment_type_str'] # a comment type
                f_text = form.cleaned_data['text'] # Text
                f_sources = form.cleaned_data['sources']
                try: f_topic = Topic.objects.get(title = form.cleaned_data['topic']) # a topic name
                except:
                    raise InvalidTopic()

                f_in_reply_to = form.cleaned_data['in_reply_to'] # a comment

                comment = Comment(text = form.cleaned_data['text'], user = userprofile)
                comment.comment_type = f_comment_type
                comment.save()
                comment.topics = [f_topic]
                if f_sources:
                    comment.sources = [f_sources]
                comment.save()
                data['comment_id'] = comment.id

                if f_in_reply_to: # Comment is in reply to another comment
                    reply_relation = CommentRelation(left_comment = comment, right_comment = f_in_reply_to, relation_type = 'reply')
                    reply_relation.save()


        else: # Not a post
            raise MethodUnsupported("This endpoint only accepts POSTs, you used:" + request.method)
        
    except InvalidTopic:
        status = 400 # Bad Request
        data['error'] = "Invalid topic"

    return HttpResponse(content = json.dumps(data), mimetype='text/html', status=status)
    # TK - need code on JavaScript side to to Ajax, etc.
    return HttpResponseRedirect("/")