Ejemplo n.º 1
0
    def setUp(self):
        self.user = get_user_model().objects.create_user(
            username='******',
            email='*****@*****.**',
            password='******'
        )
        self.other_user = get_user_model().objects.create_user(
            username='******',
            email='*****@*****.**',
            password='******'
        )
        self.feed = Feed.objects.create(
            user=self.user,
            post='A not so long text',
            likes=0,
            comments=0
        )

        self.feed2 = Feed.objects.create(
            user=self.user,
            post='Another text',
            likes=0,
            comments=0
        )

        self.feed.comment(self.other_user, 'my comment')

        like = Activity(activity_type=Activity.LIKE, feed=self.feed.id, user=self.other_user)
        like.save()
        self.user.profile.notify_liked(self.feed)
Ejemplo n.º 2
0
def like(request):
    "点击like 的 view"

    # 获取 被点赞的feed id
    feed_id = request.POST['feed']

    # 获取feed
    feed = Feed.objects.get(pk=feed_id)

    # 获取当前的登陆用户
    user = request.user

    # 查找是否这个用户以前点过赞
    like = Activity.objects.filter(activity_type=Activity.LIKE, feed=feed_id,
                                   user=user)
    if like:
        # 如果点过赞,删除notifications
        user.profile.unotify_liked(feed)
        # 删除这个点赞记录
        like.delete()

    else:
        # 创建一个赞
        like = Activity(activity_type=Activity.LIKE, feed=feed_id, user=user)
        like.save()
        user.profile.notify_liked(feed)

    return HttpResponse(feed.calculate_likes())
Ejemplo n.º 3
0
    def setUp(self):
        self.user = get_user_model().objects.create_user(
            username='******',
            email='*****@*****.**',
            password='******')
        self.other_user = get_user_model().objects.create_user(
            username='******',
            email='*****@*****.**',
            password='******')
        self.feed = Feed.objects.create(user=self.user,
                                        post='A not so long text',
                                        likes=0,
                                        comments=0)

        self.feed2 = Feed.objects.create(user=self.user,
                                         post='Another text',
                                         likes=0,
                                         comments=0)

        self.feed.comment(self.other_user, 'my comment')

        like = Activity(activity_type=Activity.LIKE,
                        feed=self.feed.id,
                        user=self.other_user)
        like.save()
        self.user.profile.notify_liked(self.feed)
Ejemplo n.º 4
0
def favorite(request):
    question_id = request.POST['question']
    question = Question.objects.get(pk=question_id)
    user = request.user
    activity = Activity.objects.filter(activity_type=Activity.FAVORITE, user=user, question=question_id)
    if activity:
        activity.delete()
    else:
        activity = Activity(activity_type=Activity.FAVORITE, user=user, question=question_id)
        activity.save()
    return HttpResponse(question.calculate_favorites())
Ejemplo n.º 5
0
def like(request):
    feed_id = request.POST['feed']
    feed = Feed.objects.get(pk=feed_id)
    user = request.user
    like = Activity.objects.filter(activity_type=Activity.LIKE, feed=feed_id, user=user)
    if like:
        like.delete()
    else:
        like = Activity(activity_type=Activity.LIKE, feed=feed_id, user=user)
        like.save()
    return HttpResponse(feed.calculate_likes())
Ejemplo n.º 6
0
def vote(request):
    answer_id = request.POST['answer']
    answer = Answer.objects.get(pk=answer_id)
    vote = request.POST['vote']
    user = request.user
    activity = Activity.objects.filter(Q(activity_type=Activity.UP_VOTE) | Q(activity_type=Activity.DOWN_VOTE), user=user, answer=answer_id)
    if activity:
        activity.delete()
    if vote in [Activity.UP_VOTE, Activity.DOWN_VOTE]:
        activity = Activity(activity_type=vote, user=user, answer=answer_id)
        activity.save()
    return HttpResponse(answer.calculate_votes())
Ejemplo n.º 7
0
def vote(request):
    answer_id = request.POST['answer']
    answer = Answer.objects.get(pk=answer_id)
    vote = request.POST['vote']
    user = request.user
    activity = Activity.objects.filter(Q(activity_type=Activity.UP_VOTE) | Q(activity_type=Activity.DOWN_VOTE), user=user, answer=answer_id)
    if activity:
        activity.delete()
    if vote in [Activity.UP_VOTE, Activity.DOWN_VOTE]:
        activity = Activity(activity_type=vote, user=user, answer=answer_id)
        activity.save()
    return HttpResponse(answer.calculate_votes())
Ejemplo n.º 8
0
def like(request):
    twit_id = request.POST['twit']
    twit = Twit.objects.get(pk=twit_id)
    user = request.user
    like = Activity.objects.filter(activity_type=Activity.LIKE, twit=twit_id, user=user)
    if like:
        user.profile.unotify_liked(twit)
        like.delete()
    else:
        like = Activity(activity_type=Activity.LIKE, twit=twit_id, user=user)
        like.save()
        user.profile.notify_liked(twit)
    return HttpResponse(twit.calculate_likes())
Ejemplo n.º 9
0
def like(request):
    kpop_id = request.POST['kpop']
    kpop = Kpop.objects.get(pk=kpop_id)
    user = request.user
    like = Activity.objects.filter(activity_type=Activity.LIKE, kpop=kpop_id, user=user)
    if like:
        user.profile.unotify_liked(kpop)
        like.delete()
    else:
        like = Activity(activity_type=Activity.LIKE, kpop=kpop_id, user=user)
        like.save()
        user.profile.notify_liked(kpop)
    return HttpResponse(kpop.calculate_likes())
Ejemplo n.º 10
0
 def test_activity_monthly_statistic(self):
     activity_one = Activity.objects.create(user=self.user,
                                            activity_type='L')
     activity_two = Activity.objects.create(user=self.user,
                                            activity_type='L')
     activity_three = Activity.objects.create(user=self.user,
                                              activity_type='L')
     self.assertTrue(isinstance(activity_one, Activity))
     self.assertTrue(isinstance(activity_two, Activity))
     self.assertTrue(isinstance(activity_three, Activity))
     self.assertEqual(
         Activity.monthly_activity(self.user)[0],
         '[{}]'.format(Activity.objects.all().count()))
     self.assertEqual(Activity.monthly_activity(self.other_user)[0], '0')
Ejemplo n.º 11
0
def profile(request, username):
    page_user = get_object_or_404(User, username=username)
    all_feeds = Feed.get_feeds().filter(user=page_user)
    paginator = Paginator(all_feeds, FEEDS_NUM_PAGES)
    feeds = paginator.page(1)
    from_feed = -1
    if feeds:  # pragma: no cover
        from_feed = feeds[0].id

    feeds_count = Feed.objects.filter(user=page_user).count()
    article_count = Article.objects.filter(create_user=page_user).count()
    article_comment_count = ArticleComment.objects.filter(
        user=page_user).count()
    activity_count = Activity.objects.filter(user=page_user).count()
    messages_count = Message.objects.filter(
        Q(from_user=page_user) | Q(user=page_user)).count()
    data, datepoints = Activity.daily_activity(page_user)
    data = {
        'page_user': page_user,
        'feeds_count': feeds_count,
        'article_count': article_count,
        'article_comment_count': article_comment_count,
        'global_interactions': activity_count + article_comment_count + messages_count,  # noqa: E501
        'bar_data': [
            feeds_count, article_count, article_comment_count, activity_count],
        'bar_labels': json.dumps('["Feeds", "Articles", "Comments", "Activities"]'),  # noqa: E501
        'line_labels': datepoints,
        'line_data': data,
        'feeds': feeds,
        'from_feed': from_feed,
        'page': 1
        }
    return render(request, 'core/profile.html', data)
Ejemplo n.º 12
0
def like(request):
    feed_id = request.POST['feed']
    feed = Feed.objects.get(pk=feed_id)
    user = request.user
    like = Activity.objects.filter(activity_type=Activity.LIKE, feed=feed_id,
                                   user=user)
    if like:
        user.profile.unotify_liked(feed)
        like.delete()

    else:
        like = Activity(activity_type=Activity.LIKE, feed=feed_id, user=user)
        like.save()
        user.profile.notify_liked(feed)
        Notification.set_notify(user.username)

    return HttpResponse(feed.calculate_likes())
Ejemplo n.º 13
0
def question_vote(request):
    question_id = request.POST['question']
    question = Question.objects.get(pk=question_id)
    vote = request.POST['vote']
    user = request.user
    activity = Activity.objects.filter(
        Q(activity_type=Activity.UP_VOTE) | Q(activity_type=Activity.DOWN_VOTE),  # noqa: E501
        user=user, question=question_id)
    if activity:
        activity.delete()

    if vote in [Activity.UP_VOTE, Activity.DOWN_VOTE]:
        activity = Activity(activity_type=vote,
                            user=user, question=question_id)
        activity.save()

    return HttpResponse(question.calculate_votes())
Ejemplo n.º 14
0
def question_vote(request):
    question_id = request.POST['question']
    question = Question.objects.get(pk=question_id)
    vote = request.POST['vote']
    user = request.user
    activity = Activity.objects.filter(
        Q(activity_type=Activity.UP_VOTE) | Q(activity_type=Activity.DOWN_VOTE),  # noqa: E501
        user=user, question=question_id)
    if activity:
        activity.delete()

    if vote in [Activity.UP_VOTE, Activity.DOWN_VOTE]:
        activity = Activity(activity_type=vote,
                            user=user, question=question_id)
        activity.save()

    return HttpResponse(question.calculate_votes())
Ejemplo n.º 15
0
def like(request):
    feed_id = request.POST['feed']
    feed = Feed.objects.get(pk=feed_id)
    user = request.user
    like = Activity.objects.filter(activity_type=Activity.LIKE,
                                   feed=feed_id,
                                   user=user)
    if like:
        user.profile.unotify_liked(feed)
        like.delete()

    else:
        like = Activity(activity_type=Activity.LIKE, feed=feed_id, user=user)
        like.save()
        user.profile.notify_liked(feed)
        Notification.set_notify(user.username)

    return HttpResponse(feed.calculate_likes())
Ejemplo n.º 16
0
def favorite(request):
    # user 收藏question,处理方式同vote
    question_id = request.POST['question']
    question = Question.objects.get(pk=question_id)
    user = request.user
    activity = Activity.objects.filter(activity_type=Activity.FAVORITE,
                                       user=user,
                                       question=question_id)
    if activity:
        activity.delete()
        user.profile.unotify_favorited(question)
    else:
        activity = Activity(activity_type=Activity.FAVORITE,
                            user=user,
                            question=question_id)
        activity.save()
        user.profile.notify_favorited(question)

    return HttpResponse(question.calculate_favorites())
Ejemplo n.º 17
0
 def test_activity_monthly_statistic(self):
     activity_one = Activity.objects.create(
         user=self.user,
         activity_type='L'
     )
     activity_two = Activity.objects.create(
         user=self.user,
         activity_type='L'
     )
     activity_three = Activity.objects.create(
         user=self.user,
         activity_type='L'
     )
     self.assertTrue(isinstance(activity_one, Activity))
     self.assertTrue(isinstance(activity_two, Activity))
     self.assertTrue(isinstance(activity_three, Activity))
     self.assertEqual(Activity.monthly_activity(self.user)[0],
                      '[{}]'.format(Activity.objects.all().count()))
     self.assertEqual(Activity.monthly_activity(self.other_user)[0], '0')
Ejemplo n.º 18
0
def vote(request):
    # 投票
    answer_id = request.POST['answer']
    answer = Answer.objects.get(pk=answer_id)
    vote = request.POST['vote']
    user = request.user

    # 投票主要是创建一个activity

    # 先查user在这个问题上之前的up/down的投票,并删掉
    activity = Activity.objects.filter(Q(activity_type=Activity.UP_VOTE)
                                       | Q(activity_type=Activity.DOWN_VOTE),
                                       user=user,
                                       answer=answer_id)
    if activity:
        activity.delete()

    # 创建一个新的投票activity
    if vote in [Activity.UP_VOTE, Activity.DOWN_VOTE]:
        activity = Activity(activity_type=vote, user=user, answer=answer_id)
        activity.save()
    return HttpResponse(answer.calculate_votes())
Ejemplo n.º 19
0
def profile(request, username):
    page_user = get_object_or_404(User, username=username)
    all_feeds = Feed.get_feeds().filter(user=page_user)
    paginator = Paginator(all_feeds, FEEDS_NUM_PAGES)
    feeds = paginator.page(1)
    from_feed = -1
    if feeds:  # pragma: no cover
        from_feed = feeds[0].id

    feeds_count = Feed.objects.filter(user=page_user).count()
    article_count = Article.objects.filter(create_user=page_user).count()
    article_comment_count = ArticleComment.objects.filter(
        user=page_user).count()
    question_count = Question.objects.filter(user=page_user).count()
    answer_count = Answer.objects.filter(user=page_user).count()
    activity_count = Activity.objects.filter(user=page_user).count()
    messages_count = Message.objects.filter(
        Q(from_user=page_user) | Q(user=page_user)).count()
    data, datepoints = Activity.daily_activity(page_user)
    data = {
        'page_user': page_user,
        'feeds_count': feeds_count,
        'article_count': article_count,
        'article_comment_count': article_comment_count,
        'question_count': question_count,
        'global_interactions': activity_count + article_comment_count + answer_count + messages_count,  # noqa: E501
        'answer_count': answer_count,
        'bar_data': [
            feeds_count, article_count, article_comment_count, question_count,
            answer_count, activity_count],
        'bar_labels': json.dumps('["Feeds", "Articles", "Comments", "Questions", "Answers", "Activities"]'),  # noqa: E501
        'line_labels': datepoints,
        'line_data': data,
        'feeds': feeds,
        'from_feed': from_feed,
        'page': 1
        }
    return render(request, 'core/profile.html', data)
Ejemplo n.º 20
0
    def post(self, request):
        if request.is_ajax():
            source = request.POST['source']
            # Handle Empty Source Case
            source_empty_check(source)
            print source
            lang = request.POST['lang']
            # Handle Invalid Language Case
            lang_valid_check(lang)
            proxy = callme.Proxy(
                server_id='fooserver',
                amqp_host='salamander.rmq.cloudamqp.com',
                amqp_vhost='spwlpmyp',
                amqp_user='******',
                amqp_password='******')

            resp = proxy.enveloppe_extract(source)

            model = Algorithm
            #source = source    + '\n' + resp['error_msg']
            run_rank = model.objects.filter(
                score__lte=int(resp['score'])).order_by('rank')
            if len(run_rank) > 0:
                rankobj = run_rank.last()
                rank = rankobj.rank + 1

            else:
                rank = 1

            run_rank_low = model.objects.filter(score__gt=int(resp['score']))
            if len(run_rank_low) > 0:
                for i in run_rank_low:
                    i.rank += 1
                    i.save()

            else:
                pass

            b = Algorithm(username=request.user.username,
                          user=request.user,
                          rank=rank,
                          score=resp['score'],
                          time=resp['duration'],
                          source_code=source,
                          cpu=18)
            b.save()
            job_post = u'{0} has sent a job score: {1} rank: {2} :'.format(
                request.user.username, resp['score'], rank)
            cop_resp = resp
            resp = model.objects.all().order_by('rank')
            values = resp.values('id')

            for ind, item in enumerate(values):
                if (item['id']) == b.id:
                    paging = divmod(ind, 5)[0]

            print(cop_resp)
            if cop_resp['error_msg'] == 'None':
                r = {'compile_status': 'OK', 'run_status_status': 'OK'}
            else:
                r = {
                    'compile_status': 'OK',
                    'run_status_status': 'ERROR',
                    'run_status_error': cop_resp['error_msg']
                }

            feed = Feed(user=request.user,
                        post=job_post,
                        job_link='/leaderboard?q=foo&flop=flip&page=' +
                        str(paging + 1))
            feed.save()
            like = Activity(activity_type=Activity.RUN_PROCESSED,
                            feed=feed.pk,
                            user=request.user)
            like.save()

            user = request.user
            user.profile.notify_liked_bis(feed)

            import json
            #r['compile_status'] = 'OK'
            #r['run_status']['status'] = 'AC'
            #r['run_status']['time_used'] = '12'
            #r['run_status']['memory_used'] = '12'
            #r['run_status']['output_html'] = "dededed"
            #r['run_status']['stderr'] =  "toto"

            #r   = json.dumps(r)
            #return HttpResponse(r, content_type="application/json")
            return JsonResponse(r, safe=False)
            #return HttpResponse(render(request, 'hackIDE/index.html'))

        else:
            print('error')
            return HttpResponseForbidden()
Ejemplo n.º 21
0
    def execute_upload(self,request):
                import uuid
                form = self.form_class(request.POST, request.FILES)
                print request.FILES['file']
                if  form.is_valid():
                    print request.FILES['file']
                    resp = self.handle_uploaded_file(request.FILES['file'])
                    if resp['score'] < 100 :
                        button_type = 'btn-warning'
                    else:
                        button_type = 'btn-success'
                    self.uuid_index  = str(uuid.uuid4())


                    resp['score']= random.randint(1, 100)

                    model = Algorithm

                    run_rank = model.objects.filter(rating__gt=int(resp['score'])).order_by('ranking')
                    if len(run_rank) > 0:
                        rankobj = run_rank.last()
                        rank = rankobj.ranking + 1

                    else:
                        rank = 1

                    run_rank_low = model.objects.filter(rating__lte=int(resp['score']))
                    if len(run_rank_low) > 0 :
                        for i in run_rank_low:
                            i.ranking += 1
                            i.save()

                    else:
                        pass

                    
                   

                    b = Algorithm(run_id= self.uuid_index, name=request.user.username, user=request.user, ranking = rank, rating=resp['score'], button = button_type, time= resp['duration'], cpu=18)
                    b.save()
                    job_post = u'{0} has sent a job score: {1} rank: {2} :'.format(request.user.username,resp['score'], rank)
                    
                    resp = model.objects.all().order_by('ranking')
                    values = resp.values('run_id')
                    
                    for ind, item  in enumerate(values) :
                        if (item['run_id']) == self.uuid_index :
                            paging =  divmod(ind, 5)[0]

                    feed = Feed(user=request.user, post=job_post, job_link='/leaderboard?q=foo&flop=flip&page='+str(paging+1))
                    feed.save()


                    #request.user.profile.notify_job_done(b)      
                    

                    like = Activity(activity_type=Activity.RUN_PROCESSED, feed=feed.pk, user=request.user)
                    like.save()

                    user = request.user
                    user.profile.notify_liked_bis(feed)
                    

                    return paging
Ejemplo n.º 22
0
    def execute_upload(self,request):
                import uuid
                form = self.form_class(request.POST, request.FILES)

                if  form.is_valid():
                    #resp = self.handle_uploaded_file(request.FILES['file'])
                    with open('uploaded_custom.py', 'wb+') as destination:
                        for chunk in request.FILES['file'].chunks():
                            destination.write(chunk)
                        destination.close()

                    proxy = callme.Proxy(server_id='fooserver2',amqp_host='amqp://*****:*****@37.187.117.106/echopen1', timeout=3600)
               
                    resp = proxy.denoise(open('uploaded_custom.py', 'rb').read())

                    self.uuid_index  = str(uuid.uuid4())

                    model = Algorithm

                    run_rank = model.objects.filter(rating__gt=int(resp['score'])).order_by('ranking')
                    if len(run_rank) > 0:
                        rankobj = run_rank.last()
                        rank = rankobj.ranking + 1

                    else:
                        rank = 1
                     
                    run_rank_low = model.objects.filter(rating__lte=int(resp['score']))
                    if len(run_rank_low) > 0 :
                        for i in run_rank_low:
                            i.ranking += 1
                            i.save()

                    else:
                        pass
                    
                                  
                    b = Algorithm(run_id= self.uuid_index, name=request.user.username, user=request.user, ranking = rank, rating=resp['score'], button = button_type, time= resp['duration'], source_code=source, cpu=18)
                    b.save()
                    job_post = u'{0} has sent a job score: {1} rank: {2} :'.format(request.user.username,resp['score'], rank)
                    
                    resp = model.objects.all().order_by('ranking')
                    values = resp.values('run_id')
                    
                    for ind, item  in enumerate(values) :
                        if (item['run_id']) == self.uuid_index :
                            paging =  divmod(ind, 5)[0]

                    feed = Feed(user=request.user, post=job_post, job_link='/leaderboard?q=foo&flop=flip&page='+str(paging+1))
                    feed.save()


                    #request.user.profile.notify_job_done(b)      
                    

                    like = Activity(activity_type=Activity.RUN_PROCESSED, feed=feed.pk, user=request.user)
                    like.save()

                    user = request.user
                    user.profile.notify_liked_bis(feed)
                    

                    return paging
Ejemplo n.º 23
0
 def setUp(self):
     # creates a fictif user
     u = User()
     u.username = "******"
     u.password = "******"
     u.save()
     
     # create an activity related to that user
     activity = Activity()
     activity.user = u
     activity.activity_type = "Favorite"
     activity.date = timezone.now()
     
     # check we can save it to the database
     activity.save()
     
     # creates two fictif users
     u1 = User()
     u1.username = "******"
     u1.password = "******"
     u1.save()
     u2 = User()
     u2.username = "******"
     u2.password = "******"
     u2.save()
     
     # create one notification for each type of notification
     # Liked
     notifL = Notification()
     notifL.from_user = u1
     notifL.to_user = u2
     notifL.date = timezone.now()
     notifL.notification_type = 'Liked' # this is a like
     # save it to the database
     notifL.save()
     # Commented
     notifC = Notification()
     notifC.from_user = u1
     notifC.to_user = u2
     notifC.date = timezone.now()
     notifC.notification_type = 'Commented'
     # save it to the database
     notifC.save()
     # Favorited
     notifF = Notification()
     notifF.from_user = u1
     notifF.to_user = u2
     notifF.date = timezone.now()
     notifF.notification_type = 'Favorited'
     # save it to the database
     notifF.save()
     # Answered
     notifA = Notification()
     notifA.from_user = u1
     notifA.to_user = u2
     notifA.date = timezone.now()
     notifA.notification_type = 'Answered'
     # save it to the database
     notifA.save()
     # Accepted answer
     notifW = Notification()
     notifW.from_user = u1
     notifW.to_user = u2
     notifW.date = timezone.now()
     notifW.notification_type = 'Accepted Answer'
     # save it to the database
     notifW.save()
     # Edited article
     notifE = Notification()
     notifE.from_user = u1
     notifE.to_user = u2
     notifE.date = timezone.now()
     notifE.notification_type = 'Edited Article'
     # save it to the database
     notifE.save()
     # Also commented
     notifS = Notification()
     notifS.from_user = u1
     notifS.to_user = u2
     notifS.date = timezone.now()
     notifS.notification_type = 'Also Commented'
     # save it to the database
     notifS.save()
Ejemplo n.º 24
0
    def execute_upload(self, request):
        import uuid
        form = self.form_class(request.POST, request.FILES)
        print request.FILES['file']
        if form.is_valid():
            print request.FILES['file']
            resp = self.handle_uploaded_file(request.FILES['file'])
            if resp['score'] < 100:
                button_type = 'btn-warning'
            else:
                button_type = 'btn-success'
            self.uuid_index = str(uuid.uuid4())

            resp['score'] = random.randint(1, 100)

            model = Algorithm

            run_rank = model.objects.filter(
                rating__gt=int(resp['score'])).order_by('ranking')
            if len(run_rank) > 0:
                rankobj = run_rank.last()
                rank = rankobj.ranking + 1

            else:
                rank = 1

            run_rank_low = model.objects.filter(rating__lte=int(resp['score']))
            if len(run_rank_low) > 0:
                for i in run_rank_low:
                    i.ranking += 1
                    i.save()

            else:
                pass

            b = Algorithm(run_id=self.uuid_index,
                          name=request.user.username,
                          user=request.user,
                          ranking=rank,
                          rating=resp['score'],
                          button=button_type,
                          time=resp['duration'],
                          cpu=18)
            b.save()
            job_post = u'{0} has sent a job score: {1} rank: {2} :'.format(
                request.user.username, resp['score'], rank)

            resp = model.objects.all().order_by('ranking')
            values = resp.values('run_id')

            for ind, item in enumerate(values):
                if (item['run_id']) == self.uuid_index:
                    paging = divmod(ind, 5)[0]

            feed = Feed(user=request.user,
                        post=job_post,
                        job_link='/leaderboard?q=foo&flop=flip&page=' +
                        str(paging + 1))
            feed.save()

            #request.user.profile.notify_job_done(b)

            like = Activity(activity_type=Activity.RUN_PROCESSED,
                            feed=feed.pk,
                            user=request.user)
            like.save()

            user = request.user
            user.profile.notify_liked_bis(feed)

            return paging
Ejemplo n.º 25
0
    def setUp(self):
        # creates a fictif user
        u = User()
        u.username = "******"
        u.password = "******"
        u.save()

        # create an activity related to that user
        activity = Activity()
        activity.user = u
        activity.activity_type = "Favorite"
        activity.date = timezone.now()

        # check we can save it to the database
        activity.save()

        # creates two fictif users
        u1 = User()
        u1.username = "******"
        u1.password = "******"
        u1.save()
        u2 = User()
        u2.username = "******"
        u2.password = "******"
        u2.save()

        # create one notification for each type of notification
        # Liked
        notifL = Notification()
        notifL.from_user = u1
        notifL.to_user = u2
        notifL.date = timezone.now()
        notifL.notification_type = 'Liked'  # this is a like
        # save it to the database
        notifL.save()
        # Commented
        notifC = Notification()
        notifC.from_user = u1
        notifC.to_user = u2
        notifC.date = timezone.now()
        notifC.notification_type = 'Commented'
        # save it to the database
        notifC.save()
        # Favorited
        notifF = Notification()
        notifF.from_user = u1
        notifF.to_user = u2
        notifF.date = timezone.now()
        notifF.notification_type = 'Favorited'
        # save it to the database
        notifF.save()
        # Answered
        notifA = Notification()
        notifA.from_user = u1
        notifA.to_user = u2
        notifA.date = timezone.now()
        notifA.notification_type = 'Answered'
        # save it to the database
        notifA.save()
        # Accepted answer
        notifW = Notification()
        notifW.from_user = u1
        notifW.to_user = u2
        notifW.date = timezone.now()
        notifW.notification_type = 'Accepted Answer'
        # save it to the database
        notifW.save()
        # Edited article
        notifE = Notification()
        notifE.from_user = u1
        notifE.to_user = u2
        notifE.date = timezone.now()
        notifE.notification_type = 'Edited Article'
        # save it to the database
        notifE.save()
        # Also commented
        notifS = Notification()
        notifS.from_user = u1
        notifS.to_user = u2
        notifS.date = timezone.now()
        notifS.notification_type = 'Also Commented'
        # save it to the database
        notifS.save()
Ejemplo n.º 26
0
    def post(self, request):
        if request.is_ajax():
            source = request.POST['source']
            # Handle Empty Source Case
            source_empty_check(source)
            print source
            lang = request.POST['lang']
            # Handle Invalid Language Case
            lang_valid_check(lang)

            proxy = callme.Proxy(server_id='fooserver2',
                                 amqp_host='localhost',
                                 timeout=3600)

            resp = proxy.enveloppe_extract(source)

            model = Algorithm

            run_rank = model.objects.filter(
                score__lte=int(resp['score'])).order_by('rank')
            if len(run_rank) > 0:
                rankobj = run_rank.last()
                rank = rankobj.rank + 1

            else:
                rank = 1

            run_rank_low = model.objects.filter(score__gt=int(resp['score']))
            if len(run_rank_low) > 0:
                for i in run_rank_low:
                    i.rank += 1
                    i.save()

            else:
                pass

            b = Algorithm(username=request.user.username,
                          user=request.user,
                          rank=rank,
                          score=resp['score'],
                          time=resp['duration'],
                          source_code=source,
                          cpu=18)
            b.save()
            job_post = u'{0} has sent a job score: {1} rank: {2} :'.format(
                request.user.username, resp['score'], rank)

            resp = model.objects.all().order_by('rank')
            values = resp.values('id')

            for ind, item in enumerate(values):
                if (item['id']) == b.id:
                    paging = divmod(ind, 5)[0]

            feed = Feed(user=request.user,
                        post=job_post,
                        job_link='/leaderboard?q=foo&flop=flip&page=' +
                        str(paging + 1))
            feed.save()

            #request.user.profile.notify_job_done(b)

            like = Activity(activity_type=Activity.RUN_PROCESSED,
                            feed=feed.pk,
                            user=request.user)
            like.save()

            user = request.user
            user.profile.notify_liked_bis(feed)

            print 'Notified'
            return HttpResponse(render(request, 'hackIDE/index.html'))

        else:
            print('error')
            return HttpResponseForbidden()