コード例 #1
0
def create_query(request):
    """

    :param request:
    :return:
    """
    if request.method != 'POST':
        return json_response({
            'status': 'error',
            'message': 'Invalid request type'
        })

    form = QueryForm(request.POST)
    if form.is_valid():
        query = Query(**form.cleaned_data)
        query.user = request.user
        query.is_public = form.cleaned_data['is_public'] == u'True'
        query.save()
        return json_response({
            'status': 'success',
            'message': 'Query successfully created',
            'query': query.to_dict()
        })

    return json_response({
        'status': 'error',
        'message': 'Fill all required fields'
    })
コード例 #2
0
ファイル: views.py プロジェクト: Jaquedeveloper/tsa
def get_group_queries(request):
    """

    :param request:
    :return:
    """
    if request.user.account.group:
        group_queries = Query.objects.filter(
            user__account__group=request.user.account.group, is_public=True
        ).exclude(
            user=request.user
        ).order_by(
            '-date'
        )
        queries = [q.to_dict() for q in group_queries]

        rq = RunningQuery.objects.filter(user=request.user)
        running_query_id = None

        if rq.exists():
            rq = rq.first()
            running_query_id = rq.query.id

        return json_response({
            'queries': queries,
            'running_query_id': running_query_id
        })
    return json_response({'queries': None})
コード例 #3
0
ファイル: views.py プロジェクト: Jaquedeveloper/tsa
def create_query(request):
    """

    :param request:
    :return:
    """
    if request.method != 'POST':
        return json_response({
            'status': 'error',
            'message': 'Invalid request type'
        })

    form = QueryForm(request.POST)
    if form.is_valid():
        query = Query(**form.cleaned_data)
        query.user = request.user
        query.is_public = form.cleaned_data['is_public'] == u'True'
        query.save()
        return json_response({
            'status': 'success',
            'message': 'Query successfully created',
            'query': query.to_dict()
        })

    return json_response({
        'status': 'error',
        'message': 'Fill all required fields'
    })
コード例 #4
0
def get_query(request):
    """

    :param request:
    :return:
    """
    try:
        query_id = int(request.POST.get('query_id', ''))
    except ValueError:
        return json_response({
            'status': 'error',
            'message': 'Invalid query id format'
        })

    query = Query.objects.filter(pk=query_id, user=request.user)
    if not query.exists():
        query = Query.objects.filter(
            pk=query_id,
            user__account__group=request.user.account.group).filter(
                is_public=True)

    if not query.exists():
        return json_response({'status': 'error', 'message': 'Query not found'})

    query = query.first()

    return json_response({
        'status': 'success',
        'message': 'Query was found',
        'query': query.to_dict()
    })
コード例 #5
0
ファイル: views.py プロジェクト: Jaquedeveloper/tsa
def get_query(request):
    """

    :param request:
    :return:
    """
    try:
        query_id = int(request.POST.get('query_id', ''))
    except ValueError:
        return json_response({
            'status': 'error',
            'message': 'Invalid query id format'
        })

    query = Query.objects.filter(pk=query_id, user=request.user)
    if not query.exists():
        query = Query.objects.filter(
            pk=query_id,
            user__account__group=request.user.account.group
        ).filter(is_public=True)

    if not query.exists():
        return json_response({
            'status': 'error',
            'message': 'Query not found'
        })

    query = query.first()

    return json_response({
        'status': 'success',
        'message': 'Query was found',
        'query': query.to_dict()
    })
コード例 #6
0
ファイル: views.py プロジェクト: Jaquedeveloper/tsa
def stop_query(request):
    """

    :param request:
    :return:
    """
    rq = RunningQuery.objects.filter(user=request.user)
    if not rq.exists():
        return json_response({
            'status': 'error',
            'message': 'You have no running queries'
        })

    try:
        query_id = int(request.POST.get('query_id', ''))
    except ValueError:
        return json_response({
            'status': 'error',
            'message': 'Invalid query id format'
        })

    query = Query.objects.filter(user=request.user, pk=query_id)
    if not query.exists():
        query = Query.objects.filter(
            pk=query_id,
            user__account__group=request.user.account.group,
            is_public=True
        )

    if not query.exists():
        return json_response({
            'status': 'error',
            'message': 'Query not found'
        })

    query = query.first()

    rq = rq.filter(query=query)

    if not rq.exists():
        return json_response({
            'status': 'error',
            'message': 'This query was run not by you'
        })

    rq = rq.first()
    app.control.revoke(rq.task_id, terminate=True)
    rq.delete()
    return json_response({
        'status': 'success',
        'message': 'Query was stopped'
    })
コード例 #7
0
ファイル: views.py プロジェクト: Jaquedeveloper/tsa
def run_query(request):
    """

    :param request:
    :return:
    """
    rq = RunningQuery.objects.filter(user=request.user)
    if rq.exists() > 0:
        return json_response({
            'status': 'error',
            'message': "You can't run more than one query right now"
        })

    try:
        query_id = int(request.POST.get('query_id', ''))
    except ValueError:
        return json_response({
            'status': 'error',
            'message': 'Invalid query id format'
        })

    query = Query.objects.filter(user=request.user, pk=query_id)
    if not query.exists():
        query = Query.objects.filter(
            pk=query_id,
            user__account__group=request.user.account.group,
            is_public=True
        )

    if not query.exists():
        return json_response({
            'status': 'error',
            'message': 'Query not found'
        })

    query = query.first()

    task_id = tasks.get_tweets.delay(query_id, query.to_search_query_string()).id

    rq = RunningQuery()
    rq.user = request.user
    rq.query = query
    rq.task_id = task_id
    rq.save()

    return json_response({
        'status': 'success',
        'message': 'Query was run. Wait for results!'
    })
コード例 #8
0
ファイル: views.py プロジェクト: saed-alkaabi/tsa
def remove_from_group(request):
    if not request.user.account.is_group_admin:
        return json_response({'status': 'error'})

    try:
        user_id = int(request.POST.get('user_id', ''))
    except ValueError:
        return json_response({'status': 'error'})

    user = User.objects.filter(pk=user_id, account__group=request.user.account.group)
    if user.exists():
        user.account.group = None
        return json_response({'status': 'success'})

    return json_response({'status': 'error'})
コード例 #9
0
def run_query(request):
    """

    :param request:
    :return:
    """
    rq = RunningQuery.objects.filter(user=request.user)
    if rq.exists() > 0:
        return json_response({
            'status':
            'error',
            'message':
            "You can't run more than one query right now"
        })

    try:
        query_id = int(request.POST.get('query_id', ''))
    except ValueError:
        return json_response({
            'status': 'error',
            'message': 'Invalid query id format'
        })

    query = Query.objects.filter(user=request.user, pk=query_id)
    if not query.exists():
        query = Query.objects.filter(
            pk=query_id,
            user__account__group=request.user.account.group,
            is_public=True)

    if not query.exists():
        return json_response({'status': 'error', 'message': 'Query not found'})

    query = query.first()

    task_id = tasks.get_tweets.delay(query_id,
                                     query.to_search_query_string()).id

    rq = RunningQuery()
    rq.user = request.user
    rq.query = query
    rq.task_id = task_id
    rq.save()

    return json_response({
        'status': 'success',
        'message': 'Query was run. Wait for results!'
    })
コード例 #10
0
def delete_query(request):
    """

    :param request:
    :return:
    """
    try:
        query_id = int(request.POST.get('query_id', ''))
    except ValueError:
        return json_response({
            'status': 'error',
            'message': 'Invalid query id format'
        })

    if request.user.account.is_group_admin:
        query = Query.objects.filter(
            pk=query_id,
            user__account__group=request.user.account.group,
            is_public=True)
    else:
        query = Query.objects.filter(user=request.user, pk=query_id)

    if not query.exists():
        return json_response({
            'status':
            'error',
            'message':
            'You have no permissions to delete this query'
        })

    query = query.first()

    rq = RunningQuery.objects.filter(query=query)
    if rq.exists():
        return json_response({
            'status': 'error',
            'message': "You can't delete running query"
        })

    query.delete()
    rq.filter(user=request.user).delete()

    return json_response({
        'status': 'success',
        'message': 'Query was deleted!'
    })
コード例 #11
0
def stop_query(request):
    """

    :param request:
    :return:
    """
    rq = RunningQuery.objects.filter(user=request.user)
    if not rq.exists():
        return json_response({
            'status': 'error',
            'message': 'You have no running queries'
        })

    try:
        query_id = int(request.POST.get('query_id', ''))
    except ValueError:
        return json_response({
            'status': 'error',
            'message': 'Invalid query id format'
        })

    query = Query.objects.filter(user=request.user, pk=query_id)
    if not query.exists():
        query = Query.objects.filter(
            pk=query_id,
            user__account__group=request.user.account.group,
            is_public=True)

    if not query.exists():
        return json_response({'status': 'error', 'message': 'Query not found'})

    query = query.first()

    rq = rq.filter(query=query)

    if not rq.exists():
        return json_response({
            'status': 'error',
            'message': 'This query was run not by you'
        })

    rq = rq.first()
    app.control.revoke(rq.task_id, terminate=True)
    rq.delete()
    return json_response({'status': 'success', 'message': 'Query was stopped'})
コード例 #12
0
ファイル: views.py プロジェクト: Jaquedeveloper/tsa
def delete_query(request):
    """

    :param request:
    :return:
    """
    try:
        query_id = int(request.POST.get('query_id', ''))
    except ValueError:
        return json_response({
            'status': 'error',
            'message': 'Invalid query id format'
        })

    if request.user.account.is_group_admin:
        query = Query.objects.filter(
            pk=query_id,
            user__account__group=request.user.account.group,
            is_public=True
        )
    else:
        query = Query.objects.filter(user=request.user, pk=query_id)

    if not query.exists():
        return json_response({
            'status': 'error',
            'message': 'You have no permissions to delete this query'
        })

    query = query.first()

    rq = RunningQuery.objects.filter(query=query)
    if rq.exists():
        return json_response({
            'status': 'error',
            'message': "You can't delete running query"
        })

    query.delete()
    rq.filter(user=request.user).delete()

    return json_response({
        'status': 'success',
        'message': 'Query was deleted!'
    })
コード例 #13
0
def get_group_queries(request):
    """

    :param request:
    :return:
    """
    if request.user.account.group:
        group_queries = Query.objects.filter(
            user__account__group=request.user.account.group,
            is_public=True).exclude(user=request.user).order_by('-date')
        queries = [q.to_dict() for q in group_queries]

        rq = RunningQuery.objects.filter(user=request.user)
        running_query_id = None

        if rq.exists():
            rq = rq.first()
            running_query_id = rq.query.id

        return json_response({
            'queries': queries,
            'running_query_id': running_query_id
        })
    return json_response({'queries': None})
コード例 #14
0
def get_my_queries(request):
    """

    :param request:
    :return:
    """
    my_queries = Query.objects.filter(user=request.user).order_by('-date')
    queries = [q.to_dict() for q in my_queries]

    rq = RunningQuery.objects.filter(user=request.user)
    running_query_id = None

    if rq.exists():
        rq = rq.first()
        running_query_id = rq.query.id

    return json_response({
        'queries': queries,
        'running_query_id': running_query_id
    })
コード例 #15
0
ファイル: views.py プロジェクト: Jaquedeveloper/tsa
def get_my_queries(request):
    """

    :param request:
    :return:
    """
    my_queries = Query.objects.filter(user=request.user).order_by('-date')
    queries = [q.to_dict() for q in my_queries]

    rq = RunningQuery.objects.filter(user=request.user)
    running_query_id = None

    if rq.exists():
        rq = rq.first()
        running_query_id = rq.query.id

    return json_response({
        'queries': queries,
        'running_query_id': running_query_id
    })
コード例 #16
0
def edit_query(request):
    """

    :param request:
    :return:
    """
    if request.method != 'POST':
        return json_response({
            'status': 'error',
            'message': 'Invalid request type'
        })

    try:
        query_id = int(request.POST.get('id', ''))
    except ValueError:
        return json_response({
            'status': 'error',
            'message': 'Invalid query id format'
        })

    if request.user.account.is_group_admin:
        query = Query.objects.filter(
            pk=query_id,
            user__account__group=request.user.account.group,
            is_public=True)
    else:
        query = Query.objects.filter(pk=query_id, user=request.user)

    if not query.exists():
        return json_response({
            'status':
            'error',
            'message':
            'You have no permissions to edit this query'
        })

    rq = RunningQuery.objects.filter(query=query)
    if rq.exists():
        return json_response({
            'status': 'error',
            'message': "You can't edit running query"
        })

    query = query.first()

    query.title = request.POST.get('title')
    query.all_words = request.POST.get('all_words')
    query.phrase = request.POST.get('phrase')
    query.any_word = request.POST.get('any_word')
    query.none_of = request.POST.get('none_of')
    query.hashtags = request.POST.get('hashtags')
    query.users = request.POST.get('users')
    query.date_from = request.POST.get('date_from') or None
    query.date_to = request.POST.get('date_to') or None
    query.is_public = request.POST.get('is_public') or False

    query.save()

    return json_response({
        'status': 'success',
        'query': query.to_dict(),
        'edited': True,
        'message': 'Query successfully edited!'
    })
コード例 #17
0
def get_query_results(request):
    """

    :param request:
    :return:
    """
    rq = RunningQuery.objects.filter(user=request.user)
    if not rq.exists():
        try:
            query_id = int(request.POST.get('query_id', ''))
        except ValueError:
            return json_response({
                'status': 'error',
                'message': 'Invalid query id format'
            })
    else:
        rq = rq.first()
        query_id = rq.query.id

    query = Query.objects.filter(user=request.user, pk=query_id)
    if not query.exists():
        query = Query.objects.filter(
            pk=query_id,
            user__account__group=request.user.account.group,
            is_public=True)

    if not query.exists():
        return json_response({'status': 'error', 'message': 'Query not found'})

    query = query.first()

    tweets = Tweet.objects.filter(query=query).order_by('-date')

    if not tweets.exists():
        return json_response({
            'status': 'error',
            'message': 'No tweets to analyse'
        })

    tweets = [tweet.to_dict() for tweet in tweets]

    full_text = ''
    hashtags = dict()
    users = set()

    for tweet in tweets:
        full_text += tweet['text'] + ' '
        for ht in tweet['hashtags'].split():
            if ht not in hashtags:
                hashtags[ht] = 0
            hashtags[ht] += 1
        users.add(tweet['user'])

    tb = TextBlob(full_text)

    keywords = query.all_words + ' ' + query.any_word + ' ' + query.phrase + ' ' + query.users + ' ' + query.hashtags
    keywords = keywords.strip()

    word_counts = sorted(zip(tb.word_counts.values(), tb.word_counts.keys()),
                         reverse=True)
    word_counts = word_counts[:int(len(word_counts) * 0.05)]

    word_counts = [(c, w) for c, w in word_counts if len(w) >= 3]

    hashtag_counts = sorted(zip(hashtags.values(), hashtags.keys()),
                            reverse=True)

    analysis = {
        'word_counts': word_counts,
        'hashtags': hashtag_counts,
        'users': list(users),
        'keywords': keywords
    }

    return json_response({
        'status': 'success',
        'tweets': tweets,
        'analysis': analysis
    })
コード例 #18
0
ファイル: views.py プロジェクト: Jaquedeveloper/tsa
def get_query_results(request):
    """

    :param request:
    :return:
    """
    rq = RunningQuery.objects.filter(user=request.user)
    if not rq.exists():
        try:
            query_id = int(request.POST.get('query_id', ''))
        except ValueError:
            return json_response({
                'status': 'error',
                'message': 'Invalid query id format'
            })
    else:
        rq = rq.first()
        query_id = rq.query.id

    query = Query.objects.filter(user=request.user, pk=query_id)
    if not query.exists():
        query = Query.objects.filter(
            pk=query_id,
            user__account__group=request.user.account.group,
            is_public=True
        )

    if not query.exists():
        return json_response({
            'status': 'error',
            'message': 'Query not found'
        })

    query = query.first()

    tweets = Tweet.objects.filter(query=query).order_by('-date')

    if not tweets.exists():
        return json_response({
            'status': 'error',
            'message': 'No tweets to analyse'
        })

    tweets = [tweet.to_dict() for tweet in tweets]

    full_text = ''
    hashtags = dict()
    users = set()

    for tweet in tweets:
        full_text += tweet['text'] + ' '
        for ht in tweet['hashtags'].split():
            if ht not in hashtags:
                hashtags[ht] = 0
            hashtags[ht] += 1
        users.add(tweet['user'])

    tb = TextBlob(full_text)

    keywords = query.all_words + ' ' + query.any_word + ' ' + query.phrase + ' ' + query.users + ' ' + query.hashtags
    keywords = keywords.strip()

    word_counts = sorted(zip(tb.word_counts.values(), tb.word_counts.keys()), reverse=True)
    word_counts = word_counts[:int(len(word_counts) * 0.05)]

    word_counts = [(c, w) for c, w in word_counts if len(w) >= 3]

    hashtag_counts = sorted(zip(hashtags.values(), hashtags.keys()), reverse=True)

    analysis = {
        'word_counts': word_counts,
        'hashtags': hashtag_counts,
        'users': list(users),
        'keywords': keywords
    }

    return json_response({
        'status': 'success',
        'tweets': tweets,
        'analysis': analysis
    })
コード例 #19
0
ファイル: views.py プロジェクト: Jaquedeveloper/tsa
def edit_query(request):
    """

    :param request:
    :return:
    """
    if request.method != 'POST':
        return json_response({
            'status': 'error',
            'message': 'Invalid request type'
        })

    try:
        query_id = int(request.POST.get('id', ''))
    except ValueError:
        return json_response({
            'status': 'error',
            'message': 'Invalid query id format'
        })

    if request.user.account.is_group_admin:
        query = Query.objects.filter(
            pk=query_id,
            user__account__group=request.user.account.group,
            is_public=True
        )
    else:
        query = Query.objects.filter(pk=query_id, user=request.user)

    if not query.exists():
        return json_response({
            'status': 'error',
            'message': 'You have no permissions to edit this query'
        })

    rq = RunningQuery.objects.filter(query=query)
    if rq.exists():
        return json_response({
            'status': 'error',
            'message': "You can't edit running query"
        })

    query = query.first()

    query.title = request.POST.get('title')
    query.all_words = request.POST.get('all_words')
    query.phrase = request.POST.get('phrase')
    query.any_word = request.POST.get('any_word')
    query.none_of = request.POST.get('none_of')
    query.hashtags = request.POST.get('hashtags')
    query.users = request.POST.get('users')
    query.date_from = request.POST.get('date_from') or None
    query.date_to = request.POST.get('date_to') or None
    query.is_public = request.POST.get('is_public') or False

    query.save()

    return json_response({
        'status': 'success',
        'query': query.to_dict(),
        'edited': True,
        'message': 'Query successfully edited!'
    })