コード例 #1
0
ファイル: views.py プロジェクト: yvanlebras/refinery-platform
def run(request):
    """Run analysis, return URL of the analysis status page
    Needs re-factoring

    """
    logger.debug("analysis_manager.views.run called")

    if not request.is_ajax():
        return HttpResponseBadRequest()  # 400
    allowed_methods = ['POST']
    if request.method not in allowed_methods:
        return HttpResponseNotAllowed(allowed_methods)  # 405

    analysis_config = simplejson.loads(request.body)
    try:
        workflow_uuid = analysis_config['workflowUuid']
        study_uuid = analysis_config['studyUuid']
        node_set_uuid = analysis_config['nodeSetUuid']
        node_relationship_uuid = analysis_config['nodeRelationshipUuid']
    except KeyError:
        return HttpResponseBadRequest()  # 400
    # must provide workflow and study UUIDs,
    # and either node set UUID or node relationship UUID
    if not (workflow_uuid and study_uuid and
            (node_set_uuid or node_relationship_uuid)):
        return HttpResponseBadRequest()  # 400

    # single-input workflow
    if node_set_uuid:
        #TODO: handle DoesNotExist exception
        curr_node_set = NodeSet.objects.get(uuid=node_set_uuid)
        curr_node_dict = curr_node_set.solr_query_components
        curr_node_dict = simplejson.loads(curr_node_dict)

        # solr results
        solr_uuids = get_solr_results(
            curr_node_set.solr_query,
            only_uuids=True,
            selected_mode=curr_node_dict['documentSelectionBlacklistMode'],
            selected_nodes=curr_node_dict['documentSelection'])

        # retrieving workflow based on input workflow_uuid
        #TODO: handle DoesNotExist exception
        curr_workflow = Workflow.objects.filter(uuid=workflow_uuid)[0]

        # TODO: catch if study or data set don't exist
        study = Study.objects.get(uuid=study_uuid)
        data_set = InvestigationLink.objects.filter(
            investigation__uuid=study.investigation.uuid).order_by(
                "version").reverse()[0].data_set

        logger.info("Associating analysis with data set %s (%s)" %
                    (data_set, data_set.uuid))

        ######### ANALYSIS MODEL ########
        # How to create a simple analysis object
        temp_name = curr_workflow.name + " " + datetime.now().strftime(
            "%Y-%m-%d @ %H:%M:%S")
        summary_name = "None provided."

        analysis = Analysis(
            summary=summary_name,
            name=temp_name,
            project=request.user.get_profile().catch_all_project,
            data_set=data_set,
            workflow=curr_workflow,
            time_start=datetime.now())
        analysis.save()

        analysis.set_owner(request.user)

        # gets galaxy internal id for specified workflow
        workflow_galaxy_id = curr_workflow.internal_id

        # getting distinct workflow inputs
        workflow_data_inputs = curr_workflow.data_inputs.all()[0]

        # NEED TO GET LIST OF FILE_UUIDS from solr query
        count = 0
        for file_uuid in solr_uuids:
            count += 1
            temp_input = WorkflowDataInputMap(
                workflow_data_input_name=workflow_data_inputs.name,
                data_uuid=file_uuid,
                pair_id=count)
            temp_input.save()
            analysis.workflow_data_input_maps.add(temp_input)
            analysis.save()

    # dual-input workflow
    if node_relationship_uuid:
        # Input list for running analysis
        ret_list = []
        # retrieving workflow based on input workflow_uuid
        curr_workflow = Workflow.objects.get(uuid=workflow_uuid)

        # TODO: catch if study or data set don't exist
        study = Study.objects.get(uuid=study_uuid)
        data_set = InvestigationLink.objects.filter(
            investigation__uuid=study.investigation.uuid).order_by(
                "version").reverse()[0].data_set

        # Get node relationship model
        curr_relationship = NodeRelationship.objects.get(
            uuid=node_relationship_uuid)

        # Iterating over node pairs
        input_keys = []
        base_input = {}
        # defining inputs used for analysis
        for workflow_inputs in curr_workflow.input_relationships.all():
            base_input[workflow_inputs.set1] = {}
            base_input[workflow_inputs.set2] = {}
            input_keys.append(workflow_inputs.set1)
            input_keys.append(workflow_inputs.set2)

        # creating instance of instance of input data pairing for analysis,
        # i.e. [{u'exp_file': {'node_uuid': u'3d061699-6bc8-11e2-9b55-406c8f1d5108', 'pair_id': 1}, u'input_file': {'node_uuid': u'3d180d11-6bc8-11e2-9bc7-406c8f1d5108', 'pair_id': 1}}]
        count = 1
        for curr_pair in curr_relationship.node_pairs.all():
            temp_pair = copy.deepcopy(base_input)
            print "curr_pair"
            print temp_pair
            print curr_pair
            #temp_pair = {}
            if curr_pair.node2:
                #print curr_pair.node2.uuid
                temp_pair[input_keys[0]]['node_uuid'] = curr_pair.node1.uuid
                temp_pair[input_keys[0]]['pair_id'] = count
                temp_pair[input_keys[1]]['node_uuid'] = curr_pair.node2.uuid
                temp_pair[input_keys[1]]['pair_id'] = count
                ret_list.append(temp_pair)
                print temp_pair
                count += 1

        logger.info("Associating analysis with data set %s (%s)" %
                    (data_set, data_set.uuid))

        ######### ANALYSIS MODEL ########
        # How to create a simple analysis object
        temp_name = curr_workflow.name + " " + str(datetime.now())
        summary_name = "None provided."

        analysis = Analysis(
            summary=summary_name,
            name=temp_name,
            project=request.user.get_profile().catch_all_project,
            data_set=data_set,
            workflow=curr_workflow,
            time_start=datetime.now())
        analysis.save()

        analysis.set_owner(request.user)

        # gets galaxy internal id for specified workflow
        workflow_galaxy_id = curr_workflow.internal_id

        # getting distinct workflow inputs
        workflow_data_inputs = curr_workflow.data_inputs.all()

        logger.debug("ret_list")
        logger.debug(simplejson.dumps(ret_list, indent=4))

        ######### ANALYSIS MODEL
        # Updating Refinery Models for updated workflow input (galaxy worfkflow input id & node_uuid
        count = 0
        for samp in ret_list:
            count += 1
            for k, v in samp.items():
                temp_input = WorkflowDataInputMap(
                    workflow_data_input_name=k,
                    data_uuid=samp[k]["node_uuid"],
                    pair_id=count)
                temp_input.save()
                analysis.workflow_data_input_maps.add(temp_input)
                analysis.save()

    # keeping new reference to analysis_status
    analysis_status = AnalysisStatus.objects.create(analysis=analysis)
    analysis_status.save()

    # call function via analysis_manager
    run_analysis.delay(analysis)

    redirect_url = reverse('analysis_manager.views.analysis_status',
                           args=(analysis.uuid, ))
    return HttpResponse(redirect_url)
コード例 #2
0
ファイル: views.py プロジェクト: DartingMelody/hack_mercari
def home(request):
    if request.method == 'GET':
        return render(request, 'home.html')
    else:
        return HttpResponseNotAllowed(['GET'])
コード例 #3
0
ファイル: views.py プロジェクト: jeffchang5/MHacks-Website
def application_review(request):
    if request.method == 'GET':
        event_date = datetime.date(1998, 10, 7)

        search_dict = {}

        hacker_search_keys = {
            'first_name': ['user__first_name', 'istartswith'],
            'last_name': ['user__last_name', 'istartswith'],
            'email': ['user__email', 'icontains'],
            'school': ['school', 'icontains'],
            'major': ['major', 'icontains'],
            'gender': ['gender', 'icontains'],
            'city': ['from_city', 'icontains'],
            'state': ['from_state', 'icontains'],
            'score_min': ['score', 'gte'],
            'score_max': ['score', 'lte'],
        }

        mentor_search_keys = {
            'first_name': ['user__first_name', 'istartswith'],
            'last_name': ['user__last_name', 'istartswith'],
            'email': ['user__email', 'icontains']
        }

        # pick search dict based on which type of search
        search_keys = dict()
        if 'hacker' in request.GET:
            search_keys = hacker_search_keys
        elif 'mentor' in request.GET:
            search_keys = mentor_search_keys

        for key in search_keys:
            if request.GET.get(key):
                condition = "{0}__{1}".format(search_keys[key][0], search_keys[key][1])
                search_dict[condition] = request.GET[key]

        # get the types of applications based on which type of search
        applications = Application.objects.none()
        if 'hacker' in request.GET:
            applications = Application.objects.filter(**search_dict)

            if request.GET.get('is_veteran'):
                applications = applications.filter(num_hackathons__gt=1)

            if request.GET.get('is_beginner'):
                applications = applications.filter(num_hackathons__lt=2)
        elif 'mentor' in request.GET:
            applications = MentorApplication.objects.filter(**search_dict)

        # submitted applications
        applications = applications.filter(submitted=True)

        if request.GET.get('is_non_UM'):
            applications = applications.filter(~Q(user__email__icontains='umich.edu'))

        if request.GET.get('is_minor'):
            applications = applications.filter(birthday__gte=event_date)

        if request.GET.get('decision') and not request.GET.get('decision') == 'All':
            applications = applications.filter(decision=request.GET.get('decision'))

        # from the oldest applicants
        applications = applications.order_by('last_updated')

        if request.GET.get('limit'):
            applications = applications if (int(request.GET['limit']) > len(applications)) else applications[:int(
                request.GET['limit'])]

        applications = applications.filter(deleted=False)
        context = {'results': applications}
        # return the appropriate HTML view
        if 'hacker' in request.GET:
            return render(request, 'application_review.html', context=context)
        elif 'mentor' in request.GET:
            return render(request, 'mentor_review.html', context=context)

    return HttpResponseNotAllowed(permitted_methods=['GET'])
コード例 #4
0
ファイル: views.py プロジェクト: ssgh01sb/mozilla-django-oidc
 def get(self, request):
     """Log out the user."""
     if self.get_settings("ALLOW_LOGOUT_GET_METHOD", False):
         return self.post(request)
     return HttpResponseNotAllowed(["POST"])
コード例 #5
0
            try:
                key_files = os.listdir(role_key)
                for key_file in key_files:
                    os.remove(os.path.join(role_key, key_file))
                os.rmdir(role_key)
            except OSError, e:
                logger.warning(u"Delete Role: delete key error, %s" % e)
                raise ServerError(u"删除系统用户key失败: %s" % e)
            logger.info(u"delete role %s - delete role key directory: %s" %
                        (role.name, role_key))
            # 数据库里删除记录
            role.delete()
            return HttpResponse(u"删除系统用户: %s" % role.name)
        except ServerError, e:
            return HttpResponseBadRequest(u"删除失败, 原因: %s" % e)
    return HttpResponseNotAllowed(u"仅支持POST")


@require_role('admin')
def perm_role_detail(request):
    """
    the role detail page
        the role_info data like:
            {'asset_groups': [],
            'assets': [<Asset: 192.168.10.148>],
            'rules': [<PermRule: PermRule object>],
            '': [],
            '': [<User: user1>]}
    """
    # 渲染数据
    header_title, path1, path2 = "系统用户", "系统用户管理", "系统用户详情"
コード例 #6
0
ファイル: views.py プロジェクト: MartinOrz/exhentaiWeb
 def invalid_method(request, *args, **kwargs):
     return HttpResponseNotAllowed(table.keys())
コード例 #7
0
def update_profile(request):
    """ This method is used as ajax call in order to update user profile """

    if not request.is_ajax() or not request.method == 'POST':
        return HttpResponseNotAllowed(['POST'])

    height = request.POST.get('height')
    weight = request.POST.get('weight')
    activity = request.POST.get('activity')
    sex = request.POST.get('sex')
    birthday = request.POST.get('birthday')
    name = request.POST.get('name')
    pk = request.POST.get('pk')
    diets = request.POST.get('diets')

    if request.user.is_authenticated():
        # p = request.user.account.profile
        p = get_object_or_404(Profile, pk=pk)
        # TODO : check propriety
        if name:
            p.name = name
        elif sex:
            p.sex = sex
        elif birthday:
            p.birthday = datetime.strptime(
                birthday, '%d-%m-%Y') if len(birthday) else datetime.now()
        elif height:
            p.height = height
        elif weight:
            p.weight = weight
        elif activity:
            p.activity = activity
        elif diets:
            diets = json2obj(diets)

            for key, checked in diets:
                if Diet.objects.filter(pk=key).exists():
                    if checked:
                        p.diets.add(key)
                    else:
                        p.diets.remove(key)
            # Updating recipe list for profile
            logger.info(
                "Calculating recipe list matching new diet criteria...")
            Recipe.for_profile_async(p)
            logger.info("Thread running.")
        p.save()

    else:
        if name:
            request.session['name'] = name
        elif birthday:
            request.session['birthday'] = birthday
        elif sex:
            request.session['sex'] = sex
        elif height:
            request.session['height'] = height
        elif weight:
            request.session['weight'] = weight
        elif activity:
            request.session['activity'] = activity
        elif diets:
            request.session['activity'] = activity

    return HttpResponse(diets)
コード例 #8
0
ファイル: views.py プロジェクト: swsnu/swpp2020-team18
def wordlist(request):

    """
    Get or adjust the Wordlist model.
    In **GET**: Returns all the words and phrases in the user's wordlist

    :param request: a HTTP request
    :type request: HttpRequest

    :returns: a JSON response with all words and phrases
    :rtype: JsonResponse

    In **PATCH**: Add or remove a phrase(word) to user's wordlist

    PATCH parameters:
        PATCH['phrase']: Phrase
        PATCH['action']: Add or remove

    :param request: a HTTP request
    :type request: HttpRequest

    :returns: a HTTP response
    :rtype: HttpResponse
    """
    if request.user.is_authenticated:
        if request.method == "GET":
            word_all_list = [
                {
                    "word": Word.objects.get(id=phrase["word_id"]).content,
                    "phrase": phrase["content"],
                    "korean_meaning": Word.objects.get(
                        id=phrase["word_id"]
                    ).korean_meaning,
                    "confidence": HistoryWord.objects.get(
                        word=Word.objects.get(id=phrase["word_id"]),
                        history=request.user.history,
                    ).confidence,
                    "created_at": WordlistPhrase.objects.get(
                        phrase=Phrase.objects.get(content=phrase["content"]),
                        wordlist=Wordlist.objects.get(user=request.user),
                    ).created_at,
                }
                for phrase in Wordlist.objects.get(user=request.user)
                .added_phrase.all()
                .values()
            ]
            return JsonResponse(
                word_all_list, safe=False, json_dumps_params={"ensure_ascii": False}
            )
        elif request.method == "PATCH":
            req_data = json.loads(request.body.decode())
            phrase_data = req_data["phrase"]
            phrase_action = req_data["action"]
            try:
                found_phrase = Phrase.objects.get(content=phrase_data)
            except Phrase.DoesNotExist:
                return HttpResponse(status=404)
            user_wordlist = request.user.wordlist
            if phrase_action == "add":
                user_wordlist.added_phrase.add(found_phrase)
            elif phrase_action == "remove":
                user_wordlist.added_phrase.remove(found_phrase)
            else:
                return HttpResponse(status=404)
            return HttpResponse(status=200)
        else:
            return HttpResponseNotAllowed(["GET", "PATCH"])
    else:
        return HttpResponse(status=401)
コード例 #9
0
 def post(self, request):
     return HttpResponseNotAllowed([])
コード例 #10
0
ファイル: views.py プロジェクト: eisoftserv/MISC
def staffapproveplatform(request):
    if request.user.is_staff == False:
        return HttpResponseForbidden("Access not allowed!")
    if (request.method != "POST"):
        return HttpResponseNotAllowed(["POST"])

    ntx = 0
    cid = request.POST["cid"]
    if len(cid) < 1 or len(cid) > 18:
        ntx += 1
    status = request.POST["status"]
    if len(status) < 2 or len(status) > 10 or status not in [
            "public", "archived"
    ]:
        ntx += 1
    type = request.POST["type"]
    if len(type) < 1 or len(type) > 10 or type not in ["social", "resource"]:
        ntx += 1
    name = request.POST["name"]
    if len(name) < 2 or len(name) > 80:
        ntx += 1
    if (ntx > 0):
        return HttpResponseBadRequest("Incorrect parameter!")

    tx = ""
    name = name.lower()
    rs = carveUrl(name)
    name = rs[0]
    tx = tx + rs[1]
    if type == "resource":
        plt = getPlatform(url=name)
        if plt is not None and str(plt.id) != cid:
            tx = tx + "Platform URL is duplicate; "
        plt = getiPlatform(id=cid)
        if plt is None:
            tx = tx + "Platform not found; "
        else:
            if plt.status != PlatformStatus.PRIVATE:
                tx = tx + "status is not private; "
        if (len(tx) > 0):
            return JsonResponse({"error": "yes", "message": tx})
        try:
            plt.url = name
            if status == "public":
                plt.status = PlatformStatus.PUBLIC
            else:
                plt.status = PlatformStatus.ARCHIVED
            plt.save()
        except Exception as exc:
            return JsonResponse({"error": "yes", "message": str(exc)})
    elif type == "social":
        soc = getNetwork(url=name)
        if soc is not None and str(soc.id) != cid:
            tx = tx + "social network URL is duplicate; "
        soc = getiNetwork(id=cid)
        if soc is None:
            tx = tx + "social network not found; "
        else:
            if soc.status != NetworkStatus.PRIVATE:
                tx = tx + "status is not private; "
        if (len(tx) > 0):
            return JsonResponse({"error": "yes", "message": tx})
        try:
            soc.url = name
            if status == "public":
                soc.status = NetworkStatus.PUBLIC
            else:
                soc.status = NetworkStatus.ARCHIVED
            soc.save()
        except Exception as exc:
            return JsonResponse({"error": "yes", "message": str(exc)})

    return JsonResponse({"error": "no", "message": "ok"})
コード例 #11
0
def send_b64(request):
    if request.method == 'POST':
        r = choice(InviteChallengeView.B64_ENC)
        return JsonResponse({'code': r, 'format': 'encoded'})
    return HttpResponseNotAllowed(['POST'])
コード例 #12
0
ファイル: views.py プロジェクト: eisoftserv/MISC
def resolvereported(request, ctype, fid, cid):
    if request.user.is_staff == False:
        return HttpResponseForbidden("Access not allowed!")
    if request.method != "GET":
        return HttpResponseNotAllowed(["GET"])
    if len(fid) > 18 or len(cid) > 18 or len(ctype) > 2 or ctype not in [
            "1", "2", "3", "4"
    ]:
        return HttpResponseBadRequest("Incorrect parameter!")
    ntype = int(ctype)

    if cid != "0":
        if ntype == ItemType.SUGGESTION:
            osug = getiSuggestion(cid)
            if osug is not None and osug.status == SuggestionStatus.PUBLIC:
                try:
                    osug.status = SuggestionStatus.ARCHIVED
                    osug.save()
                except Exception as exc:
                    return HttpResponseServerError(
                        "Something went wrong while archiving the suggestion!")
        elif ntype == ItemType.COMMENT:
            ocom = getiComment(cid)
            if ocom is not None and ocom.status == CommentStatus.PUBLIC:
                try:
                    ocom.status = CommentStatus.ARCHIVED
                    ocom.save()
                except Exception as exc:
                    return HttpResponseServerError(
                        "Something went wrong while archiving the comment!")
        elif ntype == ItemType.MESSAGE:
            omes = getiMessage(cid)
            if omes is not None and omes.status == MessageStatus.PRIVATE:
                try:
                    omes.status = MessageStatus.ARCHIVED
                    omes.save()
                except Exception as exc:
                    return HttpResponseServerError(
                        "Something went wrong while archiving the private message!"
                    )
        elif ntype == ItemType.PROFILE:
            omem = getiMember(cid)
            if omem is not None and omem.status == MemberStatus.PUBLIC:
                try:
                    omem.status = MemberStatus.PRIVATE
                    omem.save()
                except Exception as exc:
                    return HttpResponseServerError(
                        "Something went wrong while turning private the member profile!"
                    )

    oflag = getiFlag(fid)
    if oflag is not None and oflag.status == FlagStatus.INITIATED:
        try:
            if cid == "0":
                oflag.status = FlagStatus.ARCHIVED
            else:
                oflag.status = FlagStatus.PROCESSED
            oflag.save()
        except Exception as exc:
            return HttpResponseServerError(
                "Something went wrong while updating the report table!")

    return HttpResponseRedirect(reverse("staffreport"))
コード例 #13
0
ファイル: views.py プロジェクト: eisoftserv/MISC
def reporteditem(request, ctype, fid, cid):
    if request.user.is_staff == False:
        return HttpResponseForbidden("Access not allowed!")
    if request.method != "GET":
        return HttpResponseNotAllowed(["GET"])
    if len(fid) > 18 or len(cid) > 18 or len(ctype) > 2 or ctype not in [
            "1", "2", "3", "4"
    ]:
        return HttpResponseBadRequest("Incorrect parameter!")
    ntype = int(ctype)
    tx = ""

    if ntype == ItemType.SUGGESTION:
        sugg = getiSuggestion(cid)
        if sugg is None:
            tx = "Suggestion not found!"
        else:
            return render(request, 'staff/reportedsuggestion.html', {
                "ctype": ctype,
                "fid": fid,
                "elem": sugg
            })
    elif ntype == ItemType.COMMENT:
        cmnt = getiComment(cid)
        if cmnt is None:
            tx = "Comment not found!"
        else:
            return render(request, 'staff/reportedcomment.html', {
                "ctype": ctype,
                "fid": fid,
                "elem": cmnt
            })
    elif ntype == ItemType.MESSAGE:
        prime = getiMessage(cid)
        if prime is None:
            tx = "Private message not found!"
        else:
            return render(request, 'staff/reportedmessage.html', {
                "ctype": ctype,
                "fid": fid,
                "elem": prime
            })
    elif ntype == ItemType.PROFILE:
        opro = getiMember(cid)
        if opro is None:
            tx = "Profile not found!"
        else:
            return render(request, "staff/reportedprofile.html", {
                "ctype": ctype,
                "fid": fid,
                "gobuddy": opro
            })
    # we get here if the reported item is gone
    oflag = getiFlag(fid)
    if oflag is not None:
        try:
            oflag.status = FlagStatus.PROCESSED
            oflag.save()
        except Exception as exc:
            return HttpResponseServerError(
                "The item was not found and something went wrong while processing the report!"
            )

    return HttpResponseNotFound(
        "The item was not found and the report has been set as 'Processed'!")
コード例 #14
0
 def get(self, request, *args, **kwargs):
     return HttpResponseNotAllowed(permitted_methods=['POST'])
コード例 #15
0
ファイル: api.py プロジェクト: chbasel/MyJobs
def dynamic_chart(request):
    """
    return charting data given a set of filters, date range, and drilldown
    selection

    request
    {
        "date_start": "01/01/2016 00:00:00",
        "date_end": "01/08/2016 00:00:00",
        "active_filters": [{"type": "country", "value": "USA"},
                         {"type": "state", "value": "Indiana"}],
        "report": "found_on",
        "group_overwrite": "browser",
    }

    response
    {
        "column_names":
            [
                {"key": "browser", "label": "Browser"},
                {"key": "job_views", "label": "Job Views"},
             ],
        "rows":
            [
                {"browser": "Chrome", "job_views": "101",  "visits": "1050"},
                {"browser": "IE11", "job_views": "231", "visits": "841"},
                {"browser": "IE8", "job_views": "23", "visits": "341"},
                {"browser": "Firefox", "job_views": "21", "visits": "298"},
                {"browser": "Netscape Navigator", "job_views": "1", "visits": "1"},
                {"browser": "Dolphin", "job_views": "1", "visits": "1"}
             ]
    }


    """
    if request.method != 'POST':
        return HttpResponseNotAllowed(['POST'])

    validator = ApiValidator()

    query_data = json.loads(request.POST.get('request', '{}'))
    if not query_data:
        validator.note_error("No data provided.")

    if validator.has_errors():
        return validator.build_error_response()

    required_fields = ['date_end', 'date_start', 'report']
    for field in required_fields:
        if not query_data.get(field):
            validator.note_field_error(field, '%s is required' % field)

    if validator.has_errors():
        return validator.build_error_response()

    try:
        date_start = dateparser.parse(query_data['date_start'])
    except ValueError:
        validator.note_field_error('date_start',
                                   'Invalid date start: ' +
                                    query_data['date_end'])

    try:
        date_end = dateparser.parse(query_data['date_end'])
    except ValueError:
        validator.note_field_error('date_end',
                                   'Invalid date end: ' +
                                   query_data['date_end'])

    report_data = get_analytics_reporttype_datatype(query_data['report'])

    if not report_data:
        validator.note_field_error(
            "analytics_report_id", "No analytics report found.")

    if validator.has_errors():
        return validator.build_error_response()

    sample_size = 50000 # TODO: Add sample size to request object

    job_views = get_mongo_db().job_views

    buids = get_company_buids(request)

    top_query = build_top_query(date_start, date_end, buids)

    sample_query, total_count = retrieve_sampling_query_and_count(job_views,
                                                                  top_query,
                                                                  sample_size)

    if not sample_query:
        sample_size = total_count

    active_filter_query = build_active_filter_query(query_data)

    group_by, remaining_dimensions = determine_data_group_by_and_remaining(
        query_data, report_data
    )

    group_query = build_group_by_query(group_by.column_name)

    query = [
        {'$match': top_query},
    ] + sample_query + active_filter_query + group_query

    records = job_views.aggregate(query, allowDiskUse=True)

    if sample_query:
        def curried_query(count):
            return calculate_error_and_count(total_count, sample_size, count)

        records = adjust_records_for_sampling(records, curried_query)

    response = {
        "column_names":
            [
                {"key": group_by.column_name,
                 "label": group_by.filter_interface_display},
                {"key": "job_views", "label": "Job Views"},
                {"key": "visitors", "label": "Visitors"},
             ],
        "rows":
            [format_return_dict(r, group_by.column_name) for r in records],
        "chart_type": group_by.filter_interface_type,
        "group_by": group_by.column_name,
        "remaining_dimensions": [{"key": d.column_name,
                                  "label": d.filter_interface_display}
                                 for d in remaining_dimensions]
    }

    return HttpResponse(json.dumps(response))
コード例 #16
0
 def get(self, request, *args, **kwargs):
     """
     Block the get method (the View is posted from a Bootstrap modal).
     """
     return HttpResponseNotAllowed(permitted_methods=['POST'])
コード例 #17
0
 def wrapper(request, *args, **kw):
     if request.method in methods:
         return func(request, *args, **kw)
     return HttpResponseNotAllowed(methods)
コード例 #18
0
def create_description(request):
    """Page to create hidden website description."""
    return helpers.render_page('createHsDescription.html')
    return HttpResponseNotAllowed("Only GET request is allowed.")
コード例 #19
0
 def get(self, *args, **kwargs):
     return HttpResponseNotAllowed(['POST'])
コード例 #20
0
ファイル: views.py プロジェクト: reyblacua/eatsyPruebas
def errorNotAllowedView(request, exception):
    return HttpResponseNotAllowed(render(request, "products/errorView.html"))
コード例 #21
0
def dynamic_ip_update_view(request):
    """
    
    TODO: explain dynamic IP update options and logic
    
    if hostname is missing, the ips of all A and AAAA records of the zone are changed
    otherwise only the specific record with the name=hostname and provided that the
    correct ip (v4, v6) has been provided for the type of the record (A, AAAA)
    
    If no ipv4 or ipv6 address is provided, then the client IP address is used
    to update A records (if the client IP is IPv4) or AAAA records (if client IP is IPv6).
    
    curl -k \
        -F "api_key=UBSE1RJ0J175MRAMJC31JFUH" \
        -F "hostname=ns1.centos.example.org" \
        -F "ipv4=10.1.2.3" \
        -F "ipv6=3ffe:1900:4545:3:200:f8ff:fe21:67cf" \
        https://centos.example.org/powerdns/update/

    """
    if request.method != 'POST':
        return HttpResponseNotAllowed(['POST'])
    form = DynamicIPUpdateForm(request.POST)

    if not form.is_valid():
        return HttpResponseBadRequest(repr(form.errors))

    # Determine protocol or REMOTE_ADDR
    remote_ipv4 = None
    remote_ipv6 = None
    try:
        validate_ipv4_address(request.META['REMOTE_ADDR'])
    except ValidationError:
        try:
            validate_ipv6_address(request.META['REMOTE_ADDR'])
        except ValidationError:
            return HttpResponseBadRequest(
                'Cannot determine protocol of remote IP address')
        else:
            remote_ipv6 = request.META['REMOTE_ADDR']
    else:
        remote_ipv4 = request.META['REMOTE_ADDR']

    # Gather required information

    # API key

    api_key = form.cleaned_data['api_key']

    # Hostname

    hostname = form.cleaned_data['hostname']

    # If the hostname is missing, the IP addresses of all A and AAAA records
    # of the zone are updated.
    update_all_hosts_in_zone = False
    if not hostname:
        update_all_hosts_in_zone = True

    # IP addresses

    ipv4 = form.cleaned_data['ipv4']
    ipv6 = form.cleaned_data['ipv6']

    # If IP information is missing, the remote client's IP address will be used.
    if not ipv4 and not ipv6:
        if remote_ipv4:
            ipv4 = remote_ipv4
        if remote_ipv6:
            ipv6 = remote_ipv6

    # All required data is good. Process the request.

    DynamicZone = cache.get_model('powerdns_manager', 'DynamicZone')
    Record = cache.get_model('powerdns_manager', 'Record')

    # Get the relevant dynamic zone instance
    dyn_zone = DynamicZone.objects.get(api_key__exact=api_key)

    # Get A and AAAA records
    dyn_rrs = Record.objects.filter(domain=dyn_zone.domain,
                                    type__in=('A', 'AAAA'))
    if not dyn_rrs:
        return HttpResponseNotFound('A or AAAA resource records not found')

    # Check existence of hostname
    if hostname:
        hostname_exists = False
        for rr in dyn_rrs:
            if rr.name == hostname:
                hostname_exists = True
                break
        if not hostname_exists:
            return HttpResponseNotFound('error:Hostname not found: %s' %
                                        hostname)

    # Update the IPs

    rr_has_changed = False

    if update_all_hosts_in_zone:  # No hostname supplied
        for rr in dyn_rrs:

            # Try to update A records
            if rr.type == 'A' and ipv4:
                rr.content = ipv4
                rr_has_changed = True

            # Try to update AAAA records
            elif rr.type == 'AAAA' and ipv6:
                rr.content = ipv6
                rr_has_changed = True

            rr.save()

    else:  # A hostname is supplied
        for rr in dyn_rrs:
            if rr.name == hostname:

                # Try to update A records
                if rr.type == 'A' and ipv4:
                    rr.content = ipv4
                    rr_has_changed = True

                # Try to update AAAA records
                elif rr.type == 'AAAA' and ipv6:
                    rr.content = ipv6
                    rr_has_changed = True

                rr.save()

    if rr_has_changed:
        return HttpResponse('Success')
    else:
        return HttpResponseNotFound('error:No suitable resource record found')
コード例 #22
0
ファイル: middleware.py プロジェクト: tempbottle/hue
 def process_request(self, request):
   if request.method not in desktop.conf.HTTP_ALLOWED_METHODS.get():
     return HttpResponseNotAllowed(desktop.conf.HTTP_ALLOWED_METHODS.get())
コード例 #23
0
ファイル: api.py プロジェクト: starry-time/weblate
def vcs_service_hook(request, service):
    """Shared code between VCS service hooks.

    Currently used for bitbucket_hook, github_hook and gitlab_hook, but should
    be usable for other VCS services (Google Code, custom coded sites, etc.)
    too.
    """
    # We support only post methods
    if not settings.ENABLE_HOOKS:
        return HttpResponseNotAllowed(())

    # Check if we got payload
    try:
        data = parse_hook_payload(request)
    except (ValueError, KeyError, UnicodeError):
        return HttpResponseBadRequest('Could not parse JSON payload!')

    # Get service helper
    hook_helper = HOOK_HANDLERS[service]

    # Send the request data to the service handler.
    try:
        service_data = hook_helper(data)
    except Exception as error:
        LOGGER.error('failed to parse service %s data', service)
        report_error(error, sys.exc_info())
        return HttpResponseBadRequest('Invalid data in json payload!')

    # This happens on ping request upon installation
    if service_data is None:
        return hook_response('Hook working')

    # Log data
    service_long_name = service_data['service_long_name']
    repos = service_data['repos']
    repo_url = service_data['repo_url']
    branch = service_data['branch']
    full_name = service_data['full_name']

    # Generate filter
    spfilter = Q(repo__in=repos) | Q(repo__iendswith=full_name)

    # We need to match also URLs which include username and password
    for repo in repos:
        if not repo.startswith('https://'):
            continue
        spfilter = spfilter | (Q(repo__startswith='https://')
                               & Q(repo__endswith='@{0}'.format(repo[8:])))

    all_components = Component.objects.filter(spfilter)

    if branch is not None:
        all_components = all_components.filter(branch=branch)

    components = all_components.filter(project__enable_hooks=True)

    LOGGER.info(
        'received %s notification on repository %s, branch %s, '
        '%d matching components, %d to process',
        service_long_name,
        repo_url,
        branch,
        all_components.count(),
        components.count(),
    )

    # Trigger updates
    updates = 0
    for obj in components:
        updates += 1
        LOGGER.info('%s notification will update %s', service_long_name, obj)
        perform_update(obj)

    if updates == 0:
        return hook_response('No matching repositories found!', 'failure')

    return hook_response()
コード例 #24
0
    def _ajax_upload(self, request):
        if request.method == "POST":
            # determine which model and field this upload relates to
            try:
                model = request.POST['model']
                field = request.POST['field']
            except KeyError:
                return HttpResponseBadRequest("AJAX request not valid")
            else:
                # is the field one of the fields allowed?
                if field not in ('logo', 'square_logo', 'thumbnail'):
                    return HttpResponseBadRequest("AJAX request not valid")

            if request.is_ajax():
                # /!\ try this with Chrome / this is HTML5
                # the file is stored raw in the request
                is_raw = True
                upload = request
                try:
                    filename = request.POST['file']
                except KeyError:
                    return HttpResponseBadRequest("AJAX request not valid")
            # not an ajax upload, so it was the "basic" iframe version with
            # submission via form
            else:
                is_raw = False
                if len(request.FILES) == 1:
                    # FILES is a dictionary in Django but Ajax Upload gives
                    # the uploaded file an ID based on a random number, so it
                    # cannot be guessed here in the code. Rather than editing
                    # Ajax Upload to pass the ID in the querystring, observe
                    # that each upload is a separate request, so FILES should
                    # only have one entry. Thus, we can just grab the first
                    # (and only) value in the dict.
                    upload = request.FILES.values()[0]
                    filename = upload.name
                else:
                    raise Http404("Bad Upload")

            # we have a filename, now determine where to put it
            # first, we need to figure out what model this refers to
            #logger.debug(model + '/' + field)
            try:
                _model = getattr(models, model)
                upload_to = _model._meta.get_field(field).upload_to
            except Exception as e:
                logger.error(e)
                return HttpResponseBadRequest("Bad Request")

            # do we need to resize the image?
            width = request.POST['width'] if request.POST.has_key(
                'width') else None
            height = request.POST['height'] if request.POST.has_key(
                'height') else None
            if width and height:
                self.storage.set_size(width, height)

            # save the file
            self.storage.setup(filename, upload_to)
            success = self.storage.upload(upload, is_raw)
            ret_json = {'success': success, 'filename': self.storage.filename}
            return HttpResponse(json.dumps(ret_json, cls=DjangoJSONEncoder),
                                content_type='application/json; charset=utf-8')
        else:
            response = HttpResponseNotAllowed(['POST'])
            response.write("Only POST allowed")
            return response
コード例 #25
0
ファイル: views.py プロジェクト: DartingMelody/hack_mercari
def new_trip(request):
    if request.method == 'GET':
        return render(request, 'createJourney.html', API_KEY)
    else:
        return HttpResponseNotAllowed(['GET'])
コード例 #26
0
def cmd_run(request):
    if request.method == 'GET':
        form = CmdInputForm()
        return render_to_response('salt_cmd_run.html',
                                  RequestContext(request, {'form': form}))
    elif request.method == 'POST':
        form = CmdInputForm(request.POST)
        if form.is_valid():
            target = form.cleaned_data['target'].replace(' ', '')
            mapping = form.cleaned_data['mapping'].replace(' ', '')
            command = form.cleaned_data['cmdline'].lstrip(' ').rstrip(' ')
            result, info = parse_target_params(target, mapping)
            if result is None:
                return render_to_response(
                    'salt_cmd_run.html',
                    RequestContext(request, {
                        'form': form,
                        'error': info
                    }))
            if juge_danger_cmd(command):
                error = 'Danger Command !!!!'
                return render_to_response(
                    'salt_cmd_run.html',
                    RequestContext(request, {
                        'form': form,
                        'cmd_error': error
                    }))
            if not request.user.is_superuser:
                flag = True
                if command.startswith('ls'):
                    flag = False
                if command.startswith('/root/deploy/deploy_test.sh'):
                    num, output = commands.getstatusoutput(command)
                    if num != 0:
                        error = 'please checkout your command format !'
                        return render_to_response(
                            'salt_cmd_run.html',
                            RequestContext(request, {
                                'form': form,
                                'cmd_error': error
                            }))
                    cmd_info = CmdRunLogModel(user=request.user,
                                              time=datetime.now(),
                                              target=target,
                                              mapping=mapping,
                                              cmd=command,
                                              hosts='self',
                                              total=1)
                    cmd_info.save()
                    return render_to_response(
                        'salt_cmd_run.html',
                        RequestContext(request, {
                            'form': form,
                            'result': output
                        }))
                if ';' in command or r'&' in command or r'|' in command:
                    flag = True
                if flag:
                    error = 'Permission Denied ! '
                    return render_to_response(
                        'salt_cmd_run.html',
                        RequestContext(request, {
                            'form': form,
                            'cmd_error': error
                        }))
            target = info
            client = SaltByLocalApi('/etc/salt/master')
            output = client.client.cmd(target,
                                       'cmd.run', [command],
                                       expr_form=mapping)
            if output is None or output == {}:
                error = 'Bad Target Host !!!'
                return render_to_response(
                    'salt_cmd_run.html',
                    RequestContext(request, {
                        'form': form,
                        'error': error
                    }))
            result = ''
            hosts = []
            for k, v in output.iteritems():
                hosts.append(k)
                result = result + '\n\n------' + k + '-----\n\n' + v
            f = open('/tmp/.cmd_run.out', 'w')
            f.write(result)
            f.close()
            code, result = commands.getstatusoutput('cat /tmp/.cmd_run.out')
            if target == '*':
                cmd_info = CmdRunLogModel(user=request.user,
                                          time=datetime.now(),
                                          target=target,
                                          mapping=mapping,
                                          cmd=command,
                                          hosts='all hosts',
                                          total=len(hosts))
            else:
                cmd_info = CmdRunLogModel(user=request.user,
                                          time=datetime.now(),
                                          target=target,
                                          mapping=mapping,
                                          cmd=command,
                                          hosts=','.join(hosts),
                                          total=len(hosts))
            try:
                cmd_info.save()
            except:
                pass
            return render_to_response(
                'salt_cmd_run.html',
                RequestContext(request, {
                    'form': form,
                    'result': result
                }))
        else:
            return render_to_response('salt_cmd_run.html',
                                      RequestContext(request, {'form': form}))
    else:
        return HttpResponseNotAllowed(request)
コード例 #27
0
ファイル: auth.py プロジェクト: swsnu/swpp2019-team18
def token(request):
    if request.method == 'GET':
        return HttpResponse(status=204)
    else:
        return HttpResponseNotAllowed(['GET'])
コード例 #28
0
def deploy_application(request):
    match_list = ['list', 'grain', 'nodegroup', 'pcre', 'glob']
    app_list = [
        'zabbix', 'mysql', 'memcached', 'nginx', 'tomcat', 'system', 'redis',
        'php'
    ]
    if request.method == 'POST':
        if not request.user.is_superuser:
            return HttpResponseForbidden(request)
        mapping = request.POST.get('map', '').replace(' ', '')
        target = request.POST.get('target', '').replace(' ', '')
        app = str(request.POST.get('app', '').replace(' ', ''))
        if app == '' or target == '' or mapping == '':
            return HttpResponseNotAllowed(request)
        if mapping not in match_list or app not in app_list:
            return HttpResponseNotAllowed(request)
        result, info = parse_target_params(target, mapping)
        if result is None:
            return render_to_response('salt_deploy_application.html',
                                      RequestContext(request, {'error': info}))
        target = info
        client = SaltByLocalApi('/etc/salt/master')
        cmd = '%s.install' % app
        output = client.client.cmd(target,
                                   'state.sls', [cmd],
                                   expr_form=mapping)
        if output is None or output == {}:
            return render_to_response(
                'salt_deploy_application.html',
                RequestContext(request, {'error': u'无效的目标主机'}))
        error_dict = {}
        error_log = ''
        ok_dict = {}
        all_dict = {}
        for k, v in output.iteritems():
            if isinstance(v, list):
                return render_to_response(
                    'salt_deploy_application.html',
                    RequestContext(request, {'error': v[0]}))
            tmp_list = []
            for i, j in v.iteritems():
                if 'name' not in j or 'duration' not in j or 'comment' not in j:
                    tmp_list.append(
                        [i, ' ', 0, j['result'], j['changes'], j['comment']])
                else:
                    tmp_list.append([
                        i, j['name'], j['duration'], j['result'], j['changes'],
                        j['comment']
                    ])
            all_dict[k] = tmp_list
        for k, v in all_dict.iteritems():
            for i in v:
                if not i[3]:
                    error_dict[k] = v
                    break
        if len(error_dict) > 0:
            host_error_dict = {}
            for k, v in error_dict.iteritems():
                host_error_list = []
                for i in v:
                    if not i[3]:
                        host_error_list.append(i)
                        break
                host_error_dict[k] = host_error_list
            for k, v in host_error_dict.iteritems():
                error_log = error_log + '''-----host name: %s error log -----\n''' % k
                for i in v:
                    if i[4] != {} and isinstance(i[4], dict):
                        error_log = error_log + '''id:  %s   \n comment: %s \n name: %s  \n   %s \n''' % (
                            i[0], i[5], i[1], i[4]['stderr'])
                    else:
                        error_log = error_log + '''id:   %s   \n comment: %s \n name: %s  \n     %s \n''' % (
                            i[0], i[5], i[1], i[4])
        all_key = all_dict.keys()
        for key in all_key:
            if key not in error_dict.keys():
                ok_dict[key] = all_dict[key]
        head_txt = '-' * 10 + '\n'
        head_txt = head_txt + ''' total: %d , successed: %d  , failed: %d  \n''' % (
            len(all_dict), len(ok_dict), len(error_dict))
        head_txt = head_txt + '-' * 10 + '\n'
        success_txt = '-' * 10 + 'success' + '-' * 10 + '\n'
        error_txt = ''
        total_error_spend_time = 0
        total_ok_spend_time = 0
        if len(ok_dict) > 0:
            for k, v in ok_dict.iteritems():
                ok_spend_time = 0
                for i in v:
                    ok_spend_time = i[2] + ok_spend_time
                ok_spend_time = ok_spend_time / 1000
                total_ok_spend_time = total_ok_spend_time + ok_spend_time
                success_txt = success_txt + ''' Host: %s  | Spend time: %s Sec | Result: success \n''' % (
                    k, str(ok_spend_time))
        success_txt = success_txt + '-----success-----\n'
        if len(error_dict) > 0:
            log = ''
            for k, v in error_dict.iteritems():
                error_spend_time = 0
                for i in v:
                    if i[4] != {} and isinstance(i[4], dict):
                        log = log + '''name: %s \n  --error_info--: %s \n ''' % (
                            i[1], i[4]['stderr'])
                    else:
                        log = log + '''name: %s \n  --error_info--: %s \n ''' % (
                            i[1], i[4])
                    error_spend_time = i[2] + error_spend_time
                error_spend_time = error_spend_time / 1000
                total_error_spend_time = total_error_spend_time + error_spend_time
                error_txt = error_txt + ''' Host: %s   | Spend time: %s Sec | Result: failed  \n ''' % (
                    k, str(error_spend_time))
                #error_txt=error_txt+'''-----------host: %s error log-----------\n''' %k
                error_txt = error_txt + '''%s \n ----------\n''' % error_log
        if ok_dict.keys() == []:
            success_hosts = ''
        else:
            success_hosts = ','.join(ok_dict.keys())
        if error_dict.keys() == []:
            failed_hosts = ''
        else:
            failed_hosts = ','.join(error_dict.keys())
        total = len(all_key)
        deploylog = AppDeployLogModel(
            user=request.user,
            time=datetime.now(),
            target=target,
            application=app,
            mapping=mapping,
            success_hosts=success_hosts,
            failed_hosts=failed_hosts,
            total=total,
            log=head_txt + success_txt + error_txt,
            duration=str(total_error_spend_time + total_ok_spend_time) + ' s')
        try:
            deploylog.save()
        except:
            pass
        f = open('/tmp/.app_deploy.log', 'w')
        f.write(head_txt + success_txt + error_txt)
        f.close()
        code, log = commands.getstatusoutput('cat /tmp/.app_deploy.log')
        return render_to_response('salt_deploy_application.html',
                                  RequestContext(request, {'log': log}))
    return render_to_response('salt_deploy_application.html',
                              RequestContext(request))
コード例 #29
0
def get_time_series_plot(request):
    context = {'success': False}

    if request.method != 'POST':
        return HttpResponseNotAllowed(['POST'])

    try:
        log.debug(f'POST: {request.POST}')

        platform = request.POST.get('platform', None)
        sensor = request.POST.get('sensor', None)
        product = request.POST.get('product', None)
        start_date = request.POST.get('start_date', None)
        end_date = request.POST.get('end_date', None)
        reducer = request.POST.get('reducer', None)
        index_name = request.POST.get('index_name', None)
        scale = float(request.POST.get('scale', 250))
        geometry_str = request.POST.get('geometry', None)

        # Derived parameters
        ee_product = EE_PRODUCTS[platform][sensor][product]
        display_name = ee_product['display']

        if not index_name:
            index_name = ee_product['index']

        try:
            geometry = geojson.loads(geometry_str)
        except JSONDecodeError:
            raise ValueError('Please draw an area of interest.')

        if index_name is None:
            raise ValueError(
                f"We're sorry, but plotting {display_name} is not supported at this time. Please select "
                f"a different product.")

        time_series = get_time_series_from_image_collection(
            platform=platform,
            sensor=sensor,
            product=product,
            index_name=index_name,
            scale=scale,
            geometry=geometry,
            date_from=start_date,
            date_to=end_date,
            reducer=reducer)

        log.debug(f'Time Series: {time_series}')

        figure = generate_figure(figure_title=display_name,
                                 time_series=time_series)

        plot_view = PlotlyView(figure, height='200px', width='100%')

        context.update({'success': True, 'plot_view': plot_view})

    except ValueError as e:
        context['error'] = str(e)

    except Exception:
        context[
            'error'] = f'An unexpected error has occurred. Please try again.'
        log.exception('An unexpected error occurred.')

    return render(request, 'earth_engine/plot.html', context)
コード例 #30
0
ファイル: views.py プロジェクト: sunhughees/frepple
    def post(cls, request, *args, **kwargs):
        # Allow only post from superusers
        if not request.user.is_superuser:
            return HttpResponseNotAllowed(
                ["post"], content="Only a superuser can execute SQL statements"
            )
        if request.method != "POST" or not request.is_ajax():
            return HttpResponseForbidden("<h1>%s</h1>" % _("Permission denied"))

        if "format" in request.POST:
            formatted = sqlparse.format(
                request.POST.get("sql", ""),
                keyword_case="lower",
                identifier_case="lower",
                strip_comments=False,
                reindent=True,
                wrap_after=50,
                indent_tabs=False,
                indent_width=2,
            )
            return JsonResponse({"formatted": formatted})

        elif "save" in request.POST:
            if "id" in request.POST:
                m = SQLReport.objects.using(request.database).get(pk=request.POST["id"])
                if m.user.id != request.user.id:
                    return HttpResponseForbidden("You're not the owner of this report")
                f = SQLReportForm(request.POST, instance=m)
            else:
                f = SQLReportForm(request.POST)
            if f.is_valid():
                try:
                    m = f.save(commit=False)
                    m.user = request.user
                    m.save()
                    return JsonResponse({"id": m.id, "status": "ok"})
                except Exception as e:
                    return JsonResponse({"id": m.id, "status": str(e)})
            else:
                return HttpResponseServerError("Error saving report")

        elif "delete" in request.POST:
            pk = request.POST["id"]
            SQLReport.objects.using(request.database).filter(
                pk=pk, user=request.user
            ).delete()
            messages.add_message(
                request,
                messages.INFO,
                _('The %(name)s "%(obj)s" was deleted successfully.')
                % {"name": _("my report"), "obj": pk},
            )
            return HttpResponse("ok")

        elif "test" in request.POST:
            return StreamingHttpResponse(
                content_type="application/json; charset=%s" % settings.DEFAULT_CHARSET,
                streaming_content=cls._generate_json_data(
                    database=request.database, sql=request.POST["sql"]
                ),
            )

        else:
            return HttpResponseNotAllowed("Unknown post request")