Esempio n. 1
0
 def chpasswd(self, pk):
     res = clib.get_obj(self.serializer, **{'pk': pk})
     if res.get('error'):
         return clib.jsonerror(res['error'])
     user = res['object']
     try:
         user.set_password(self.request.data['password'])
         user.save()
         return clib.jsonsuccess('Password has been changed')
     except KeyError:
         return clib.jsonerror('Could not change password')
Esempio n. 2
0
 def add(self, **kwargs):
     try:
         # TODO. MUST BE ADRESSED IN API 3.1.
         try:
             data = self.request.data.dicts[0] or self.request.data.dicts[1]
         except:
             data = self.request.data
         if kwargs:
             data.update(kwargs)
         srl = self.serializer(data=data)
         if srl.is_valid():
             srl.save()
             return jsonresult(srl.data)
         return jsonerror(srl.errors)
     except ValidationError as e:
         return jsonerror('Error: {}'.format(e))
Esempio n. 3
0
 def upd(self, **kwargs):
     try:
         res = get_obj(self.serializer, **kwargs)
         if res.get('error'):
             return jsonerror(res['error'])
         srl = self.serializer(res['object'],
                               data=self.request.data,
                               partial=True)
         if srl.is_valid():
             srl.save()
             return jsonresult(srl.data)
         return jsonerror(''.join([
             ','.join([k + '-' + ''.join(v)])
             for k, v in srl.errors.items()
         ]))
     except ValidationError as e:
         return jsonerror('Error: {}'.format(e))
Esempio n. 4
0
 def login(self, request, username, password):
     user = authenticate(username=username, password=password)
     if user:
         login(request, user)
         token, _ = Token.objects.get_or_create(user=user)
         data = self.data(pk=user.id)
         data['result']['token'] = token.key
         return data
     return clib.jsonerror('Invalid credentials')
Esempio n. 5
0
 def add(self, **kwargs):
     try:
         # TODO. MUST BE ADRESSED IN API 3.1.
         try:
             data = self.request.data.dicts[0] or self.request.data.dicts[1]
         except:
             data = self.request.data
         data['content'] = self.request.data['content']
         data['content_type'] = self.request.data['content'].content_type
         if kwargs:
             data.update(kwargs)
         srl = self.serializer(data=data)
         # TODO. Remove save method and use "create" instead.
         if srl.is_valid():
             srl.save()
             return clib.jsonresult(srl.data)
         return clib.jsonerror(srl.errors)
     except ValidationError as e:
         return clib.jsonerror('Error: {}'.format(e))
Esempio n. 6
0
 def destroy(self, request, pk=None):
     uid = request.user.pk
     um = usr.UsersManager(request)
     user = um.data(pk=uid)
     node = self.manager.data(pk=pk)
     if not clib.check_perm(node['result'], user['result'], co.WRITE):
         return Response(
             data=clib.jsonerror('You do not have write permissions'),
             status=status.HTTP_401_UNAUTHORIZED)
     return Response(data=self.manager.rm(pk))
Esempio n. 7
0
 def create(self, request):
     uid = request.user.pk
     um = usr.UsersManager(request)
     gres = um.groups(uid)
     if gres.get('error'):
         return Response(data=gres, status=status.HTTP_400_BAD_REQUEST)
     elif not gres.get('result'):
         err = clib.jsonerror(
             'User should be assigned to at least one group')
         return Response(data=err, status=status.HTTP_400_BAD_REQUEST)
     gids = [i['id'] for i in gres.get('result')]
     res = self.manager.add(uid=uid, gid=gids[0])
     if res.get('error'):
         return Response(data=res, status=status.HTTP_400_BAD_REQUEST)
     return Response(data=res)
Esempio n. 8
0
 def image(self, request, pk=None):
     uid = request.user.pk
     um = usr.UsersManager(request)
     user = um.data(pk=uid)
     node = self.manager.data(pk=pk)
     if not clib.check_perm(node['result'], user['result'], co.READ):
         return Response(
             data=clib.jsonerror('You do not have read permissions'),
             status=status.HTTP_401_UNAUTHORIZED)
     assets = self.manager.serializer.Meta.model
     asset = assets.objects.get(pk=pk)
     img = asset.content.thumbnail if request.GET.get(
         'thumb') else asset.content.get()
     response = StreamingHttpResponse(img, content_type=asset.content_type)
     response['Content-Disposition'] = 'inline;filename='
     response['Content-Type'] = asset.content_type
     return response
Esempio n. 9
0
 def groups(self, uid):
     try:
         user = self.serializer.Meta.model.objects.get(pk=uid)
     except Exception, e:
         return clib.jsonerror(str(e))
Esempio n. 10
0
 def rm_group(self, uid, gid):
     try:
         user = self.serializer.Meta.model.objects.get(pk=uid)
         user.groups.remove(int(gid))
     except Exception, e:
         return clib.jsonerror(str(e))
Esempio n. 11
0
 def rm(self, pk):
     res = get_obj(self.serializer, **{'pk': pk})
     if res.get('error'):
         return jsonerror(res['error'])
     res['object'].delete()
     return jsonsuccess('Object id:<%s> has been removed' % (pk, ))