Esempio n. 1
0
File: views.py Progetto: rtnpro/faf
def new(request):
    if request.method == 'POST':
        form = NewReportForm(request.POST, request.FILES)
        if form.is_valid():
            db = pyfaf.storage.getDatabase()
            report = form.cleaned_data['file']['converted']

            if len(str(report)) > ureport.MAX_UREPORT_LENGTH:
                err = "uReport may only be {0} bytes long".format(ureport.MAX_UREPORT_LENGTH)
                if "application/json" in request.META.get("HTTP_ACCEPT"):
                    return HttpResponse(json.dumps({"error": err}),
                                        status=413, mimetype="application/json")

                return HttpResponse(err, status=413, mimetype="application/json")

            try:
                dbreport = ureport.is_known(report, db, return_report=True)
            except:
                dbreport = None

            known = bool(dbreport)
            spool_dir = pyfaf.config.get('Report.SpoolDirectory')
            fname = str(uuid.uuid4())
            with open(os.path.join(spool_dir, 'incoming', fname), 'w') as fil:
                fil.write(form.cleaned_data['file']['json'])

            if 'application/json' in request.META.get('HTTP_ACCEPT'):
                response = {'result': known }

                opsys_id = None
                opsys = db.session.query(OpSys).filter(OpSys.name == report["os"]["name"]).first()
                if opsys:
                    opsys_id = opsys.id

                solution = ureport.find_ureport_kb_solution(report, db, opsys_id=opsys_id)
                if solution is not None:
                    response['message'] = ("Your problem seems to be caused by {0}\n\n"
                                           "{1}".format(solution.cause, solution.note_text))
                    if solution.url:
                        response['message'] += ("\n\nYou can get more information at {0}"
                                                .format(solution.url))

                    response['solutions'] = [{'cause': solution.cause,
                                              'note':  solution.note_text,
                                              'url':   solution.url}]
                    response['result'] = True

                try:
                    if "component" in report:
                        component = ureport.get_component(report['component'], report['os'], db)
                    else:
                        component = ureport.guess_component(report['installed_package'], report['os'], db)

                    if component:
                        response['bthash'] = ureport.get_report_hash(report, component.name)[1]
                except:
                    # ToDo - log the exception somehow
                    pass

                if known:
                    site = RequestSite(request)
                    url = reverse('pyfaf.hub.reports.views.item', args=[dbreport.id])
                    parts = [{"reporter": "ABRT Server",
                              "value": "https://{0}{1}".format(site.domain, url),
                              "type": "url"}]

                    bugs = db.session.query(ReportRhbz).filter(ReportRhbz.report_id == dbreport.id).all()
                    for bug in bugs:
                        # ToDo: do not hardcode the URL
                        parts.append({"reporter": "Bugzilla",
                                      "value": "https://bugzilla.redhat.com/show_bug.cgi?id={0}".format(bug.rhbzbug_id),
                                      "type": "url"})

                    if not 'message' in response:
                        response['message'] = ''
                    else:
                        response['message'] += '\n\n'

                    response['message'] += "\n".join(p["value"] for p in parts if p["type"].lower() == "url")
                    response['reported_to'] = parts

                return HttpResponse(json.dumps(response),
                    status=202,
                    mimetype='application/json')

            return render_to_response('reports/success.html',
                {'report': report, 'known': known},
                context_instance=RequestContext(request))
        else:
            err = form.errors['file'][0]
            if 'application/json' in request.META.get('HTTP_ACCEPT'):
                response = {'error' : err}
                return HttpResponse(json.dumps(response),
                status=400, mimetype='application/json')

            return render_to_response('reports/new.html', {'form': form},
                context_instance=RequestContext(request))
    else:
        form = NewReportForm()

    return render_to_response('reports/new.html', {'form': form},
        context_instance=RequestContext(request))
Esempio n. 2
0
File: reports.py Progetto: abrt/faf
def new():
    form = NewReportForm()
    if request.method == "POST":
        try:
            if not form.validate() or form.file.name not in request.files:
                raise InvalidUsage("Invalid form data.", 400)
            raw_data = request.files[form.file.name].read()
            try:
                data = json.loads(raw_data)
            except Exception as ex: # pylint: disable=broad-except
                _save_invalid_ureport(db, raw_data, str(ex))
                raise InvalidUsage("Couldn't parse JSON data.", 400)

            try:
                ureport.validate(data)
            except Exception as exp: # pylint: disable=broad-except
                reporter = None
                if ("reporter" in data and
                        "name" in data["reporter"] and
                        "version" in data["reporter"]):
                    reporter = "{0} {1}".format(data["reporter"]["name"],
                                                data["reporter"]["version"])

                _save_invalid_ureport(db, json.dumps(data, indent=2),
                                      str(exp), reporter=reporter)

                if ("os" in data and
                        "name" in data["os"] and
                        data["os"]["name"] not in systems and
                        data["os"]["name"].lower() not in systems):
                    _save_unknown_opsys(db, data["os"])
                if str(exp) == 'uReport must contain affected package':
                    raise InvalidUsage(("Server is not accepting problems "
                                        "from unpackaged files."), 400)
                raise InvalidUsage("uReport data is invalid.", 400)

            report = data

            max_ureport_length = InvalidUReport.__lobs__["ureport"]

            if len(str(report)) > max_ureport_length:
                raise InvalidUsage("uReport may only be {0} bytes long"
                                   .format(max_ureport_length), 413)

            osr_id = None
            osr = None
            if report["os"]["name"] in systems:
                try:
                    osr = (db.session.query(OpSysRelease)
                           .join(OpSys)
                           .filter(OpSys.name ==
                                   systems[report["os"]["name"]].nice_name)
                           .filter(OpSysRelease.version ==
                                   report["os"]["version"])
                           .first())
                except (DatabaseError, InterfaceError) as e:
                    flash("Database unreachable. The uReport couldn't be saved. Please try again later.",
                          "danger")
                    logging.exception(e)
                    return render_template("reports/new.html",
                                           form=form), 503 #HTTP Service Unavailable

                if osr:
                    osr_id = osr.id
            try:
                dbreport = ureport.is_known(report, db, return_report=True,
                                            opsysrelease_id=osr_id)
            except Exception as e: # pylint: disable=broad-except
                logging.exception(e)
                dbreport = None

            known = bool(dbreport)
            fname = str(uuid.uuid4())
            fpath = os.path.join(paths["reports_incoming"], fname)
            with open(fpath, 'w') as file:
                file.write(raw_data.decode("utf-8"))

            if request_wants_json():
                response = {'result': known}

                try:
                    report2 = ureport2(report)
                    ureport.validate(report2)
                except FafError:
                    report2 = None

                if report2 is not None:
                    try:
                        solution = find_solution(report2, db=db, osr=osr)
                    except (DatabaseError, InterfaceError) as e:
                        flash("Database unreachable. The solution couldn't be retrieved. Please try again later.",
                              "danger")
                        logging.exception(e)
                        return render_template("reports/new.html",
                                               form=form), 503 #HTTP Service Unavailable
                    if solution is not None:
                        response["message"] = (
                            "Your problem seems to be caused by {0}\n\n"
                            "{1}".format(solution.cause, solution.note_text))

                        if solution.url:
                            response["message"] += (
                                "\n\nYou can get more information at {0}"
                                .format(solution.url))

                        solution_dict = {"cause": solution.cause,
                                         "note":  solution.note_text,
                                         "url":   solution.url}
                        if not solution_dict["url"]:
                            del solution_dict["url"]
                        response["solutions"] = [solution_dict]
                        response["result"] = True

                    try:
                        problemplugin = problemtypes[
                            report2["problem"]["type"]]
                        response["bthash"] = problemplugin.hash_ureport(
                            report2["problem"])
                    except Exception as e: # pylint: disable=broad-except
                        logging.exception(e)

                if known:
                    url = url_for('reports.item', report_id=dbreport.id,
                                  _external=True)
                    parts = [{"reporter": "ABRT Server",
                              "value": url,
                              "type": "url"}]
                    try:
                        bugs = (db.session.query(BzBug)
                                .join(ReportBz)
                                .filter(ReportBz.bzbug_id == BzBug.id)
                                .filter(ReportBz.report_id == dbreport.id)
                                .all())
                    except (DatabaseError, InterfaceError) as e:
                        flash("Database unreachable. The bugs couldn't be retrieved. Please try again later.",
                              "danger")
                        logging.exception(e)
                        return render_template("reports/new.html",
                                               form=form), 503 #HTTP Service Unavailable
                    for bug in bugs:
                        parts.append({"reporter": "Bugzilla",
                                      "value": bug.url,
                                      "type": "url"})

                    if 'message' not in response:
                        response['message'] = ''
                    else:
                        response['message'] += '\n\n'

                    response[
                        'message'] += "\n".join(p["value"] for p in parts
                                                if p["type"].lower() == "url")
                    response['reported_to'] = parts

                json_response = jsonify(response)
                json_response.status_code = 202
                return json_response

            flash(
                "The uReport was saved successfully. Thank you.", "success")
            return render_template("reports/new.html",
                                   form=form), 202

        except InvalidUsage as e:
            if request_wants_json():
                response = jsonify({"error": e.message})
                response.status_code = e.status_code
                return response

            flash(e.message, "danger")
            return render_template("reports/new.html",
                                   form=form), e.status_code

    return render_template("reports/new.html",
                           form=form)
Esempio n. 3
0
def new():
    form = NewReportForm()
    if request.method == "POST":
        try:
            if not form.validate() or form.file.name not in request.files:
                raise InvalidUsage("Invalid form data.", 400)
            raw_data = request.files[form.file.name].read()
            try:
                data = json.loads(raw_data)
            except Exception as ex:
                _save_invalid_ureport(db, raw_data, str(ex))
                raise InvalidUsage("Couldn't parse JSON data.", 400)

            try:
                ureport.validate(data)
            except Exception as exp:
                reporter = None
                if ("reporter" in data and "name" in data["reporter"]
                        and "version" in data["reporter"]):
                    reporter = "{0} {1}".format(data["reporter"]["name"],
                                                data["reporter"]["version"])

                _save_invalid_ureport(db,
                                      json.dumps(data, indent=2),
                                      str(exp),
                                      reporter=reporter)

                if ("os" in data and "name" in data["os"]
                        and data["os"]["name"] not in systems
                        and data["os"]["name"].lower() not in systems):
                    _save_unknown_opsys(db, data["os"])

                raise InvalidUsage("uReport data is invalid.", 400)

            report = data

            max_ureport_length = InvalidUReport.__lobs__["ureport"]

            if len(str(report)) > max_ureport_length:
                raise InvalidUsage(
                    "uReport may only be {0} bytes long".format(
                        max_ureport_length), 413)

            osr_id = None
            osr = None
            if report["os"]["name"] in systems:
                osr = (db.session.query(OpSysRelease).join(OpSys).filter(
                    OpSys.name == systems[report["os"]["name"]].nice_name
                ).filter(
                    OpSysRelease.version == report["os"]["version"]).first())

                if osr:
                    osr_id = osr.id
            try:
                dbreport = ureport.is_known(report,
                                            db,
                                            return_report=True,
                                            opsysrelease_id=osr_id)
            except Exception as e:
                logging.exception(e)
                dbreport = None

            known = bool(dbreport)
            fname = str(uuid.uuid4())
            fpath = os.path.join(paths["reports_incoming"], fname)
            with open(fpath, 'w') as file:
                file.write(raw_data)

            if request_wants_json():
                response = {'result': known}

                try:
                    report2 = ureport2(report)
                except FafError:
                    report2 = None

                if report2 is not None:
                    solution = find_solution(report2, db=db, osr=osr)
                    if solution is not None:
                        response["message"] = (
                            "Your problem seems to be caused by {0}\n\n"
                            "{1}".format(solution.cause, solution.note_text))

                        if solution.url:
                            response["message"] += (
                                "\n\nYou can get more information at {0}".
                                format(solution.url))

                        solution_dict = {
                            "cause": solution.cause,
                            "note": solution.note_text,
                            "url": solution.url
                        }
                        if not solution_dict["url"]:
                            del solution_dict["url"]
                        response["solutions"] = [solution_dict]
                        response["result"] = True

                    try:
                        problemplugin = problemtypes[report2["problem"]
                                                     ["type"]]
                        response["bthash"] = problemplugin.hash_ureport(
                            report2["problem"])
                    except Exception as e:
                        logging.exception(e)
                        pass

                if known:
                    url = url_for('reports.item',
                                  report_id=dbreport.id,
                                  _external=True)
                    parts = [{
                        "reporter": "ABRT Server",
                        "value": url,
                        "type": "url"
                    }]

                    bugs = (db.session.query(BzBug).join(ReportBz).filter(
                        ReportBz.bzbug_id == BzBug.id).filter(
                            ReportBz.report_id == dbreport.id).all())
                    for bug in bugs:
                        parts.append({
                            "reporter": "Bugzilla",
                            "value": bug.url,
                            "type": "url"
                        })

                    if 'message' not in response:
                        response['message'] = ''
                    else:
                        response['message'] += '\n\n'

                    response['message'] += "\n".join(
                        p["value"] for p in parts
                        if p["type"].lower() == "url")
                    response['reported_to'] = parts

                json_response = jsonify(response)
                json_response.status_code = 202
                return json_response
            else:
                flash("The uReport was saved successfully. Thank you.",
                      "success")
                return render_template("reports/new.html", form=form), 202

        except InvalidUsage as e:
            if request_wants_json():
                response = jsonify({"error": e.message})
                response.status_code = e.status_code
                return response
            else:
                flash(e.message, "danger")
                return render_template("reports/new.html",
                                       form=form), e.status_code

    return render_template("reports/new.html", form=form)
Esempio n. 4
0
File: views.py Progetto: trams/faf
def new(request):
    if request.method == 'POST':
        form = NewReportForm(request.POST, request.FILES)
        if form.is_valid():
            db = pyfaf.storage.getDatabase()
            report = form.cleaned_data['file']['converted']

            # maybe determine it better?
            max_ureport_length = InvalidUReport.__lobs__["ureport"]

            if len(str(report)) > max_ureport_length:
                err = "uReport may only be {0} bytes long".format(max_ureport_length)
                if "application/json" in request.META.get("HTTP_ACCEPT"):
                    return HttpResponse(json.dumps({"error": err}),
                                        status=413, mimetype="application/json")

                return HttpResponse(err, status=413, mimetype="application/json")

            try:
                dbreport = ureport.is_known(report, db, return_report=True)
            except Exception as e:
                logging.exception(e)
                dbreport = None

            known = bool(dbreport)
            spool_dir = get_spool_dir("reports")
            fname = str(uuid.uuid4())
            with open(os.path.join(spool_dir, 'incoming', fname), 'w') as fil:
                fil.write(form.cleaned_data['file']['json'])

            if 'application/json' in request.META.get('HTTP_ACCEPT'):
                response = {'result': known }

                opsys_id = None
                opsys = db.session.query(OpSys).filter(OpSys.name == report["os"]["name"]).first()
                if opsys:
                    opsys_id = opsys.id

                try:
                    report2 = ureport2(report)
                except FafError:
                    report2 = None

                if report2 is not None:
                    solution = find_solution(report2, db=db)
                    if solution is not None:
                        response['message'] = ("Your problem seems to be caused by {0}\n\n"
                                               "{1}".format(solution.cause, solution.note_text))
                        if solution.url:
                            response['message'] += ("\n\nYou can get more information at {0}"
                                                    .format(solution.url))

                        response['solutions'] = [{'cause': solution.cause,
                                                  'note':  solution.note_text,
                                                  'url':   solution.url}]
                        response['result'] = True

                    try:
                        problemplugin = problemtypes[report2["problem"]["type"]]
                        response["bthash"] = problemplugin.hash_ureport(report2["problem"])
                    except Exception as e:
                        logging.exception(e)
                        pass

                if known:
                    site = RequestSite(request)
                    url = reverse('webfaf.reports.views.item', args=[dbreport.id])
                    parts = [{"reporter": "ABRT Server",
                              "value": "https://{0}{1}".format(site.domain, url),
                              "type": "url"}]

                    bugs = (db.session.query(BzBug)
                                      .join(ReportBz)
                                      .filter(ReportBz.bzbug_id == BzBug.id)
                                      .filter(ReportBz.report_id == dbreport.id)
                                      .all())
                    for bug in bugs:
                        parts.append({"reporter": "Bugzilla",
                                      "value": bug.url,
                                      "type": "url"})

                    if 'message' not in response:
                        response['message'] = ''
                    else:
                        response['message'] += '\n\n'

                    response['message'] += "\n".join(p["value"] for p in parts if p["type"].lower() == "url")
                    response['reported_to'] = parts

                return HttpResponse(json.dumps(response),
                    status=202,
                    mimetype='application/json')

            return render_to_response('reports/success.html',
                {'report': report, 'known': known},
                context_instance=RequestContext(request))
        else:
            err = form.errors['file'][0]
            if 'application/json' in request.META.get('HTTP_ACCEPT'):
                response = {'error' : err}
                return HttpResponse(json.dumps(response),
                status=400, mimetype='application/json')

            return render_to_response('reports/new.html', {'form': form},
                context_instance=RequestContext(request))
    else:
        form = NewReportForm()

    return render_to_response('reports/new.html', {'form': form},
        context_instance=RequestContext(request))