Ejemplo n.º 1
0
def postdiscus(request, id):
    post = get_object_or_404(Post, id=id)
    comments = post.comments.filter(post=post)
    comment = Comment(body=post_text, author=request.user)
    comment.save()
    context = {"post": post, "comments": comments}
    return render(request, "chatroom.html", context)
Ejemplo n.º 2
0
    def test_topic_to_blockchain(self):

        self.topic = Topic.objects.create(
            title='Improve test module',
            body='implement class that autogenerates users',
            owner=self.thinker,
        )

        #self.topic.create_snapshot(blockchain=1)
        self.topic.create_snapshot(blockchain=False)
        self.topic.save()

        self.assertEqual(
            TopicSnapshot.objects.filter(topic=self.topic).count(), 1)

        self.comment = Comment(topic=self.topic,
                               text="""
            - {1.5},{?0.5} for coming up with basic class structure,
            - {?2.5} for implementation,
            - {?3.5} for testing.

            Here is the result so far:
            https://github.com/wefindx/infty2.0/commit/9f096dc54f94c31eed9558eb32ef0858f51b1aec
            """,
                               owner=self.doer)
        self.comment.save()
        # self.comment.create_snapshot(blockchain=1)
        self.comment.create_snapshot(blockchain=False)

        self.assertEqual(
            CommentSnapshot.objects.filter(comment=self.comment).count(), 1)
Ejemplo n.º 3
0
def create_fake_comments(number=10):
    Comment.objects.all().delete()
    for i in range(number):
        instance = Comment(
            name=fake.name(),
            email=fake.email(),
            content=fake.text()
        )
        instance.save()
Ejemplo n.º 4
0
    def test_comment_delete(self):
        comment_id = Comment(parent=ndb.Key(urlsafe=self.post_id),
                        name='Joe Bloggs', 
                        url='http://sasmple.com', 
                        email='*****@*****.**', 
                        body='Body Comment').put().urlsafe()

        self.assertEqual(len(Comment.query().fetch()), 1)
        res = self.c.delete('/comment/delete/{0}'.format(comment_id))
        self.assertEqual(len(Comment.query().fetch()), 0)
Ejemplo n.º 5
0
    def setUp(self):
        # Let's say we have currencies:
        self.hur = Currency(label='hur')
        self.hur.save()
        self.eur = Currency(label='eur')
        self.eur.save()
        self.usd = Currency(label='usd')
        self.usd.save()

        # And some currency prices:
        self.hprice = HourPriceSnapshot(
            name='FRED',
            base=self.usd,
            data=json.loads("""
{"realtime_start":"2017-07-28","realtime_end":"2017-07-28","observation_start":"1600-01-01","observation_end":"9999-12-31","units":"lin","output_type":1,"file_type":"json","order_by":"observation_date","sort_order":"desc","count":136,"offset":0,"limit":1,"observations":[{"realtime_start":"2017-07-28","realtime_end":"2017-07-28","date":"2017-06-01","value":"26.25"}]}"""
                            ),
            endpoint=
            'https://api.stlouisfed.org/fred/series/observations?series_id=CES0500000003&api_key=0a90ca7b5204b2ed6e998d9f6877187e&limit=1&sort_order=desc&file_type=json',
        )
        self.hprice.save()
        self.cprice = CurrencyPriceSnapshot(
            name='FIXER',
            base=self.eur,
            data=json.loads("""
{"base":"EUR","date":"2017-07-28","rates":{"AUD":1.4732,"BGN":1.9558,"BRL":3.7015,"CAD":1.4712,"CHF":1.1357,"CNY":7.9087,"CZK":26.048,"DKK":7.4364,"GBP":0.89568,"HKD":9.1613,"HRK":7.412,"HUF":304.93,"IDR":15639.0,"ILS":4.1765,"INR":75.256,"JPY":130.37,"KRW":1317.6,"MXN":20.809,"MYR":5.0229,"NOK":9.3195,"NZD":1.5694,"PHP":59.207,"PLN":4.2493,"RON":4.558,"RUB":69.832,"SEK":9.5355,"SGD":1.5947,"THB":39.146,"TRY":4.1462,"USD":1.1729,"ZAR":15.281}}"""
                            ),
            endpoint='https://api.fixer.io/latest?base=eur',
        )
        self.cprice.save()

        # Let's say we have a user 'thinker', 'doer' and 'investor'..
        self.thinker = self.make_user('thinker')
        self.thinker.save()
        self.doer = self.make_user('doer')
        self.doer.save()
        self.investor = self.make_user('investor')
        self.investor.save()

        # Let's say thinker writes a topic.
        self.topic = Topic.objects.create(
            title='Improve test module',
            body='implement class that autogenerates users',
            owner=self.thinker,
        )
        self.topic.save()

        # Let's say a doer writes a comment with declared hours,
        # more than available in daily quota day:

        self.comment = Comment(topic=self.topic,
                               text="""
            - {14.5},{?0.5} for coming up with basic class structure,
            """,
                               owner=self.doer)
        self.comment.save()
Ejemplo n.º 6
0
    def post(self, request):
        # pass filled out HTML-Form from View to CommentForm()
        form = CommentForm(request.POST)
        if form.is_valid():
            # create a Comment DB Entry
            text = form.cleaned_data['text']
            video_id = request.POST['video']
            video = Video.objects.get(id=video_id)

            new_comment = Comment(text=text, user=request.user, video=video)
            new_comment.save()
            return HttpResponseRedirect('/video/{}'.format(str(video_id)))
        return HttpResponse('This is Register view. POST Request.')
Ejemplo n.º 7
0
def create_comment(request):
    data = json.loads(request.body)
    name = data['name']
    tree_url = data['tree']
    comment = data['comment']
    if None in (name, tree_url, comment):
        return HttpResponse(status=400)
    associated_tree = get_object_or_404(Location, url=tree_url)
    newcomment = Comment(name=name,
                         associated_tree=associated_tree,
                         comment=comment)
    print newcomment
    newcomment.save()
    return HttpResponse(status=200)
Ejemplo n.º 8
0
def add_comment(request, pk):
    """Add a new comment."""
    p = request.POST

    if p.has_key("body") and p["body"]:
        author = "Anonymous"
        if p["author"]: author = p["author"]

        comment = Comment(post=Post.objects.get(pk=pk))
        cf = CommentForm(p, instance=comment)
        cf.fields["author"].required = False

        comment = cf.save(commit=False)
        comment.author = author
        comment.save()
    return HttpResponseRedirect(reverse("core.views.post", args=[pk]))
Ejemplo n.º 9
0
    def test_get_comments(self):
        insert_amount = 30
        with self.app.test_request_context(), self.db.atomic():
            coms = [
                dict(content='comment_test',
                     article=self.art,
                     nickname='tester',
                     reviewed=True) for i in range(insert_amount)
            ]
            Comment.insert_many(coms).execute()

        resp = self.client.get(self.path_prefix + '/comments/',
                               query_string={'limit': insert_amount // 2})
        self.assertResponseRestfulAndSuccess(resp)
        self.assertEqual(insert_amount // 2,
                         len(self.get_json(resp)['comments']))
Ejemplo n.º 10
0
def Detail(request, pk):
    post = Post.objects.get(pk=pk)
    comments = Comment.objects.filter(post=post)
    form = CommentForm()
    context = {
        'post': post,
        'comments': comments,
        'form': form,
    }
    if request.is_ajax() and request.POST:
        text = request.POST.get('comment')
        comment = Comment(text=text, post=post)
        comment.save()
        data = {'message': request.POST.get('comment')}
        return HttpResponse(json.dumps(data), content_type='application/json')
    return render(request, 'core/detail.html', context)
Ejemplo n.º 11
0
def review_comment(aid, cid):
    '''``PATCH`` |API_URL_BASE|/article/:aid/comment/:cid

    Review a comment. The author is the only one having permission.

    Response JSON:

    .. code-block:: javascript

        // success
        {
            $errors: null,
            comment: {
                id: integer,
                nickname: string,
                content: string,
                time: datetime,
                reply_to: integer // maybe null if no references.
            }
        }

        // failed
        {$errors: {reply_to: 'the comment you reply to doesn't not exist.'}}

    Permission required: ``REVIEW_COMMENT``
    '''
    try:
        author_id = (Article.select(Article.author)
                            .where(Article.id == aid).get()).author_id
        comment = Comment.get((Comment.id == cid) & (Comment.article == aid))
        if not author_id == current_user.get_id():
            raise Comment.DoesNotExist()
    except (Article.DoesNotExist, Comment.DoesNotExist):
        return {'comment_id': '欲回复的评论不存在'}

    comment.reviewed = True
    comment.save()

    event_emitted.send(
        current_app._get_current_object(),
        type='Comment: Review',
        description='comment(%d) has been reviewed by %s.' %
                    (comment.id, current_user.get_id())
    )

    return None, {'comment': comment.to_dict()}
Ejemplo n.º 12
0
    def __init__(self, object, *args, **kwargs):
        object_id = object.id
        content_type = get_content_type_for_model(object)
        # If there is no instance, make a fake one!
        if not 'instance' in kwargs:
            kwargs['instance'] = Comment(object_id=object_id,
                                         content_type=content_type)

        super(CommentForm, self).__init__(*args, **kwargs)
Ejemplo n.º 13
0
def make_comment(request,pk):
    if request.method == 'POST':
        form = CommentCreateForm(request.POST)
        if form.is_valid():
            # breakpoint()
            content = form.cleaned_data['content']
            book = Book.objects.get(pk=pk)
            new_comment = Comment(user=request.user,content=content,target_book=book)
            new_comment.save()
        return redirect(to='book-detail', pk=pk)
    else:
        form = CommentCreateForm()
        book = Book.objects.get(pk=pk)

        return render(request, 'core/comment.html', {
            'book' : book,
            'form' : form,
        })
Ejemplo n.º 14
0
def comment_list(request, stock_id=""):
    '''
    comment_list
    '''
    if request.method == 'GET':
        if not request.user.is_authenticated:
            return HttpResponse(status=401)

        response_list = []
        for comment_ in Comment.objects.filter(stock=stock_id).iterator():
            response_list.append({
                'stock': stock_id,
                'time': comment_.time,
                'content': comment_.content,
                'author': comment_.author.nickname,
                'author_id': comment_.author.id,
                'id': comment_.id
            })
        return JsonResponse(response_list, safe=False)

    if request.method == 'POST':
        if not request.user.is_authenticated:
            return HttpResponse(status=401)
        stock = get_object_or_404(Stock, id=stock_id)
        try:
            req_data = json.loads(request.body.decode())
            comment_content = req_data['content']
            comment_author = req_data['author']
        except (KeyError, JSONDecodeError):
            return HttpResponseBadRequest()
        user = get_user_model().objects.get(nickname=comment_author)
        comment_ = Comment(stock=stock, content=comment_content, author=user)
        comment_.save()
        response_dict = {
            'id': comment_.id,
            'stock': comment_.stock.id,
            'time': comment_.time,
            'content': comment_.content,
            'author': comment_.author.nickname,
            'author_id': comment_.author.id
        }
        return JsonResponse(response_dict, status=201)

    return HttpResponseNotAllowed(['GET', 'POST'])
Ejemplo n.º 15
0
def sendcomment(request):
    user = request.user
    form = CommentForm(request.POST)
    if form.is_valid():
        data = form.cleaned_data
        classname = data['objectclass']
        objclass = {
            'Mix': Mix,
            'Group': Group,
            'Tag': Tag,
            'Platform': Platform
        }[classname]
        obj = objclass.objects.get(id=data['objectid'])
        comment = Comment(content_object=obj, text=data['text'], user=user)
        comment.save()
        return HttpResponseRedirect(comment.get_absolute_url())
    else:
        #2do error message
        return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
Ejemplo n.º 16
0
def AddNewComment(request, slug):
    if request.user.is_anonymous():
    	ComOwner = request.POST.get('ItemOwner')
    	ComOwnerFN = ""
    	ComOwnerState = "non-confirmed"

    else:
    	for account in request.user.socialaccount_set.all():
          ItemOwnerPrvdr = account.provider

          if ItemOwnerPrvdr == "facebook": 
		  ComOwner = account.extra_data['username']
		  ComOwnerFN = ""
		  ComOwnerState = "confirmed"
    	  elif ItemOwnerPrvdr == "twitter":
	    	  ComOwner = account.extra_data['screen_name']
	    	  ComOwnerFN = ""
	    	  ComOwnerState = "confirmed"
    	  elif ItemOwnerPrvdr == "google":
	    	  ComOwner = account.extra_data['name']
	    	  ComOwnerFN = account.extra_data['id']
	    	  ComOwnerState = "confirmed"
    	  elif ItemOwnerPrvdr == "persona":
	    	  ComOwner = account.user
	    	  ComOwnerFN = ""
	    	  ComOwnerState = "confirmed"
	  else:
	  	  pass

    ComContent = request.POST.get('comment')
    ComPubDate = timezone.now()
    
    list = get_object_or_404(List, slug=slug)
    p = Comment(
	NewComment = list, 
	ComContent = ComContent,
	ComOwnerState = ComOwnerState,
        ComOwner = ComOwner,
        ComPubDate = ComPubDate,
	)
    p.save()

    return HttpResponseRedirect(reverse('sopler:list', args=(list.slug,)))
Ejemplo n.º 17
0
    def test_get_a_unreviewed_comment_without_permission(self):
        with self.app.test_request_context(), self.db.atomic():
            comment = Comment.create(content='comment_test',
                                     article=self.art,
                                     nickname='tester',
                                     reviewed=False)

        self.login_as_su()
        resp = self.get_a_comment(comment.id)
        self.assertResponseErrorInField(resp, 'comment_id')
Ejemplo n.º 18
0
def AddNewComment(request, slug):
    if request.user.is_anonymous():
        ComOwner = request.POST.get('ItemOwner')
        ComOwnerFN = ""
        ComOwnerState = "non-confirmed"

    else:
        for account in request.user.socialaccount_set.all():
            ItemOwnerPrvdr = account.provider

            if ItemOwnerPrvdr == "facebook":
                ComOwner = account.extra_data['username']
                ComOwnerFN = ""
                ComOwnerState = "confirmed"
            elif ItemOwnerPrvdr == "twitter":
                ComOwner = account.extra_data['screen_name']
                ComOwnerFN = ""
                ComOwnerState = "confirmed"
            elif ItemOwnerPrvdr == "google":
                ComOwner = account.extra_data['name']
                ComOwnerFN = account.extra_data['id']
                ComOwnerState = "confirmed"
            elif ItemOwnerPrvdr == "persona":
                ComOwner = account.user
                ComOwnerFN = ""
                ComOwnerState = "confirmed"
            else:
                pass

    ComContent = request.POST.get('comment')
    ComPubDate = timezone.now()

    list = get_object_or_404(List, slug=slug)
    p = Comment(
        NewComment=list,
        ComContent=ComContent,
        ComOwnerState=ComOwnerState,
        ComOwner=ComOwner,
        ComPubDate=ComPubDate,
    )
    p.save()

    return HttpResponseRedirect(reverse('sopler:list', args=(list.slug, )))
Ejemplo n.º 19
0
def newcomment(request, urlCode):
    """Adds a new comment to the database"""
    output = "OK"
    debugvar = 0
    try:
        if(request.method == "POST"):
            name = request.POST["name"]
            name = javaScriptEscape(name)
            message = request.POST["message"]
            message = javaScriptEscape(message)
            side = request.POST["side"]
            side = javaScriptEscape(side)
            chunk = int(request.POST["chunk"])
            line = int(request.POST["line"])
            
            # basic sanity check
            if(not (side == 'lhs' or side == 'rhs')):
                output = "ERROR"
            else:
                targetPatch = get_object_or_404(Patch, pk=urlCode)
                print "patch - ", urlCode, " chunknum ", chunk
                targetChunks = Chunk.objects.filter(patch = targetPatch, chunkNum = chunk)
                print "Target chunks length - " + str(len(targetChunks))
                targetChunk = targetChunks[0]
                newComment = Comment()
                newComment.chunk = targetChunk
                newComment.commentAuthor = name
                newComment.commentText = message
                newComment.diffSide = side
                newComment.commentLine = line
                newComment.chunkID = chunk
                newComment.commentID = 0

                newComment.save()
        else:
            output = "ERROR"
    except Exception as e:
        from IPython import Shell
        Shell.IPShellEmbed()
        print "exception ", e
        output = "ERROR"
    return HttpResponse(output)
Ejemplo n.º 20
0
def issue_comment_send(request):
    issue = get_object_or_404(Issue, id=request.POST.get("issue", 0))
    text = request.POST.get("comment")
    comment = Comment()
    comment.created_by = request.user
    comment.comment = text
    comment.issue = issue
    comment.save()
    return issue_poll(request)
Ejemplo n.º 21
0
def get_comments(aid):
    '''``GET`` |API_URL_BASE|/article/:aid/comments/

    Get information of all comments to an article.

    :param limit: **Query** limit amount of comment per page,
        default: |COMMENT_LIST_DEFAULT_LIMIT|
    :param page: **Query**  page control, start from zero, default: 1

    Response JSON:

    .. code-block:: javascript

        // success
        {
            $errors: null,
            comments: [
                {
                    id: integer,
                    nickname: string,
                    content: string,
                    time: datetime,
                    reply_to: integer // maybe null if no references.
                }
            ]
        }

        // failed
        {$errors: {article_id: 'this article doesn't not exist.'}}

    Permission required: ``READ_COMMENT``
    '''
    default_limit = app_config['COMMENT_LIST_DEFAULT_LIMIT']

    try:
        author_id = (Article.select(Article.author)
                            .where(Article.id == aid).get()).author_id
    except Article.DoesNotExist:
        return {'article_id': '无法找到这篇文章,可能已经被删除'}

    page = request.args.get('page', 1, type=int) - 1
    limit = request.args.get('limit', default_limit, type=int)

    query = (Comment.select()
                    .where(Comment.article == aid)
                    .offset(page * limit).limit(limit + 1))

    if current_user.get_id() != author_id:
        query = query.where(Comment.reviewed == True)  # noqa: E712

    comments = [comment.to_dict() for comment in query]
    is_more = len(comments) > limit

    return None, {'is_more': is_more, 'comments': comments[:limit]}
Ejemplo n.º 22
0
 def put(self, request, id, **kwargs):
     coerce_put_post(request)
     comment = Comment.get(id)
     if comment:
         comment.name = request.PUT.get('name', '')
         comment.url = request.PUT.get('url', '')
         comment.email = request.PUT.get('email', '')
         comment.body = request.PUT.get('body', '')
         comment.put()
         return HttpResponse(json.dumps({'status': 'OK', 'message': 'Comment Updated'}))
     return HttpResponse(json.dumps({'status': 'Error', 'message': 'Post not foud'}))
Ejemplo n.º 23
0
    def test_get_a_comment_mismatch_article(self):
        with self.app.test_request_context(), self.db.atomic():
            article = Article.create(id='testart_',
                                     title='',
                                     text_type='',
                                     source_text='')
            comment = Comment.create(content='comment_test',
                                     article=self.art,
                                     nickname='tester',
                                     reviewed=True)

        resp = self.client.get(self.api_url_base + '/article/' + article.id +
                               '/comment/' + str(comment.id))
        self.assertResponseErrorInField(resp, 'comment_id')
Ejemplo n.º 24
0
    def test_get_a_unreviewed_comment(self):
        with self.app.test_request_context(), self.db.atomic():
            comment = Comment.create(content='comment_test',
                                     article=self.art,
                                     nickname='tester',
                                     reviewed=False)

        resp = self.get_a_comment(comment.id)
        self.assertResponseRestfulAndSuccess(resp)

        self.assertJSONHasKey(resp, 'comment')
        comment_ = self.get_json(resp)['comment']

        self.assertEqual(comment_['content'], comment.content)
Ejemplo n.º 25
0
def get_a_comment(aid, cid):
    '''``GET`` |API_URL_BASE|/article/:aid/comment/:cid

    Get a comment to an article

    Response JSON:

    .. code-block:: javascript

        // success
        {
            $errors: null,
            comment: {
                id: integer,
                nickname: string,
                content: string,
                time: datetime,
                reply_to: integer // maybe null if no references.
            }
        }

        // failed
        {$errors: {comment_id: 'this comment does not exist.'}}

    Permission required: ``READ_COMMENT``
    '''
    try:
        author_id = (Article.select(Article.author)
                            .where(Article.id == aid).get()).author_id
        comment = Comment.get((Comment.id == cid) & (Comment.article == aid))

        if comment.reviewed is False and current_user.get_id() != author_id:
            raise Comment.DoesNotExist()
    except (Article.DoesNotExist, Comment.DoesNotExist):
        return {'comment_id': '该评论不存在'}

    return None, {'comment': comment.to_dict()}
Ejemplo n.º 26
0
def issue_comment_send(request):
    issue = get_object_or_404(Issue, id=request.POST.get("issue", 0))
    text = request.POST.get("comment")
    comment = Comment()
    comment.created_by = request.user
    comment.comment = text
    comment.issue = issue
    comment.save()
    return issue_poll(request)
Ejemplo n.º 27
0
    def _ask_question(self, topic, text, user_profile):
        comment_type = CommentType.objects.get(name="Question")
        question = Comment(text=text, user=user_profile, comment_type=comment_type)
        question.save()
        question.topics = [topic]
        question.save()
        self._questions.append(question)

        return question
Ejemplo n.º 28
0
def save_comments(data_comments, id_posts):
    cleaned_comments = []

    for obj in data_comments:
        if not id_posts[obj["postId"]]:
            continue

        obj["post"] = id_posts[obj["postId"]]

        cleaned_comments.append(
            Comment(name=obj["name"],
                    email=obj["email"],
                    body=obj["body"],
                    post=obj["post"]))

    response_comments = Comment.objects.bulk_create(cleaned_comments,
                                                    ignore_conflicts=True)
    response_comments = remove_unvalid_objects(response_comments)

    return response_comments
Ejemplo n.º 29
0
def delete_comment(aid, cid):
    '''``DELETE`` |API_URL_BASE|/article/:aid/comment/:cid

    Delete a comment. The author is the only one having permission.

    Response JSON:

    .. code-block:: javascript

        // success
        {$errors: null}

        // failed
        {
            $errors: {
                comment_id: 'the comment you reply to doesn't not exist.'
                permission: 'you are not allowed to delete this comment.'
            }
        }

    Permission required: ``REVIEW_COMMENT``
    '''
    try:
        author_id = (Article.select(Article.author)
                            .where(Article.id == aid).get()).author_id
        comment = Comment.get((Comment.id == cid) & (Comment.article == aid))
    except (Article.DoesNotExist, Comment.DoesNotExist):
        return {'comment_id': '该评论不存在'}

    is_author = author_id == current_user.get_id()
    if not is_author:
        return {'permission': '你无权删除该条评论'}

    comment.delete_instance()

    event_emitted.send(
        current_app._get_current_object(),
        type='Comment: Delete',
        description='comment(%d) has been deleted by %s.' %
                    (comment.id, current_user.get_id())
    )
Ejemplo n.º 30
0
 def insert_a_comment(self, reviewed=False):
     with self.app.test_request_context():
         return Comment.create(article=self.art,
                               reviewed=reviewed,
                               content='',
                               nickname='')
Ejemplo n.º 31
0
 def list(self, request, *args, **kwargs):
     # return Response({'something': 'my custom JSON'})
     comment = Comment(email='*****@*****.**', content='foo bar')
     serializer = CommentSerializer(comment)
     return Response(serializer.data)
Ejemplo n.º 32
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("/")
Ejemplo n.º 33
0
def create_comment(comment, author, post_id):
  comment = Comment(comment=comment, post_id=post_id, author=author)
  comment.save()
  return comment.toJSON()
Ejemplo n.º 34
0
def newcomment(request, urlCode):
    """Adds a new comment to the database"""
    output = "OK"
    debugvar = 0
    try:
        if (request.method == "POST"):
            name = request.POST["name"]
            name = javaScriptEscape(name)
            message = request.POST["message"]
            message = javaScriptEscape(message)
            side = request.POST["side"]
            side = javaScriptEscape(side)
            chunk = int(request.POST["chunk"])
            line = int(request.POST["line"])

            # basic sanity check
            if (not (side == 'lhs' or side == 'rhs')):
                output = "ERROR"
            else:
                targetPatch = get_object_or_404(Patch, pk=urlCode)
                print "patch - ", urlCode, " chunknum ", chunk
                targetChunks = Chunk.objects.filter(patch=targetPatch,
                                                    chunkNum=chunk)
                print "Target chunks length - " + str(len(targetChunks))
                targetChunk = targetChunks[0]
                newComment = Comment()
                newComment.chunk = targetChunk
                newComment.commentAuthor = name
                newComment.commentText = message
                newComment.diffSide = side
                newComment.commentLine = line
                newComment.chunkID = chunk
                newComment.commentID = 0

                newComment.save()
        else:
            output = "ERROR"
    except Exception as e:
        from IPython import Shell
        Shell.IPShellEmbed()
        print "exception ", e
        output = "ERROR"
    return HttpResponse(output)
Ejemplo n.º 35
0
 def delete(self, request, id, **kwargs):
     Comment.delete(id)
     return HttpResponse(json.dumps({'status': 'OK', 'message': 'Post Deleted'}), \
         content_type='application/json')
Ejemplo n.º 36
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)
Ejemplo n.º 37
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
Ejemplo n.º 38
0
 def compute_claimed(self, obj):
     return Comment.user_claimed(obj)
Ejemplo n.º 39
0
class TestTopic(TestCase):
    def setUp(self):
        # Let's say we have currencies:
        self.hur = Currency(label='hur')
        self.hur.save()
        self.usd = Currency(label='usd')
        self.usd.save()
        self.eur = Currency(label='eur')
        self.eur.save()
        self.cny = Currency(label='cny')
        self.cny.save()
        self.gbp = Currency(label='gbp')
        self.gbp.save()

        # Let's say we have the hour price, and currency prices
        # collected periodically:
        #
        # - HourPriceSnapshot
        # - CurrencyPriceSnapshot0

        self.hprice = HourPriceSnapshot(
            name='FRED',
            base=self.usd,
            data=json.loads("""
{"realtime_start":"2017-07-28","realtime_end":"2017-07-28","observation_start":"1600-01-01","observation_end":"9999-12-31","units":"lin","output_type":1,"file_type":"json","order_by":"observation_date","sort_order":"desc","count":136,"offset":0,"limit":1,"observations":[{"realtime_start":"2017-07-28","realtime_end":"2017-07-28","date":"2017-06-01","value":"26.25"}]}"""
                            ),
            endpoint=
            'https://api.stlouisfed.org/fred/series/observations?series_id=CES0500000003&api_key=0a90ca7b5204b2ed6e998d9f6877187e&limit=1&sort_order=desc&file_type=json',
        )
        self.hprice.save()
        self.cprice = CurrencyPriceSnapshot(
            name='FIXER',
            base=self.eur,
            data=json.loads("""
{"base":"EUR","date":"2017-07-28","rates":{"AUD":1.4732,"BGN":1.9558,"BRL":3.7015,"CAD":1.4712,"CHF":1.1357,"CNY":7.9087,"CZK":26.048,"DKK":7.4364,"GBP":0.89568,"HKD":9.1613,"HRK":7.412,"HUF":304.93,"IDR":15639.0,"ILS":4.1765,"INR":75.256,"JPY":130.37,"KRW":1317.6,"MXN":20.809,"MYR":5.0229,"NOK":9.3195,"NZD":1.5694,"PHP":59.207,"PLN":4.2493,"RON":4.558,"RUB":69.832,"SEK":9.5355,"SGD":1.5947,"THB":39.146,"TRY":4.1462,"USD":1.1729,"ZAR":15.281}}"""
                            ),
            endpoint='https://api.fixer.io/latest?base=eur',
        )
        self.cprice.save()

        # Let's say we have a user 'thinker'..
        self.thinker = self.make_user('thinker')
        self.thinker.save()

        # ..who writes a post:
        self.topic = Topic.objects.create(
            title='Improve test module',
            body='implement class that autogenerates users',
            owner=self.thinker,
        )
        self.topic.save()

        # Then, we have a user 'doer'..
        self.doer = self.make_user('doer')
        self.doer.save()

        # ..who creates a comment on it, with:
        self.comment = Comment(
            topic=self.topic,
            # 1. time spent inside "{...}" brackets
            # 2. estimates of future time needed inside "{?...}"
            # 3. declared work result - the content of comment
            text="""
            - {1.5},{?0.5} for coming up with basic class structure,
            - {?2.5} for implementation,
            - {?3.5} for testing.

            Here is the result so far:
            https://github.com/wefindx/infty2.0/commit/9f096dc54f94c31eed9558eb32ef0858f51b1aec
            """,
            owner=self.doer)
        self.comment.save()

        # ...and another comment, with some other amount of time, and result:
        self.comment2 = Comment(topic=self.topic,
                                text="""
            - {?8} for testing.

            Here is the result so far:
            https://wiki.mindey.com/shared/screens/7e402349b3c2e3a626b5d25fd.png
            """,
                                owner=self.doer)
        self.comment2.save()

        # Then, investor comes in:
        self.investor = self.make_user('investor')
        self.investor.save()

        # And there is another investor:

        self.investor2 = self.make_user('investor2')
        self.investor2.save()

        # And there is another investor:

        self.investor3 = self.make_user('investor3')
        self.investor3.save()

        # Ok, so, what investments can happen?

    def test_currency_uppercase(self):
        self.assertEqual(self.hur.label, 'HUR')
        self.assertEqual(self.eur.label, 'EUR')
        self.assertEqual(self.usd.label, 'USD')

    def test_currency_parsing(self):
        self.assertEqual(self.hprice.data['observations'][0]['value'], "26.25")
        self.assertEqual(self.cprice.data['rates']['USD'], 1.1729)

    def test_currency_eur(self):
        self.assertEqual(self.eur.in_hours(),
                         Decimal(1) / ((Decimal('26.25') / Decimal(1.1729))))

    def test_currency_gbp(self):
        self.assertEqual(
            self.gbp.in_hours(),
            Decimal(1) / (Decimal('26.25') / Decimal(1.1729) *
                          (Decimal(0.89568))))

    def test_currency_cny(self):
        self.assertEqual(
            self.cny.in_hours(),
            Decimal(1) / ((Decimal('26.25') / Decimal(1.1729) *
                           (Decimal(7.9087)))))

    def test_comment_parsed_values(self):

        self.assertEqual(self.comment.claimed_hours, 1.5)

        self.assertEqual(self.comment.assumed_hours, 6.5)

    def test_comment2_parsed_values(self):
        self.assertEqual(self.comment2.assumed_hours, Decimal('8.0'))

    def test_topic_saved_to_bigchaindb(self):
        # signed by user who
        # it's easier if all users have at least one keypair.
        pass

    """
    INVESTMENT TYPES TO COVER
    =========================
    """

    def test_simple_investment(self):
        """
        "Simple Investment"
        : there is only one investor, and the amount to invest is smaller
          than then time declared.

        DOER
             1.5 h                       6.5 ĥ
        [------------][-------------------------------------------]

        INVESTOR

        0.2 ḥ
        [--]

        CONTRIBUTIONS
        [--]

        # https://wiki.mindey.com/shared/screens/b6767a949a4884cfd4fe17b41.png
        """

        # Investor decides that s/he wants to invest into 0.2, and pay in EUR
        # of claimed time;  sees the "(1.5 h) invest" button, and clicks it.

        self.tx = self.comment.invest(0.2, 'eur', self.investor)

        self.assertTrue(
            self.tx.hour_unit_cost -
            Decimal('22.38042458862648158969049976') < Decimal('1E-28'))

        # As part of transaction, the ContributionCertificates
        # are generated to both parties - the doer, and investor.
        # For thinker -- will add the option to auto-generate
        # first comment with amount of time claimed to write the topic.

        # Generated amount is approximately equal to amount purchased.
        self.assertTrue(
            (self.tx.matched_hours - Decimal(0.2)) < Decimal('1E-28'))

        # Doer and Investor gets equal amount as contributors.

        # There should be 2 parts of the transaction:
        self.assertEqual(
            ContributionCertificate.objects.filter(
                transaction=self.tx).count(), 2)

        # Both parts should be equal:
        self.assertEqual(
            ContributionCertificate.objects.filter(
                transaction=self.tx).first().hours,
            ContributionCertificate.objects.filter(
                transaction=self.tx).last().hours,
        )

        # Both parts of the transaction should be
        self.assertTrue((ContributionCertificate.objects.filter(
            transaction=self.tx).first().hours -
                         Decimal(0.1)) < Decimal('1E-28'))

        # The balance (score) of user is defined as sum of all contributions!
        self.assertEqual(
            Decimal('0.1'),
            ContributionCertificate.objects.filter(
                received_by=self.doer).aggregate(total=Sum('hours'))['total'])

        # The balance (score) of user is defined as sum of all contributions!
        self.assertEqual(
            Decimal('0.1'),
            ContributionCertificate.objects.filter(
                received_by=self.investor).aggregate(
                    total=Sum('hours'))['total'])

        self.assertEqual(self.comment.remains(),
                         Decimal('1.5') + Decimal('6.5') - Decimal('0.2'))

        # Test the balances of the users.
        self.assertEqual(ContributionCertificate.user_matched(self.doer),
                         Decimal('0.1'))

        self.assertEqual(ContributionCertificate.user_matched(self.investor),
                         Decimal('0.1'))

    def test_simple_investment_multiparty(self):
        """
        "Simple Investment Multiparty"
        : there is a couple of investors, and the total amount of investment
          is smaller than total declared time.

        DOER
             1.5 h                       6.5 ĥ
        [------------][-------------------------------------------]

        INVESTOR 1

        0.2 ḥ
        [--]

        INVESTOR 2
            0.5 ḥ
            [----]

        CONTRIBUTIONS
        [--][----]
        """
        self.tx1 = self.comment.invest(0.2, 'eur', self.investor)
        self.tx2 = self.comment.invest(0.5, 'cny', self.investor2)

        self.assertTrue(
            Decimal(0.7) - self.comment.invested() < Decimal('1E-28'))

        self.assertTrue(
            self.tx1.hour_unit_cost -
            Decimal('22.38042458862648158969049976') < Decimal('1E-28'))

        self.assertTrue(
            self.tx2.hour_unit_cost -
            Decimal('177.0000639440702549483852555') < Decimal('1E-28'))

        self.assertEqual(
            ContributionCertificate.objects.filter(
                comment_snapshot__comment=self.comment).count(), 4)

        self.assertEqual(ContributionCertificate.user_matched(self.doer),
                         Decimal('0.35'))

        self.assertEqual(ContributionCertificate.user_matched(self.investor),
                         Decimal('0.1'))

        self.assertEqual(ContributionCertificate.user_matched(self.investor2),
                         Decimal('0.25'))

    def test_future_investment_one_investor(self):
        """
        DOER
                                     8.0 ĥ
        [---------------------------------------------------------]

        INVESTOR

          1.0 ħ
        [--------]

        CONTRIBUTIONS
        [-FUTURE-] ( .unmatched )


        Now, let's say that the doer updates the comment to show
        the progress (changes the .assumed_hours to .claimed_hours)

                                     8.0 ĥ
        [---------------------------------------------------------]
        COMMENT UPDATE
             |
            \ /
             v

        DOER
        0.4 h                       7.6 ĥ
        [--][-----------------------------------------------------]

        INVESTOR

          1.0 ħ
        [--------]

        CONTRIBUTIONS
        [-FUTURE-] ( .unmatched )

        0.4 ḥ
        [--]

        HAPPENS

        (1) We go through old certificates with broken=False, matched=False,
        (2) Make new child certificates with broken=False, matched=True,
        (3) and at the points of overflow, broken=False, matched=False.
        (4) invalidate parent certificates.
        """

        # We have correct initial state:
        self.assertEqual(self.comment2.claimed_hours, Decimal(0.0))
        self.assertEqual(self.comment2.assumed_hours, Decimal(8.0))
        self.assertEqual(self.comment2.remains(), Decimal(8.0))

        # An investor comes to make an investment of 1.0

        self.tx = self.comment2.invest(1.0, 'eur', self.investor)

        # Both investor and doer get a certificate of investment.
        self.assertEqual(
            ContributionCertificate.objects.filter(
                comment_snapshot__comment=self.comment2).count(), 2)

        # Invested amount 1.0
        self.assertEqual(Decimal('1.0'), self.comment2.invested())

        # Remains to invest 7.0
        self.assertEqual(self.comment2.remains(), Decimal('7.0'))

        # None is matched yet.
        self.assertEqual(self.comment2.matched(), Decimal('0.0'))

        # Both investor and doer should by now have zero matched time.
        self.assertEqual(ContributionCertificate.user_matched(self.doer),
                         Decimal('0.0'))

        self.assertEqual(ContributionCertificate.user_matched(self.investor),
                         Decimal('0.0'))

        # Both doer and investor should have '0.5' h each as 'unmatched'
        self.assertEqual(ContributionCertificate.user_unmatched(self.doer),
                         Decimal('0.5'))

        self.assertEqual(ContributionCertificate.user_unmatched(self.investor),
                         Decimal('0.5'))

        # Let's say, the doer updates comment to show progress.

        self.comment2.text = """
        - {0.4}{?7.6} for testing.

        Here is the result so far:
        https://wiki.mindey.com/shared/screens/7e402349b3c2e3a626b5d25fd.png
        https://wiki.mindey.com/shared/screens/92fe2c4d8c795e4ff39884622.png
        """
        self.comment2.save()

        # The matched time is 0.4
        self.assertEqual(self.comment2.matched(), Decimal('0.4'))

        # The donated time is 0.6
        self.assertEqual(self.comment2.donated(), Decimal('0.6'))

        # Invested should be 1.0 as before
        self.assertEqual(self.comment2.invested(), Decimal('1.0'))

        # Remains the same amount to invest:
        self.assertTrue(
            Decimal('7.0') - self.comment2.remains() < Decimal('1E-15'))

        # Number of certificates for the comment should be:
        self.assertEqual(
            ContributionCertificate.objects.filter(
                transaction__comment=self.comment2).count(), 6)

        # Two of them broken:
        self.assertEqual(
            ContributionCertificate.objects.filter(
                transaction__comment=self.comment2, broken=True).count(), 2)

        # Four of them not broken:
        self.assertEqual(
            ContributionCertificate.objects.filter(
                transaction__comment=self.comment2, broken=False).count(), 4)

        # Two of unbroken those received by doer:
        self.assertEqual(
            ContributionCertificate.objects.filter(
                transaction__comment=self.comment2,
                received_by=self.doer,
                broken=False).count(), 2)

        # Two of unbroken those received by the investor:
        self.assertEqual(
            ContributionCertificate.objects.filter(
                transaction__comment=self.comment2,
                received_by=self.investor,
                broken=False).count(), 2)

        # Doer has 0.2, and investor also 0.2 matched hours.

        # Two of unbroken those received by the investor, summing to 0.2:
        self.assertEqual(
            Decimal(
                ContributionCertificate.objects.filter(
                    transaction__comment=self.comment2,
                    received_by=self.investor,
                    matched=True,
                    broken=False).aggregate(total=Sum('hours')).get('total')
                or 0), Decimal('0.2'))

        # Two of unbroken those received by the doer, summing to 0.2:
        self.assertEqual(
            Decimal(
                ContributionCertificate.objects.filter(
                    transaction__comment=self.comment2,
                    received_by=self.doer,
                    matched=True,
                    broken=False).aggregate(total=Sum('hours')).get('total')
                or 0), Decimal('0.2'))

        # Doer has 0.2 matched hours
        self.assertEqual(ContributionCertificate.user_matched(self.doer),
                         Decimal('0.2'))

        # Investor has 0.2 matched hours
        self.assertEqual(ContributionCertificate.user_matched(self.investor),
                         Decimal('0.2'))

    def test_mixed_present_future_investment_one_investor(self):
        """
        "Mixed Present-Future Investment one investor"
        : there is only one investor, and the amount to invest is larger
          than the time declared.
          (needs to simulate that if there is re-declaration of time,
           where there is more time declared, then the new ContributionCer-
           tificates generated, linked to same Transaction)

        DOER
             1.5 h                       6.5 ĥ
        [------------][-------------------------------------------]

        INVESTOR

                    4.0 ḥ
        [---------------------------]

        CONTRIBUTIONS
        [--PRESENT--][---FUTURE-----] ( .unmatched )
        """
        self.assertEqual(self.comment.claimed_hours, Decimal('1.5'))
        self.assertEqual(self.comment.assumed_hours, Decimal('6.5'))

        self.tx1 = self.comment.invest(4.0, 'eur', self.investor)

        # [INVESTED]
        self.assertEqual(self.comment.invested(), Decimal('4.0'))

        # [PRESENT]
        self.assertEqual(self.comment.matched(), Decimal('1.5'))

        # [FUTURE]
        self.assertEqual(self.comment.donated(), Decimal('2.5'))

        # [REMAINS]
        self.assertEqual(self.comment.remains(), Decimal('4.0'))

        # Balances:
        self.assertEqual(ContributionCertificate.user_matched(self.doer),
                         Decimal('0.75'))

        self.assertEqual(ContributionCertificate.user_matched(self.investor),
                         Decimal('0.75'))

    def test_future_investment_multiparty(self):
        """
        DOER
                                     8.0 ĥ
        [---------------------------------------------------------]

        INVESTOR1 INVESTOR2                INVESTOR3

           1.0 ḥ    1.0 ḥ                    6.0 ḥ
        [--------][--------][-------------------------------------]

        CONTRIBUTIONS
        [-FUTURE-][-FUTURE-][---------------FUTURE----------------]
        ( .unmatched ) ( .unmatched ) ( .unmatched )
        """

        self.assertEqual(self.comment2.remains(), Decimal('8.0'))

        ### Make sure investor3 has enough quota and reserve.
        self.investor3
        payment = Payment.objects.create(request={
            "amount": "150",
            "currency": "usd",
        },
                                         platform=0,
                                         provider=0,
                                         owner=self.investor3)
        ### /Make sure investor3 has enough quota and reserve.

        self.tx1 = self.comment2.invest(1.0, 'eur', self.investor)
        self.tx2 = self.comment2.invest(1.0, 'eur', self.investor2)
        self.tx3 = self.comment2.invest(6.0, 'eur', self.investor3)

        self.assertEqual(self.comment2.invested(), Decimal('8.0'))

        self.assertEqual(self.comment2.remains(), Decimal('0.0'))

        # [PRESENT]
        self.assertEqual(self.comment2.matched(), Decimal('0.0'))

        # [FUTURE]
        self.assertEqual(self.comment2.donated(), Decimal('8.0'))

        # .matched_hours by investors must be zero.
        self.assertEqual(self.comment2.matched(by=self.investor),
                         Decimal('0.0'))
        self.assertEqual(self.comment2.matched(by=self.investor2),
                         Decimal('0.0'))
        self.assertEqual(self.comment2.matched(by=self.investor3),
                         Decimal('0.0'))

        # .donated_hours by investors must be 1/2,1/2,6/2 respectively.
        self.assertEqual(self.comment2.donated(by=self.investor),
                         Decimal('1.0') / Decimal(2))
        self.assertEqual(self.comment2.donated(by=self.investor2),
                         Decimal('1.0') / Decimal(2))
        self.assertEqual(self.comment2.donated(by=self.investor3),
                         Decimal('6.0') / Decimal(2))

        # .donated_hours by doer must be 4, or 1/2+1/2+6/2, as doer gets 50%.
        self.assertEqual(self.comment2.donated(by=self.doer), Decimal('4.0'))

        # Balances
        self.assertEqual(ContributionCertificate.user_matched(self.doer),
                         Decimal('0.0'))

        self.assertEqual(ContributionCertificate.user_matched(self.investor),
                         Decimal('0.0'))

        self.assertEqual(ContributionCertificate.user_matched(self.investor2),
                         Decimal('0.0'))

        self.assertEqual(ContributionCertificate.user_matched(self.investor3),
                         Decimal('0.0'))

        self.assertEqual(ContributionCertificate.user_unmatched(self.doer),
                         Decimal('4.0'))

        self.assertEqual(ContributionCertificate.user_unmatched(self.investor),
                         Decimal('0.5'))

        self.assertEqual(
            ContributionCertificate.user_unmatched(self.investor2),
            Decimal('0.5'))

        self.assertEqual(
            ContributionCertificate.user_unmatched(self.investor3),
            Decimal('3.0'))

    def test_mixed_present_future_investment_multiparty(self):
        """
        "Mixed Present-Future Investment Multiparty
        : there is a couple of investors ,and the total amount of investment
          is larger than the time declared.
          (needs to simulate that if there is re-declaration of time,
          where there is more time declared, then the new ContributionCer-
          tificates are generated, so as to preserve priority of investors'
          sequence. E.g., if small extra time is declared, the newly
          crated ContributionCertificate is for the earlier investments.)

        DOER
             1.5 h                       6.5 ĥ
        [------------][-------------------------------------------]

        INVESTOR1 INVESTOR2                INVESTOR3

           1.0 ḥ    1.0 ḥ                    6.0 ḥ
        [--------][--------][-------------------------------------]

        CONTRIBUTIONS
        [----P---][P-][-F--][--------------FUTURE-----------------]

        """
        self.assertEqual(self.comment.claimed_hours, Decimal('1.5'))
        self.assertEqual(self.comment.assumed_hours, Decimal('6.5'))
        self.assertEqual(self.comment.remains(), Decimal('8.0'))

        self.tx1 = self.comment.invest(1.0, 'eur', self.investor)
        self.assertEqual(self.comment.invested(), Decimal('1.0'))
        self.assertEqual(self.comment.remains(), Decimal('7.0'))
        self.assertEqual(self.comment.donated(), Decimal('0.0'))
        self.assertEqual(self.comment.contributions(), 2)

        self.assertEqual(self.comment.claimed_hours - self.comment.matched(),
                         Decimal(0.5))

        self.assertEqual(self.comment.assumed_hours - self.comment.donated(),
                         Decimal(6.5))

        self.assertEqual(self.comment.remains(), Decimal('7.0'))

        self.tx2 = self.comment.invest(1.0, 'eur', self.investor2)
        self.assertEqual(self.comment.invested(), Decimal('2.0'))
        self.assertEqual(self.comment.remains(), Decimal('6.0'))
        self.assertEqual(self.comment.contributions(), 6)
        self.assertEqual(self.comment.donated(), Decimal('0.5'))

        ### Make sure investor3 has enough quota and reserve.
        payment = Payment.objects.create(request={
            "amount": "200",
            "currency": "usd",
        },
                                         platform=0,
                                         provider=0,
                                         owner=self.investor3)
        ### /Make sure investor3 has enough quota and reserve.

        self.tx3 = self.comment.invest(6.0, 'eur', self.investor3)
        self.assertEqual(self.comment.invested(), Decimal('8.0'))
        self.assertEqual(self.comment.remains(), Decimal('0.0'))
        self.assertEqual(self.comment.donated(), Decimal('6.5'))

        self.assertEqual(self.comment.remains(), Decimal('0.0'))

        self.assertEqual(self.comment.matched(), Decimal('1.5'))

        self.assertEqual(self.comment.donated(), Decimal('6.5'))

        # Balances
        self.assertEqual(ContributionCertificate.user_matched(self.doer),
                         Decimal('0.75'))

        self.assertEqual(ContributionCertificate.user_matched(self.investor),
                         Decimal('0.5'))

        self.assertEqual(ContributionCertificate.user_matched(self.investor2),
                         Decimal('0.25'))

        self.assertEqual(ContributionCertificate.user_matched(self.investor3),
                         Decimal('0.0'))

        self.assertEqual(ContributionCertificate.user_unmatched(self.doer),
                         Decimal('3.25'))

        self.assertEqual(ContributionCertificate.user_unmatched(self.investor),
                         Decimal('0.0'))

        self.assertEqual(
            ContributionCertificate.user_unmatched(self.investor2),
            Decimal('0.25'))

        self.assertEqual(
            ContributionCertificate.user_unmatched(self.investor3),
            Decimal('3.0'))

    def test_redeclaration_of_less_time(self):
        """
        test re-declarations of time. E.g., if there is already covered
          declared time, it should not be possible to save comment with
          less declared time than is already covered by transactions.

        -Should not be possible to change the declared time to smaller
        if there are investments already.-
        """

        self.assertEqual(self.comment.claimed_hours, Decimal('1.5'))
        self.assertEqual(self.comment.assumed_hours, Decimal('6.5'))

        self.comment.invest(0.1, 'eur', self.investor)

        self.comment.text = """
        - {1.0},{?0.1} for coming up with basic class structure,
        - {?2.5} for implementation,
        - {?3.5} for testing.

        Here is the result so far:
        https://github.com/wefindx/infty2.0/commit/9f096dc54f94c31eed9558eb32ef0858f51b1aec
        """

        self.comment.save()

        self.assertEqual(self.comment.claimed_hours, Decimal('1.0'))
        self.assertTrue(
            self.comment.assumed_hours - Decimal('6.1') < Decimal('1E-15'))

        self.assertEqual(self.comment.invested(), Decimal('0.1'))

        self.assertEqual(self.comment.matched(), Decimal('0.1'))

        self.assertEqual(self.comment.donated(), Decimal('0.0'))

        # Balances
        self.assertEqual(ContributionCertificate.user_matched(self.doer),
                         Decimal('0.05'))

        self.assertEqual(ContributionCertificate.user_matched(self.investor),
                         Decimal('0.05'))

        self.assertEqual(ContributionCertificate.user_unmatched(self.doer),
                         Decimal('0.0'))

        self.assertEqual(ContributionCertificate.user_unmatched(self.investor),
                         Decimal('0.0'))
Ejemplo n.º 40
0
class TestReserve(TestCase):
    def setUp(self):
        # Let's say we have currencies:
        self.hur = Currency(label='hur')
        self.hur.save()
        self.eur = Currency(label='eur')
        self.eur.save()
        self.usd = Currency(label='usd')
        self.usd.save()

        # And some currency prices:
        self.hprice = HourPriceSnapshot(
            name='FRED',
            base=self.usd,
            data=json.loads("""
{"realtime_start":"2017-07-28","realtime_end":"2017-07-28","observation_start":"1600-01-01","observation_end":"9999-12-31","units":"lin","output_type":1,"file_type":"json","order_by":"observation_date","sort_order":"desc","count":136,"offset":0,"limit":1,"observations":[{"realtime_start":"2017-07-28","realtime_end":"2017-07-28","date":"2017-06-01","value":"26.25"}]}"""
                            ),
            endpoint=
            'https://api.stlouisfed.org/fred/series/observations?series_id=CES0500000003&api_key=0a90ca7b5204b2ed6e998d9f6877187e&limit=1&sort_order=desc&file_type=json',
        )
        self.hprice.save()
        self.cprice = CurrencyPriceSnapshot(
            name='FIXER',
            base=self.eur,
            data=json.loads("""
{"base":"EUR","date":"2017-07-28","rates":{"AUD":1.4732,"BGN":1.9558,"BRL":3.7015,"CAD":1.4712,"CHF":1.1357,"CNY":7.9087,"CZK":26.048,"DKK":7.4364,"GBP":0.89568,"HKD":9.1613,"HRK":7.412,"HUF":304.93,"IDR":15639.0,"ILS":4.1765,"INR":75.256,"JPY":130.37,"KRW":1317.6,"MXN":20.809,"MYR":5.0229,"NOK":9.3195,"NZD":1.5694,"PHP":59.207,"PLN":4.2493,"RON":4.558,"RUB":69.832,"SEK":9.5355,"SGD":1.5947,"THB":39.146,"TRY":4.1462,"USD":1.1729,"ZAR":15.281}}"""
                            ),
            endpoint='https://api.fixer.io/latest?base=eur',
        )
        self.cprice.save()

        # Let's say we have a user 'thinker', 'doer' and 'investor'..
        self.thinker = self.make_user('thinker')
        self.thinker.save()
        self.doer = self.make_user('doer')
        self.doer.save()
        self.investor = self.make_user('investor')
        self.investor.save()

        # Let's say thinker writes a topic.
        self.topic = Topic.objects.create(
            title='Improve test module',
            body='implement class that autogenerates users',
            owner=self.thinker,
        )
        self.topic.save()

        # Let's say a doer writes a comment with declared hours,
        # more than available in daily quota day:

        self.comment = Comment(topic=self.topic,
                               text="""
            - {14.5},{?0.5} for coming up with basic class structure,
            """,
                               owner=self.doer)
        self.comment.save()

    def test_comment_parsed_values(self):

        self.assertEqual(self.comment.claimed_hours, 14.5)

        self.assertEqual(self.comment.assumed_hours, 0.5)

    def test_user_has_zero_reserve(self):

        self.assertEqual(Reserve.user_reserve_remains(self.investor), 0.)

    @responses.activate
    def test_user_can_change_reserve(self):
        # Initially, quota should be as defined in settings.
        self.assertEqual(Transaction.user_quota_remains_today(self.investor),
                         4.)
        # Initially, reserve should be zero:
        self.assertEqual(Reserve.user_reserve_remains(self.investor), 0.)

        # After payment, reserve should be more than zero:
        payment = Payment.objects.create(request={
            "amount": "150",
            "currency": "usd",
        },
                                         platform=0,
                                         provider=0,
                                         owner=self.investor)

        self.assertEqual(Reserve.user_reserve_remains(self.investor),
                         Decimal('5.71428571'))
        self.assertEqual(Transaction.user_quota_remains_today(self.investor),
                         4.)

        # Investment should use up the hours, first from quota,
        tx = self.comment.invest(1., 'eur', self.investor)

        self.assertEqual(Transaction.user_quota_remains_today(self.investor),
                         3.)

        self.assertEqual(Reserve.user_reserve_remains(self.investor),
                         Decimal('5.71428571'))

        # Try more
        tx = self.comment.invest(3., 'eur', self.investor)

        self.assertEqual(Transaction.user_quota_remains_today(self.investor),
                         0.)

        self.assertEqual(Reserve.user_reserve_remains(self.investor),
                         Decimal('5.71428571'))

        # Try more
        tx = self.comment.invest(1., 'eur', self.investor)

        self.assertEqual(Transaction.user_quota_remains_today(self.investor),
                         0.)

        self.assertEqual(Reserve.user_reserve_remains(self.investor),
                         Decimal('4.71428571'))

        credit = Transaction.user_quota_remains_today(self.investor) + \
            Reserve.user_reserve_remains(self.investor)

        self.assertEqual(credit, Decimal('4.71428571'))

        # Remaining credit should be such:
        self.assertEqual(
            credit,
            (Decimal(4.) + Decimal('5.71428571')) - \
            (Decimal(1.) + Decimal(3.) + Decimal(1.))
        )

        # Comment should still have remaining 10. hrs
        self.assertEqual(
            self.comment.remains(),
            Decimal(14.5)+Decimal(0.5) - \
            (Decimal(1.) + Decimal(3.) + Decimal(1.))
        )

        # If we try invest more than available, we should fail:

        # Try more
        tx = self.comment.invest(4.9, 'eur', self.investor)

        # Not created:
        self.assertEqual(tx, None)

        # But we should be able to invest exactly the remainder:

        tx = self.comment.invest(Decimal('4.71428571'), 'eur', self.investor)

        self.assertEqual(self.comment.remains(), Decimal('5.28571428'))

        credit = Transaction.user_quota_remains_today(self.investor) + \
            Reserve.user_reserve_remains(self.investor)

        self.assertEqual(credit, 0.)
Ejemplo n.º 41
0
    def setUp(self):
        # Let's say we have currencies:
        self.hur = Currency(label='hur')
        self.hur.save()
        self.usd = Currency(label='usd')
        self.usd.save()
        self.eur = Currency(label='eur')
        self.eur.save()
        self.cny = Currency(label='cny')
        self.cny.save()
        self.gbp = Currency(label='gbp')
        self.gbp.save()

        # Let's say we have the hour price, and currency prices
        # collected periodically:
        #
        # - HourPriceSnapshot
        # - CurrencyPriceSnapshot0

        self.hprice = HourPriceSnapshot(
            name='FRED',
            base=self.usd,
            data=json.loads("""
{"realtime_start":"2017-07-28","realtime_end":"2017-07-28","observation_start":"1600-01-01","observation_end":"9999-12-31","units":"lin","output_type":1,"file_type":"json","order_by":"observation_date","sort_order":"desc","count":136,"offset":0,"limit":1,"observations":[{"realtime_start":"2017-07-28","realtime_end":"2017-07-28","date":"2017-06-01","value":"26.25"}]}"""
                            ),
            endpoint=
            'https://api.stlouisfed.org/fred/series/observations?series_id=CES0500000003&api_key=0a90ca7b5204b2ed6e998d9f6877187e&limit=1&sort_order=desc&file_type=json',
        )
        self.hprice.save()
        self.cprice = CurrencyPriceSnapshot(
            name='FIXER',
            base=self.eur,
            data=json.loads("""
{"base":"EUR","date":"2017-07-28","rates":{"AUD":1.4732,"BGN":1.9558,"BRL":3.7015,"CAD":1.4712,"CHF":1.1357,"CNY":7.9087,"CZK":26.048,"DKK":7.4364,"GBP":0.89568,"HKD":9.1613,"HRK":7.412,"HUF":304.93,"IDR":15639.0,"ILS":4.1765,"INR":75.256,"JPY":130.37,"KRW":1317.6,"MXN":20.809,"MYR":5.0229,"NOK":9.3195,"NZD":1.5694,"PHP":59.207,"PLN":4.2493,"RON":4.558,"RUB":69.832,"SEK":9.5355,"SGD":1.5947,"THB":39.146,"TRY":4.1462,"USD":1.1729,"ZAR":15.281}}"""
                            ),
            endpoint='https://api.fixer.io/latest?base=eur',
        )
        self.cprice.save()

        # Let's say we have a user 'thinker'..
        self.thinker = self.make_user('thinker')
        self.thinker.save()

        # ..who writes a post:
        self.topic = Topic.objects.create(
            title='Improve test module',
            body='implement class that autogenerates users',
            owner=self.thinker,
        )
        self.topic.save()

        # Then, we have a user 'doer'..
        self.doer = self.make_user('doer')
        self.doer.save()

        # ..who creates a comment on it, with:
        self.comment = Comment(
            topic=self.topic,
            # 1. time spent inside "{...}" brackets
            # 2. estimates of future time needed inside "{?...}"
            # 3. declared work result - the content of comment
            text="""
            - {1.5},{?0.5} for coming up with basic class structure,
            - {?2.5} for implementation,
            - {?3.5} for testing.

            Here is the result so far:
            https://github.com/wefindx/infty2.0/commit/9f096dc54f94c31eed9558eb32ef0858f51b1aec
            """,
            owner=self.doer)
        self.comment.save()

        # ...and another comment, with some other amount of time, and result:
        self.comment2 = Comment(topic=self.topic,
                                text="""
            - {?8} for testing.

            Here is the result so far:
            https://wiki.mindey.com/shared/screens/7e402349b3c2e3a626b5d25fd.png
            """,
                                owner=self.doer)
        self.comment2.save()

        # Then, investor comes in:
        self.investor = self.make_user('investor')
        self.investor.save()

        # And there is another investor:

        self.investor2 = self.make_user('investor2')
        self.investor2.save()

        # And there is another investor:

        self.investor3 = self.make_user('investor3')
        self.investor3.save()