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')
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)
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)
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)
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])
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))))
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])
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')
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')
def create_user(request): user_id = util.firebase_get_user_id(request.META['HTTP_AUTHORIZATION']) util.create_user(user_id) return HttpResponse('Ok')
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")
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)
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)
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')
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))