Ejemplo n.º 1
0
def report_error():
    """ Accepts POST content containing an error message and emails it. """
    flask.request.User = users.token_to_object(flask.request, strict=False)
    report = flask.request.get_json().get('value', None)
    if report is None:
        raise utils.InvalidUsage("User error report 'value' must be a str!")
    utils.email_error_report(report)
    return utils.http_200
Ejemplo n.º 2
0
def private_router(action):
    """ The private version of the previous method. This one handles routes
    where we require, at a minimum, a user that is recognized by the API as a
    registered user. We also check to see if they're an admin. """

    # we need to be an admin to get into here
    if not flask.request.User.user.get('admin', False):
        return utils.http_403

    if action == 'new':
        r_obj = releaseObject()
        return flask.Response(json.dumps(r_obj.record,
                                         default=json_util.default),
                              status=200,
                              mimetype="application/json")

    # 3.) JSON is required below, so sanity check for it here:
    if flask.request.get_json() is None:
        err = ("The '%s' action requires valid JSON in the POST (or is not a "
               "valid endpoint)!")
        raise utils.InvalidUsage(err % action, 422)

    release_oid = flask.request.get_json().get('_id', None)
    if release_oid is None:
        raise utils.InvalidUsage('_id is required!', 422)

    r_obj = releaseObject(_id=release_oid['$oid'])

    if action == 'update':
        r_obj.update()
        return flask.Response(json.dumps(r_obj.record,
                                         default=json_util.default),
                              status=200,
                              mimetype="application/json")
    elif action == 'delete':
        return flask.Response(json.dumps(r_obj.delete().raw_result,
                                         default=json_util.default),
                              status=200,
                              mimetype="application/json")

    # if we're still here, throw an error, because obviously we've got POST data
    #   to some oddball/unknown endpoint...
    err = "'%s' method not allowed!" % action
    return flask.Response(err, status=405)
Ejemplo n.º 3
0
    def load(self):
        """ Load a release record. """
        if getattr(self, '_id', None) is None:
            self.new()

        self.record = self.mdb.find_one({'_id': self._id})
        if self.record is None:
            err = "Release OID '%s' not found!" % self._id
            raise utils.InvalidUsage(err, status_code=400)

        for key, value in self.data_model.items():
            setattr(self, key, self.record.get(key, None))
Ejemplo n.º 4
0
def get_data(resource=None):
    """ Retrieves various types of admin panel data. If the requester wants
	something we don't have, raise a 400. """

    if resource == 'user_data':
        return panel.get_user_data()
    elif resource == 'settlement_data':
        return panel.get_settlement_data()
    elif resource == 'logs':
        return panel.serialize_system_logs()

    raise utils.InvalidUsage("Resource '%s' does not exist!" % resource,
                             status_code=400)
Ejemplo n.º 5
0
    def new(self):
        """ Create a new release record. """

        platform = self.request.get('platform', None)
        if platform is None:
            raise utils.InvalidUsage(
                'Platform must be specified when creating a new release!',
                status_code=422)

        self.logger.info("Creating a new release for '%s'" % platform)
        self._id = self.mdb.insert({})

        self.created_on = datetime.now()
        self.created_by = flask.request.User._id
        self.platform = platform

        self.set_latest_version()
        self.save()
Ejemplo n.º 6
0
    def new(self):
        """ Creates a new webapp alert; initializes. """

        self.alert = request.json

        # sanity check the incoming
        for req_var in ['body', 'title']:
            if self.alert[req_var] is None:
                raise utils.InvalidUsage(
                    "Webapp Alert key '%s' cannot be None type!" % req_var)

        self.alert['sub_type'] = self.alert['type']
        self.alert['type'] = 'webapp_alert'
        self.alert['created_on'] = datetime.now()
        self.alert['created_by'] = ObjectId(self.alert['created_by'])
        self.alert['expired'] = False
        self.alert['release'] = API.config['VERSION']
        self.alert['remote_ip'] = request.remote_addr

        # finally, save it and return it
        self.alert['_id'] = utils.mdb.notifications.insert(self.alert)
Ejemplo n.º 7
0
    def get_random_names(self, count=100):
        """ Returns 'count' random names for each sex. This is meant primarily
        as a front-end helper, so it returns a JSON-like dict. It also raises
        a big, wordy error if you ask it for too many names. """

        m = copy(names.male)
        f = copy(names.female)

        m.extend(copy(names.neuter))
        f.extend(copy(names.neuter))

        male = set()
        female = set()

        for l in m,f:
            if count > len(l):
                raise utils.InvalidUsage('Cannot return more than %s random names!' % len(l))

        for i in [(male, m), (female, f)]:
            l, s = (i)
            while len(l) < count:
                l.add(random.choice(s))

        return {'M': sorted(list(male)), 'F': sorted(list(female))}
Ejemplo n.º 8
0
 def load(self):
     self.alert = utils.mdb.notifications.find_one({'_id': self._id})
     if self.alert is None:
         raise utils.InvalidUsage('Notification %s does not exist!' %
                                  (self._id),
                                  status_code=404)