Beispiel #1
0
    def post(self, slug):
        if not current_user.is_authenticated():
            abort(401)
        obj = self.get_or_404(slug=slug)
        key = obj.__class__.__name__.lower()
        starred = getattr(current_user, 'starred_{0}s'.format(key))

        if obj not in starred:
            starred.append(obj)
            current_user.save()
            obj.on_star.send(obj)
            return marshal(current_user, user_fields), 201
        else:
            return marshal(current_user, user_fields)
Beispiel #2
0
 def get(self, id):
     '''Fetch metrics for an object given its ID'''
     if id == 'site':
         object_id = current_site.id
     else:
         try:
             object_id = ObjectId(id)
         except:
             object_id = id
     queryset = Metrics.objects(object_id=object_id).order_by('-date')
     args = parser.parse_args()
     if args.get('day'):
         metrics = [queryset(date=args['day']).first_or_404()]
     elif args.get('start'):
         end = args.get('end', date.today().isoformat())
         metrics = list(queryset(date__gte=args['start'], date__lte=end))
     else:
         metrics = [queryset.first_or_404()]
     if not args.get('cumulative') and metrics:
         # Turn cumulative data into daily counts based on the first
         # result. Might return negative values if there is a drop.
         reference_values = metrics[-1].values.copy()
         for metric in reversed(metrics):
             current_values = metric.values.copy()
             metric.values = {
                 name: count - reference_values[name]
                 for name, count in current_values.iteritems()
                 if name in reference_values
             }
             reference_values = current_values
     return marshal(metrics, metrics_fields)
Beispiel #3
0
 def get(self, id):
     """Fetch metrics for an object given its ID"""
     if id == "site":
         object_id = current_site.id
     else:
         try:
             object_id = ObjectId(id)
         except:
             object_id = id
     queryset = Metrics.objects(object_id=object_id).order_by("-date")
     args = parser.parse_args()
     if args.get("day"):
         metrics = [queryset(date=args["day"]).first_or_404()]
     elif args.get("start"):
         end = args.get("end", date.today().isoformat())
         metrics = list(queryset(date__gte=args["start"], date__lte=end))
     else:
         metrics = [queryset.first_or_404()]
     if not args.get("cumulative") and metrics:
         # Turn cumulative data into daily counts based on the first
         # result. Might return negative values if there is a drop.
         reference_values = metrics[-1].values.copy()
         for metric in reversed(metrics):
             current_values = metric.values.copy()
             metric.values = {
                 name: count - reference_values[name]
                 for name, count in current_values.iteritems()
                 if name in reference_values
             }
             reference_values = current_values
     return marshal(metrics, metrics_fields)
Beispiel #4
0
 def delete(self, slug):
     if not current_user.is_authenticated():
         abort(401)
     obj = self.get_or_404(slug=slug)
     key = obj.__class__.__name__.lower()
     starred = getattr(current_user, 'starred_{0}s'.format(key))
     starred.remove(obj)
     current_user.save()
     obj.on_unstar.send(obj)
     return marshal(current_user, user_fields), 204
Beispiel #5
0
 def post(self, slug):
     dataset = Dataset.objects.get_or_404(slug=slug)
     form = ResourceForm(request.form, csrf_enabled=False)
     if not form.validate():
         return {'errors': form.errors}, 400
     resource = Resource()
     form.populate_obj(resource)
     dataset.resources.append(resource)
     dataset.save()
     return marshal(resource, resource_fields), 201
Beispiel #6
0
 def put(self, slug, rid):
     dataset = Dataset.objects.get_or_404(slug=slug)
     resource = get_by(dataset.resources, 'id', UUID(rid))
     if not resource:
         abort(404)
     form = ResourceForm(request.form, instance=resource, csrf_enabled=False)
     if not form.validate():
         return {'errors': form.errors}, 400
     form.populate_obj(resource)
     dataset.save()
     return marshal(resource, resource_fields)
Beispiel #7
0
    def post(self, id):
        '''Create a new Issue for an object given its ID'''
        form = api.validate(IssueCreateForm)

        message = Message(content=form.comment.data, posted_by=current_user.id)
        issue = self.model.objects.create(subject=id,
                                          type=form.type.data,
                                          user=current_user.id,
                                          discussion=[message])
        on_new_issue.send(issue)

        return marshal(issue, issue_fields), 201
Beispiel #8
0
    def post(self, id):
        '''Create a new Issue for an object given its ID'''
        form = api.validate(IssueCreateForm)

        message = Message(content=form.comment.data, posted_by=current_user.id)
        issue = self.model.objects.create(
            subject=id,
            type=form.type.data,
            user=current_user.id,
            discussion=[message]
        )
        on_new_issue.send(issue)

        return marshal(issue, issue_fields), 201
Beispiel #9
0
 def get(self, id, period=None, names=None):
     try:
         object_id = ObjectId(id)
     except:
         object_id = id
     queryset = Metrics.objects(object_id=object_id).order_by('-date')
     if period:
         period = parse_period(period)
         if isinstance(period, basestring):
             result = queryset(date=period).first_or_404()
         else:
             result = list(queryset(date__gte=period[0], date__lte=period[1]))
     else:
         result = queryset.first_or_404()
     return marshal(result, metrics_fields)
Beispiel #10
0
 def post(self, id):
     '''Add comment and optionnaly close an issue given its ID'''
     issue = Issue.objects.get_or_404(id=id)
     form = api.validate(IssueCommentForm)
     issue.discussion.append(Message(
         content=form.comment.data,
         posted_by=current_user.id
     ))
     close = form.close.data
     if close:
         issue.closed_by = current_user._get_current_object()
         issue.closed = datetime.now()
     issue.save()
     if close:
         on_issue_closed.send(issue)
     return marshal(issue, issue_fields), 200
Beispiel #11
0
 def post(self, id):
     '''Add comment and optionnaly close an issue given its ID'''
     issue = Issue.objects.get_or_404(id=id)
     form = api.validate(IssueCommentForm)
     message = Message(content=form.comment.data, posted_by=current_user.id)
     issue.discussion.append(message)
     close = form.close.data
     if close:
         issue.closed_by = current_user._get_current_object()
         issue.closed = datetime.now()
     issue.save()
     if close:
         on_issue_closed.send(issue, message=message)
     else:
         on_new_issue_comment.send(issue, message=message)
     return marshal(issue, issue_fields), 200
Beispiel #12
0
 def get(self, id):
     """Fetch metrics for an object given its ID"""
     if id == "site":
         object_id = current_site.id
     else:
         try:
             object_id = ObjectId(id)
         except:
             object_id = id
     queryset = Metrics.objects(object_id=object_id).order_by("-date")
     args = parser.parse_args()
     if args.get("day"):
         result = [queryset(date=args["day"]).first_or_404()]
     elif args.get("start"):
         end = args.get("end", date.today().isoformat())
         result = list(queryset(date__gte=args["start"], date__lte=end))
     else:
         result = [queryset.first_or_404()]
     return marshal(result, metrics_fields)
Beispiel #13
0
 def get(self, id):
     '''Fetch metrics for an object given its ID'''
     if id == 'site':
         object_id = current_site.id
     else:
         try:
             object_id = ObjectId(id)
         except:
             object_id = id
     queryset = Metrics.objects(object_id=object_id).order_by('-date')
     args = parser.parse_args()
     if args.get('day'):
         result = [queryset(date=args['day']).first_or_404()]
     elif args.get('start'):
         end = args.get('end', date.today().isoformat())
         result = list(queryset(date__gte=args['start'], date__lte=end))
     else:
         result = [queryset.first_or_404()]
     return marshal(result, metrics_fields)
Beispiel #14
0
 def get(self, id):
     '''Fetch metrics for an object given its ID'''
     if id == 'site':
         object_id = current_site.id
     else:
         try:
             object_id = ObjectId(id)
         except:
             object_id = id
     queryset = Metrics.objects(object_id=object_id).order_by('-date')
     args = parser.parse_args()
     if args.get('day'):
         result = [queryset(date=args['day']).first_or_404()]
     elif args.get('start'):
         end = args.get('end', date.today().isoformat())
         result = list(queryset(date__gte=args['start'], date__lte=end))
     else:
         result = [queryset.first_or_404()]
     return marshal(result, metrics_fields)
Beispiel #15
0
 def get(self, id):
     '''Get an issue given its ID'''
     issue = Issue.objects.get_or_404(id=id)
     return marshal(issue, issue_fields)
Beispiel #16
0
 def get(self, id):
     '''List all Issues for an object given its ID'''
     issues = self.model.objects(subject=id)
     if not request.args.get('closed'):
         issues = issues(closed=None)
     return marshal(list(issues), issue_fields)
Beispiel #17
0
 def post(self, slug):
     reuse = self.get_or_404(slug=slug)
     reuse.featured = True
     reuse.save()
     return marshal(reuse, reuse_fields)
Beispiel #18
0
 def get(self, id):
     '''Get an issue given its ID'''
     issue = Issue.objects.get_or_404(id=id)
     return marshal(issue, issue_fields)
Beispiel #19
0
 def delete(self, slug):
     reuse = self.get_or_404(slug=slug)
     reuse.featured = False
     reuse.save()
     return marshal(reuse, reuse_fields)
Beispiel #20
0
 def delete(self, slug):
     dataset = self.get_or_404(slug=slug)
     dataset.featured = False
     dataset.save()
     return marshal(dataset, dataset_fields)
Beispiel #21
0
 def post(self, slug):
     dataset = self.get_or_404(slug=slug)
     dataset.featured = True
     dataset.save()
     return marshal(dataset, dataset_fields)