def test_post_to_slack_with_error(mock_post, mock_send_mail):
    mock_post.side_effect = RequestException
    with pytest.raises(utils.NetworkError):
        utils.post_to_slack(mock_post, 'webhook')
        mock_send_mail.assert_called_once(
            'An error occurred while attempting to to post to slack',
            admin_email)
def post_menu_to_slack(lunches_dict, webhook=slack_webhook):
    if utils.check_lunches(lunches_dict):
        post = utils.lunches_dict_to_slack_post(lunches_dict)
        utils.post_to_slack(post, webhook)
        return True
    else:
        return False
Ejemplo n.º 3
0
    def get(self, request, *args, **kwargs):
        try:
            self.object = self.get_object()
        except:
            messages.error(self.request, 'That issue was not found.')
            return redirect("/")

        if self.request.GET.get('paymentId'):
            import paypalrestsdk
            paypalrestsdk.configure({
                'mode': settings.MODE,
                'client_id': settings.CLIENT_ID,
                'client_secret': settings.CLIENT_SECRET
            })

            payment = paypalrestsdk.Payment.find(
                self.request.GET.get('paymentId'))

            custom = payment.transactions[0].custom

            if payment.execute({"payer_id": self.request.GET.get('PayerID')}):
                send_mail(
                    'Processed Payment PayerID' +
                    self.request.GET.get('PayerID') + " paymentID" +
                    self.request.GET.get('paymentId'),
                    "payment occured",
                    '*****@*****.**',
                    ['*****@*****.**'],
                    html_message="payment occured",
                )
                for obj in serializers.deserialize("json",
                                                   custom,
                                                   ignorenonexistent=True):
                    obj.object.created = datetime.datetime.now()
                    obj.object.checkout_id = self.request.GET.get(
                        'checkout_id')
                    obj.save()
                    action.send(self.request.user,
                                verb='placed a $' + str(obj.object.price) +
                                ' bounty on ',
                                target=obj.object.issue)
                    post_to_slack(obj.object)
                    if not settings.DEBUG:
                        create_comment(obj.object.issue)
            else:
                messages.error(request, payment.error)
                send_mail(
                    'Payment error: PayerID' +
                    self.request.GET.get('PayerID') + " paymentID" +
                    self.request.GET.get('paymentId'),
                    "payment error",
                    '*****@*****.**',
                    ['*****@*****.**'],
                    html_message="payment error",
                )

        return super(IssueDetailView, self).get(request, *args, **kwargs)
Ejemplo n.º 4
0
    def get(self, request, *args, **kwargs):
        try:
            self.object = self.get_object()
        except Http404:
            messages.error(self.request, "That issue was not found.")
            return redirect("/")

        if self.request.GET.get("paymentId"):
            import paypalrestsdk

            paypalrestsdk.configure(
                {"mode": settings.MODE, "client_id": settings.CLIENT_ID, "client_secret": settings.CLIENT_SECRET}
            )

            payment = paypalrestsdk.Payment.find(self.request.GET.get("paymentId"))

            custom = payment.transactions[0].custom

            if payment.execute({"payer_id": self.request.GET.get("PayerID")}):
                send_mail(
                    "Processed Payment PayerID"
                    + self.request.GET.get("PayerID")
                    + " paymentID"
                    + self.request.GET.get("paymentId"),
                    "payment occured",
                    "*****@*****.**",
                    ["*****@*****.**"],
                    html_message="payment occured",
                )
                for obj in serializers.deserialize("json", custom, ignorenonexistent=True):
                    obj.object.created = datetime.datetime.now()
                    obj.object.checkout_id = self.request.GET.get("checkout_id")
                    obj.save()
                    action.send(
                        self.request.user,
                        verb="placed a $" + str(obj.object.price) + " bounty on ",
                        target=obj.object.issue,
                    )
                    post_to_slack(obj.object)
                    if not settings.DEBUG:
                        create_comment(obj.object.issue)
            else:
                messages.error(request, payment.error)
                send_mail(
                    "Payment error: PayerID"
                    + self.request.GET.get("PayerID")
                    + " paymentID"
                    + self.request.GET.get("paymentId"),
                    "payment error",
                    "*****@*****.**",
                    ["*****@*****.**"],
                    html_message="payment error",
                )

        return super(IssueDetailView, self).get(request, *args, **kwargs)
Ejemplo n.º 5
0
def post_article():
    # Get one article from the current batch
    try:
        article_of_the_day = retrieve_article()
    except Exception as err:
        error_message = {
            'text':
            '@edwin Something went wrong retrieving the Article of the Day:\n%s'
            % err,
            'link_names':
            1
        }
        try:
            res = post_to_slack(error_message)
        except:
            res = 'failed'
        sys.exit(
            'Something went wrong retrieving the Article of the Day. Post to Slack: %s'
            % res)
    # Now we can start posting the article
    # 1. post to Twitter
    error_message = {}
    try:
        twitter = post_to_twitter(article_of_the_day)
    except Exception as err:
        error_message = {
            'text':
            '@edwin Something went wrong posting the Article of the Day to Twitter:\n%s'
            % err,
            'link_names':
            1
        }
    if not twitter:
        error_message = {
            'text':
            '@edwin Unable to post the Article of the Day to Twitter:\n%s' %
            twitter,
            'link_names':
            1
        }
    if error_message:
        try:
            res = post_to_slack(error_message)
        except:
            res = 'failed'
        sys.exit(
            'Something went wrong posting the Article of the Day to Twitter. Post to Slack: %s'
            % res)
    # With a successful post we add this article to the library containing all the articles
    # that have been posted
    res = update_main_library(article_of_the_day['bibcode'])
Ejemplo n.º 6
0
    def get(self, request, *args, **kwargs):
        if self.request.GET.get('checkout_id'):
            wepay = WePay(settings.WEPAY_IN_PRODUCTION, settings.WEPAY_ACCESS_TOKEN)
            wepay_data = wepay.call('/checkout/', {
                'checkout_id': self.request.GET.get('checkout_id'),
            })

            for obj in serializers.deserialize("xml", wepay_data['long_description'], ignorenonexistent=True):
                obj.object.created = datetime.datetime.now()
                obj.object.checkout_id = self.request.GET.get('checkout_id')
                obj.save()
                action.send(self.request.user, verb='placed a $' + str(obj.object.price) + ' bounty on ', target=obj.object.issue)
                post_to_slack(obj.object)

        return super(IssueDetailView, self).get(request, *args, **kwargs)
Ejemplo n.º 7
0
    def get(self, request, *args, **kwargs):
        try:
            self.object = self.get_object()
        except Http404:
            messages.error(self.request, 'That issue was not found.')
            return redirect("/")
        context = self.get_context_data(object=self.object)
        return self.render_to_response(context)
        if self.request.GET.get('paymentId'):
            import paypalrestsdk
            paypalrestsdk.configure({
                'mode': settings.MODE,
                'client_id': settings.CLIENT_ID,
                'client_secret': settings.CLIENT_SECRET
            })

            payment = paypalrestsdk.Payment.find(self.request.GET.get('paymentId'))

            custom = payment.transactions[0].custom

            if payment.execute({"payer_id": self.request.GET.get('PayerID')}):
                for obj in serializers.deserialize("json", custom, ignorenonexistent=True):
                    obj.object.created = datetime.datetime.now()
                    obj.object.checkout_id = self.request.GET.get('checkout_id')
                    obj.save()
                    action.send(self.request.user, verb='placed a $' + str(obj.object.price) + ' bounty on ', target=obj.object.issue)
                    post_to_slack(obj.object)
                    if not settings.DEBUG:
                        create_comment(obj.object.issue)
            else:
                messages.error(request, payment.error)

        if self.request.GET.get('checkout_id'):
            wepay = WePay(settings.WEPAY_IN_PRODUCTION, settings.WEPAY_ACCESS_TOKEN)
            wepay_data = wepay.call('/checkout/', {
                'checkout_id': self.request.GET.get('checkout_id'),
            })

            for obj in serializers.deserialize("xml", wepay_data['long_description'], ignorenonexistent=True):
                obj.object.created = datetime.datetime.now()
                obj.object.checkout_id = self.request.GET.get('checkout_id')
                obj.save()
                action.send(self.request.user, verb='placed a $' + str(obj.object.price) + ' bounty on ', target=obj.object.issue)
                post_to_slack(obj.object)
                if not settings.DEBUG:
                    create_comment(obj.object.issue)

        return super(IssueDetailView, self).get(request, *args, **kwargs)
Ejemplo n.º 8
0
    def get(self, request, *args, **kwargs):
        try:
            self.object = self.get_object()
        except Http404:
            messages.error(self.request, 'That issue was not found.')
            return redirect("/")
        context = self.get_context_data(object=self.object)
        return self.render_to_response(context)
        if self.request.GET.get('paymentId'):
            import paypalrestsdk
            paypalrestsdk.configure({
                'mode': settings.MODE,
                'client_id': settings.CLIENT_ID,
                'client_secret': settings.CLIENT_SECRET
            })

            payment = paypalrestsdk.Payment.find(self.request.GET.get('paymentId'))

            custom = payment.transactions[0].custom

            if payment.execute({"payer_id": self.request.GET.get('PayerID')}):
                for obj in serializers.deserialize("json", custom, ignorenonexistent=True):
                    obj.object.created = datetime.datetime.now()
                    obj.object.checkout_id = self.request.GET.get('checkout_id')
                    obj.save()
                    action.send(self.request.user, verb='placed a $' + str(obj.object.price) + ' bounty on ', target=obj.object.issue)
                    post_to_slack(obj.object)
                    if not settings.DEBUG:
                        create_comment(obj.object.issue)
            else:
                messages.error(request, payment.error)

        if self.request.GET.get('checkout_id'):
            wepay = WePay(settings.WEPAY_IN_PRODUCTION, settings.WEPAY_ACCESS_TOKEN)
            wepay_data = wepay.call('/checkout/', {
                'checkout_id': self.request.GET.get('checkout_id'),
            })

            for obj in serializers.deserialize("xml", wepay_data['long_description'], ignorenonexistent=True):
                obj.object.created = datetime.datetime.now()
                obj.object.checkout_id = self.request.GET.get('checkout_id')
                obj.save()
                action.send(self.request.user, verb='placed a $' + str(obj.object.price) + ' bounty on ', target=obj.object.issue)
                post_to_slack(obj.object)
                if not settings.DEBUG:
                    create_comment(obj.object.issue)

        return super(IssueDetailView, self).get(request, *args, **kwargs)
Ejemplo n.º 9
0
 def run(self, **kwargs):
     with create_app().app_context():
         resp = post_article()
         # If 'resp' has a key 'Slack' we have to send
         # a message to Slack
         if 'Slack' in resp:
             error_message = {'text': resp['Slack'], 'link_names': 1}
             try:
                 slack = post_to_slack(error_message)
             except:
                 current_app.logger.exception("Failed to post to Slack")
Ejemplo n.º 10
0
    def get(self, request, *args, **kwargs):
        if self.request.GET.get('checkout_id'):
            wepay = WePay(settings.WEPAY_IN_PRODUCTION,
                          settings.WEPAY_ACCESS_TOKEN)
            wepay_data = wepay.call(
                '/checkout/', {
                    'checkout_id': self.request.GET.get('checkout_id'),
                })

            for obj in serializers.deserialize("xml",
                                               wepay_data['long_description'],
                                               ignorenonexistent=True):
                obj.object.created = datetime.datetime.now()
                obj.object.checkout_id = self.request.GET.get('checkout_id')
                obj.save()
                action.send(self.request.user,
                            verb='placed a $' + str(obj.object.price) +
                            ' bounty on ',
                            target=obj.object.issue)
                post_to_slack(obj.object)
                if not settings.DEBUG:
                    create_comment(obj.object.issue)

        return super(IssueDetailView, self).get(request, *args, **kwargs)
Ejemplo n.º 11
0
def generate_batch():
    # Initialize data structures
    ## The current date
    current_date = datetime.now()
    ## The dictionary that will hold the bibcodes in each cluster
    cluster_members = defaultdict(list)
    ## The dictionary that contains the label for each cluster
    cluster_labels = {}
    ## The dictionary that contains the necessary metadata for each bibcode
    graph = {}
    ## The list that will hold the candidates for the new batch
    candidates = []
    ## bibstems list to avoid papers from the same journal
    bibstems = []
    # Retrieve the initial metadata from Solr (specify a year range)
    # Include the previous year in January
    if current_date.month == 1:
        year_range = "%s-%s" % (current_date.year - 1, current_date.year)
    else:
        year_range = str(current_date.year)
    data = get_data(year_range)
    # From the initial dataset, get the actual candidates by
    # 1. removing all publications that we used previously
    clean_data = cleanup_data(data)
    # Create a paper network based on the candidates found
    # This network will be segmented into clusters. These clusters will be used to find candidates.
    visdata = paper_network.get_papernetwork(
        clean_data, current_app.config.get("MAX_GROUPS"))
    # Use the network to determine the new batch. The clustering is stored in the
    # "summaryGraph" attribute, while the complete network in stored in "fullGraph".
    #
    # For each cluster, retrieve the keywords that describe its contents.
    # It is possible not enough information is available to retrieve keywords
    for summary_node in visdata['summaryGraph']['nodes']:
        cluster_labels[summary_node['node_name']] = list(
            summary_node['node_label'].keys())
    # Every node in the complete network represents a publication. The node name is the bibcode
    # of the publication. The weight of the node within the network is determined from its
    # indegree (number of citations), the number of authors and the 90-day reads. The weight
    # closely resembles the "classic factor".
    for node in visdata['fullGraph']['nodes']:
        autnum = max(node['author_count'], 1)
        citnum = node['citation_count']
        rdsnum = node['read_count']
        try:
            weight = math.log10(1 + (float(citnum + rdsnum) / float(autnum)))
        except:
            weight = float(node['cite_read_boost'])
        cluster_members[node['group']].append((node['node_name'], weight))
        # Here we store all attributes of each node to be accessed later on
        graph[node['node_name']] = node
    # Now we cycle through the clusters and build a list of candidates
    for cluster, bibset in cluster_members.items():
        candidate = bibset[0][0]
        # we do a minimal effort to try to avoid multiple candidates from 1 journal
        if candidate[4:9] in bibstems:
            candidate = bibset[1][0]
        candidates.append((cluster, candidate))
        bibstems.append(candidate[4:9])
    # The new batch is a random pick of 5 from the candidates
    new_batch = sample(candidates, 5)
    # If the new batch has less than 5 articles, sound the alarm
    if len(new_batch) < 5:
        error_message = {
            'text':
            '@edwin Found only %s articles instead of 5! Check!' %
            len(new_batch),
            'link_names':
            1
        }
        try:
            res = post_to_slack(error_message)
        except:
            res = 'failed'
        sys.exit('AoD batch is too small. Post to Slack: %s' % res)
    # Store the new batch in the appropriate ADS Library
    try:
        saved_batch = save_new_batch(new_batch)
    except Exception as err:
        error_message = {
            'text':
            '@edwin Something went wrong saving the current AoD batch:\n%s' %
            err,
            'link_names':
            1
        }
        try:
            res = post_to_slack(error_message)
        except:
            res = 'failed'
        sys.exit(
            'Something went wrong saving the current AoD batch. Post to Slack: %s'
            % res)
    # Check that 5 records were posted
    try:
        number_added = saved_batch['number_added']
    except:
        number_added = 0
    if number_added != 5:
        error_message = {
            'text':
            '@edwin Something went wrong saving the current AoD batch! Please check!',
            'link_names': 1
        }
        try:
            res = post_to_slack(error_message)
        except:
            res = 'failed'
        sys.exit(
            'Something went wrong saving the current AoD batch. Post to Slack: %s'
            % res)
    # Email the overview of the new batch.
    # For each candidate, include the keywords of the cluster it came from
    subject = '<%s|Articles of the Day - batch %s/%s/%s>' % (
        saved_batch['library_url'], current_date.month, current_date.day,
        current_date.year)
    message = '```'
    for entry in new_batch:
        try:
            label = "; ".join(
                [l.decode("utf-8") for l in cluster_labels[entry[0]]])
        except:
            label = "NA"
        message += "%s\tlabel: %s\n" % (entry[1], label)
    message += '```'
    post_message = {
        'text': '@edwin %s\nEntries:\n%s' % (subject, message),
        'link_names': 1
    }
    try:
        res = post_to_slack(post_message)
    except:
        res = 'failed'
        sys.stderr.write(
            'Something went wrong posting the current AoD batch to Slack.')
Ejemplo n.º 12
0
def create_issue_and_bounty(request):
    languages = []
    for lang in Issue.LANGUAGES:
        languages.append(lang[0])
    user = request.user
    if not user.is_authenticated():
        return render(request, 'post.html', {
            'languages': languages,
            'message': 'You need to be authenticated to post bounty'
        })
    if request.method == 'GET':
        return render(request, 'post.html', {
            'languages': languages,
        })
    if request.method == 'POST':
        url = request.POST.get('issueUrl','')
        if not url:
            messages.error(request, 'Please provide an issue url')
            return render(request, 'post.html', {
                'languages': languages
            })
        issue_data = get_issue(request, url)
        if issue_data:
            service = Service.objects.get(name=issue_data['service'])
            instance = Issue(created = user,number = issue_data['number'],
            project=issue_data['project'],user = issue_data['user'],service=service)
        else:
            return render(request, 'post.html', {
                'languages': languages,
                'message':'Please provide a propper issue url',
            })
        form = IssueCreateForm(request.POST, instance=instance)
        bounty_form = BountyCreateForm(request.POST)
        bounty_form_is_valid = bounty_form.is_valid()
        if form.is_valid() and bounty_form_is_valid:
            if not instance.pk: 
                issue = form.save()
            else:
                issue = instance
                #issue already exists, post additional bounty
                #this doesn't seem to be working yet
            price = bounty_form.cleaned_data['price']
            bounty_instance = Bounty(user = user,issue = issue,price = price)
            #save this data and post it with the return_uri from wepay
            data = serializers.serialize('xml', [ bounty_instance, ])
            bounty_instance.save()

            wepay = WePay(settings.WEPAY_IN_PRODUCTION, settings.WEPAY_ACCESS_TOKEN)
            wepay_data = wepay.call('/checkout/create', {
                'account_id': settings.WEPAY_ACCOUNT_ID,
                'amount': request.POST.get('grand_total'),
                'short_description': 'CoderBounty',
                'long_description': data,
                'type': 'service',
                'currency': 'USD'
            })
            print wepay_data

            #return redirect(wepay_data['checkout_uri'])

            post_to_slack(bounty_instance)

            return render(request, 'post.html', {
                'languages': languages,
                'message':'Successfully saved issue'
            })
        else:
            return render(request, 'post.html', {
                'languages': languages,
                'message':'Error',
                'errors': form.errors,
                'bounty_errors':bounty_form.errors,
            })