Ejemplo n.º 1
0
def report_new_ebooks(created=None):  #created= creation date
    if created:
        period = (created, created + timedelta(days=1))
    else:
        period = (date_today() - timedelta(days=1), date_today())
    works = models.Work.objects.filter(
        editions__ebooks__created__range=period).distinct()
    for work in works:
        # only notify if a new ebooks are active, don't notify person if the book was just wished
        # ebooks() only returns active ebooks
        for ebook in work.ebooks().filter(created__range=period):
            notification.send_now(work.wished_by(excluding=period),
                                  "wishlist_unglued_book_released",
                                  {'work': work}, True)
            break
Ejemplo n.º 2
0
def update_account_status(all_accounts=True, send_notice_on_change_only=True):
    """update the status of all Accounts
    
        By default, send notices only if the status is *changing*.  Set send_notice_on_change_only = False to
        send notice based on new_status regardless of old status.  (Useful for initialization)
    """
    errors = []

    if all_accounts:
        accounts_to_calc = Account.objects.all()
    else:
        # active accounts with expiration dates from this month earlier
        today = date_today()
        year = today.year
        month = today.month
        accounts_to_calc = Account.objects.filter(
            Q(date_deactivated__isnull=True)).filter(
                (Q(card_exp_year__lt=year)
                 | Q(card_exp_year=year, card_exp_month__lte=month)))

    for account in accounts_to_calc:
        try:
            account.update_status(
                send_notice_on_change_only=send_notice_on_change_only)
        except Exception, e:
            errors.append(e)
Ejemplo n.º 3
0
    def calculated_status(self):
        """returns ACTIVE, DEACTIVATED, EXPIRED, EXPIRING, or ERROR"""

        # is it deactivated?

        today = date_today()

        if self.date_deactivated is not None:
            return 'DEACTIVATED'

    # is it expired?

        elif self.card_exp_year < today.year or (
                self.card_exp_year == today.year
                and self.card_exp_month < today.month):
            return 'EXPIRED'

    # about to expire?  do I want to distinguish from 'ACTIVE'?

        elif self.card_exp_year == today.year and self.card_exp_month == today.month:
            return 'EXPIRING'

    # any transactions w/ errors after the account date?
    # Transaction.objects.filter(host='stripelib', status='Error', approved=True).count()

        elif Transaction.objects.filter(
                host='stripelib', status='Error', approved=True,
                user=self.user).filter(date_payment__gt=self.date_created):
            return 'ERROR'
        return 'ACTIVE'
Ejemplo n.º 4
0
def rh_tools(request, template_name='rh_intro.html'):
    if not request.user.is_authenticated:
        return render(request, 'rh_intro.html')
    claims = request.user.claim.filter(user=request.user)
    campaign_form = "xxx"
    if not claims:
        return render(request, template_name)
    for claim in claims:
        if claim.can_open_new:
            if request.method == 'POST' and  \
                    request.POST.has_key('cl_%s-work' % claim.id) and \
                    int(request.POST['cl_%s-work' % claim.id]) == claim.work_id :
                claim.campaign_form = OpenCampaignForm(
                    data = request.POST,
                    prefix = 'cl_'+str(claim.id),
                )
                if claim.campaign_form.is_valid():
                    new_campaign = claim.campaign_form.save(commit=False)
                    if new_campaign.type == BUY2UNGLUE:
                        new_campaign.target = D(settings.UNGLUEIT_MAXIMUM_TARGET)
                        new_campaign.set_cc_date_initial()
                    elif new_campaign.type == REWARDS:
                        new_campaign.deadline = date_today() + timedelta(
                            days=int(settings.UNGLUEIT_LONGEST_DEADLINE)
                        )
                        new_campaign.target = D(settings.UNGLUEIT_MINIMUM_TARGET)
                    elif new_campaign.type == THANKS:
                        new_campaign.target = D(settings.UNGLUEIT_MINIMUM_TARGET)
                    new_campaign.save()
                    claim.campaign_form.save_m2m()
                    claim.campaign_form = None
            else:
                c_type = 2
                claim.campaign_form = OpenCampaignForm(
                    initial={
                        'work': claim.work,
                        'name': claim.work.title,
                        'userid': request.user.id,
                        'managers': [request.user.id],
                        'type': c_type
                    },
                    prefix='cl_'+str(claim.id),
                    )
        if claim.campaign:
            if claim.campaign.status in ['ACTIVE','INITIALIZED']:
                e_m_key = 'edit_managers_%s' % claim.campaign.id
                if request.method == 'POST' and request.POST.has_key(e_m_key):
                    claim.campaign.edit_managers_form = EditManagersForm(
                        instance=claim.campaign,
                        data=request.POST,
                        prefix=claim.campaign.id,
                    )
                    if claim.campaign.edit_managers_form.is_valid():
                        claim.campaign.edit_managers_form.save()
                        claim.campaign.edit_managers_form = EditManagersForm(
                            instance=claim.campaign,
                            prefix=claim.campaign.id,
                        )
                else:
                    claim.campaign.edit_managers_form = EditManagersForm(
                        instance=claim.campaign,
                        prefix=claim.campaign.id,
                    )
    campaigns = request.user.campaigns.all()
    new_campaign = None
    for campaign in campaigns:
        if campaign.clonable():
            if request.method == 'POST' and  request.POST.has_key('c%s-campaign_id'% campaign.id):
                clone_form = CloneCampaignForm(data=request.POST, prefix = 'c%s' % campaign.id)
                if clone_form.is_valid():
                    campaign.clone()
            else:
                campaign.clone_form = CloneCampaignForm(
                    initial={'campaign_id':campaign.id},
                    prefix='c%s' % campaign.id,
                )
    return render(request, template_name, {'claims': claims , 'campaigns': campaigns})
Ejemplo n.º 5
0
def handle_transaction_charged(sender, transaction=None, **kwargs):
    if transaction == None:
        return
    transaction._current_total = None
    context = {
        'transaction': transaction,
        'current_site': Site.objects.get_current()
    }
    if not transaction.campaign:
        if transaction.user:
            notification.send([transaction.user], "donation", context, True)
        elif transaction.receipt:
            from regluit.core.tasks import send_mail_task
            message = render_to_string("notification/donation/full.txt",
                                       context)
            send_mail_task.delay('unglue.it donation confirmation', message,
                                 '*****@*****.**', [transaction.receipt])
    elif transaction.campaign.type is REWARDS:
        notification.send([transaction.user], "pledge_charged", context, True)
    elif transaction.campaign.type is BUY2UNGLUE:
        # provision the book
        Acq = apps.get_model('core', 'Acq')
        if transaction.offer.license == LIBRARY:
            library = Library.objects.get(id=transaction.extra['library_id'])
            new_acq = Acq.objects.create(user=library.user,
                                         work=transaction.campaign.work,
                                         license=LIBRARY)
            if transaction.user_id != library.user_id:  # don't put it on reserve if purchased by the library
                reserve_acq = Acq.objects.create(
                    user=transaction.user,
                    work=transaction.campaign.work,
                    license=RESERVE,
                    lib_acq=new_acq)
                reserve_acq.expire_in(datetime.timedelta(hours=2))
            copies = int(transaction.extra.get('copies', 1))
            while copies > 1:
                Acq.objects.create(user=library.user,
                                   work=transaction.campaign.work,
                                   license=LIBRARY)
                copies -= 1
        else:
            if transaction.extra.get('give_to', False):
                # it's a gift!
                Gift = apps.get_model('core', 'Gift')
                giftee = Gift.giftee(transaction.extra['give_to'],
                                     str(transaction.id))
                new_acq = Acq.objects.create(user=giftee,
                                             work=transaction.campaign.work,
                                             license=transaction.offer.license)
                gift = Gift.objects.create(acq=new_acq,
                                           message=transaction.extra.get(
                                               'give_message', ''),
                                           giver=transaction.user,
                                           to=transaction.extra['give_to'])
                context['gift'] = gift
                notification.send([giftee], "purchase_gift", context, True)
            else:
                new_acq = Acq.objects.create(user=transaction.user,
                                             work=transaction.campaign.work,
                                             license=transaction.offer.license)
        transaction.campaign.update_left()
        notification.send([transaction.user], "purchase_complete", context,
                          True)
        from regluit.core.tasks import watermark_acq
        watermark_acq.delay(new_acq.id)
        if transaction.campaign.cc_date < date_today():
            transaction.campaign.update_status(send_notice=True)
    elif transaction.campaign.type is THANKS:
        if transaction.user:
            Acq = apps.get_model('core', 'Acq')
            new_acq = Acq.objects.create(user=transaction.user,
                                         work=transaction.campaign.work,
                                         license=THANKED)
            notification.send([transaction.user], "purchase_complete", context,
                              True)
        elif transaction.receipt:
            from regluit.core.tasks import send_mail_task
            message = render_to_string(
                "notification/purchase_complete/full.txt", context)
            send_mail_task.delay('unglue.it transaction confirmation', message,
                                 '*****@*****.**', [transaction.receipt])
    from regluit.core.tasks import emit_notifications
    emit_notifications.delay()