예제 #1
0
def update_user(request, user_id):
    user_id = util.firebase_get_user_id(request.META['HTTP_AUTHORIZATION'])
    try:
        user = get_object_or_404(User, user_id=user_id)
    except User.DoesNotExist:
        return Http404("User not found.")
    return HttpResponse('Ok')
예제 #2
0
def get_notif(request):
    user_id = util.firebase_get_user_id(request.META['HTTP_AUTHORIZATION'])
    result = []
    for n in Notif.objects.filter(user_id=user_id, send=False):
        result.append(n.message)
        Notif.objects.filter(id=n.id).update(send=True)
    return JsonResponse(result)
예제 #3
0
def get_applets(request):
    user_id = util.firebase_get_user_id(request.META['HTTP_AUTHORIZATION'])
    result = []
    for app in Applet.objects.filter(user_id=user_id):
        temp = applet_to_json(app)
        result.append(temp)
    return JsonResponse(result)
예제 #4
0
def get_applets_by_services(request, service):
    user_id = util.firebase_get_user_id(request.META['HTTP_AUTHORIZATION'])
    result = [
        applet_to_json(a)
        for a in Applet.objects.filter(user_id=user_id, action_service=service)
    ]
    for v in Applet.objects.filter(user_id=user_id, reaction_service=service):
        result.append(applet_to_json(v))
    return JsonResponse(result)
예제 #5
0
def get_applet_by_action(request, service, action):
    result = []
    user_id = util.firebase_get_user_id(request.META['HTTP_AUTHORIZATION'])
    for app in Applet.objects.filter(user_id=user_id):
        if (app.action_service == service
                and app.action == action) or (app.reaction_service == service
                                              and app.reaction == action):
            result.append(app)
    return JsonResponse([applet_to_json(a) for a in result])
예제 #6
0
def set_applet(request, id):

    data = request_to_json(request)

    if data['action']['service'] == settings.SERVICE_NAME[3]:
        for p in data['action']['param']:
            if p['name'] == 'Currency':
                if not p['value'] in settings.CURRENCY_LIST:
                    return HttpResponse('Currency is not valid')

    user_id = util.firebase_get_user_id(request.META['HTTP_AUTHORIZATION'])

    def fill_applet(param):
        p = ParamApplet()
        p.name = param['name']
        p.type = False if param['type'] == 'int' else True
        p.value = param['value']
        p.id_applet = app.id
        return p

    with transaction.atomic():
        app = Applet.objects.select_for_update().get(user_id=user_id,
                                                     id_applet=int(id))
        app.action_service = data['action']['service']
        app.action = data['action']['action']
        app.reaction_service = data['reaction']['service']
        app.reaction = data['reaction']['reaction']
        app.data = None
        app.save()

    for param in ParamApplet.objects.filter(applet_id=app.id):
        param.delete()

    for param in data['action']['param']:
        p = fill_applet(param)
        p.side = True
        p.applet_id = app.id
        p.save()

    for param in data['reaction']['param']:
        p = fill_applet(param)
        p.side = False
        p.applet_id = app.id
        p.save()

    return JsonResponse(
        applet_to_json(Applet.objects.get(user_id=user_id, id_applet=int(id))))
예제 #7
0
def search_applets(request):
    if not request.GET['stringTosearch']:
        return HttpResponse('Ko, need string search')
    user_id = util.firebase_get_user_id(request.META['HTTP_AUTHORIZATION'])
    applets = []
    for s in Applet.objects.filter(
            action_service__icontains=request.GET['stringTosearch'],
            user_id=user_id):
        applets.append(s)
    for s in Applet.objects.filter(
            reaction_service__icontains=request.GET['stringTosearch'],
            user_id=user_id):
        applets.append(s)
    for s in Applet.objects.filter(
            action__icontains=request.GET['stringTosearch'], user_id=user_id):
        applets.append(s)
    for s in Applet.objects.filter(
            reaction__icontains=request.GET['stringTosearch'],
            user_id=user_id):
        applets.append(s)
    return JsonResponse([applet_to_json(s) for s in applets])
예제 #8
0
def activate_applet(request, id):
    def get_service(name):
        tab = [Github, Intra, None, None, None, Google, None, None]
        return tab[settings.SERVICE_NAME.index(name)]

    user_id = util.firebase_get_user_id(request.META['HTTP_AUTHORIZATION'])
    model_action = get_service(
        Applet.objects.get(user_id=user_id, id_applet=id).action_service)
    model_reaction = get_service(
        Applet.objects.get(user_id=user_id, id_applet=id).reaction_service)
    if model_action and not model_action.objects.get(user_id=user_id).token:
        return HttpResponse(
            'You need to enable ' +
            Applet.objects.get(user_id=user_id, id_applet=id).action_service,
            status=303)
    if model_reaction and not model_reaction.objects.get(
            user_id=user_id).token:
        return HttpResponse(
            'You need to enable ' +
            Applet.objects.get(user_id=user_id, id_applet=id).reaction_service,
            status=303)
    Applet.objects.filter(user_id=user_id, id_applet=id).update(enable=True)
    return HttpResponse('Ok')
예제 #9
0
def sync_token(request, service):
    user_id = util.firebase_get_user_id(request.META['HTTP_AUTHORIZATION'])
    if service == settings.SERVICE_NAME[0]:
        if request.POST.get('code'):
            param = {
                'client_id': settings.GITHUB_ID,
                "client_secret": settings.GITHUB_SECRET,
                "code": request.POST.get('code')
            }
            r = requests.post('https://github.com/login/oauth/access_token',
                              param)
            Github.objects.filter(user_id=user_id).update(
                token=r.text.split('&')[0].split('=')[1])
            return HttpResponse('Ok')
        Github.objects.filter(user_id=user_id).update(
            token=request.POST.get('token'),
            refresh=request.POST.get('refresh'))
    elif service == settings.SERVICE_NAME[1]:
        j = json.loads(
            util.request_create(request.POST.get('token'),
                                settings.SERVICE_INTRA +
                                'v1/intra/marks').text)
        print(j, file=stderr)
        if j['data'] == None:
            return HttpResponse('Token Not Valid', status=303)
        Intra.objects.filter(user_id=user_id).update(
            token=request.POST.get('token'),
            refresh=request.POST.get('refresh'))
    elif service == settings.SERVICE_NAME[2]:
        Slack.objects.filter(user_id=user_id).update(
            token=request.POST.get('token'),
            refresh=request.POST.get('refresh'))
    elif service == settings.SERVICE_NAME[5]:
        Google.objects.filter(user_id=user_id).update(
            token=request.POST.get('token'),
            refresh=request.POST.get('refresh'))
    return HttpResponse('Ok')
예제 #10
0
def create_user(request):
    user_id = util.firebase_get_user_id(request.META['HTTP_AUTHORIZATION'])
    util.create_user(user_id)
    return HttpResponse('Ok')
예제 #11
0
def desactivate_service(request, service):
    user_id = util.firebase_get_user_id(request.META['HTTP_AUTHORIZATION'])
    Service.objects.filter(name=service, user_id=user_id).update(enable=False)
    Applet.objects.filter(user_id=user_id,
                          action_service=service).update(enable=False)
    return HttpResponse("Ok")
예제 #12
0
def get_one_service(request, service):
    user_id = util.firebase_get_user_id(request.META['HTTP_AUTHORIZATION'])
    response = service_to_json(settings.SERVICE_NAME.index(service), user_id)
    return JsonResponse(response)
예제 #13
0
def get_services(request):
    user_id = util.firebase_get_user_id(request.META['HTTP_AUTHORIZATION'])
    response = [service_to_json(i, user_id) for i in range(8)]
    return JsonResponse(response)
예제 #14
0
def desactivate_applet(request, id):
    user_id = util.firebase_get_user_id(request.META['HTTP_AUTHORIZATION'])
    Applet.objects.filter(user_id=user_id, id_applet=id).update(enable=False)
    return HttpResponse('Ok')
예제 #15
0
def get_applet(request, id):
    user_id = util.firebase_get_user_id(request.META['HTTP_AUTHORIZATION'])
    app = Applet.objects.filter(user_id=user_id, id_applet=id).get()
    return JsonResponse(applet_to_json(app))