예제 #1
0
def remove_old_backups(dest):
    '''Only keep the backupdate that are less than a month old'''
    str_now = now.strftime("%d-%m-%Y")

    try:
        arch_date = now - MonthDelta(1)
        directories = [
            os.path.join(dest, name) for name in os.listdir(dest)
            if os.path.isdir(os.path.join(dest, name))
        ]
        old_dirs = [
            name.replace('\\', '/') for name in directories
            if os.path.getctime(name) < time.mktime(arch_date.timetuple())
        ]
        for name in old_dirs:
            print('Removing ' + name)
            shutil.rmtree(name, ignore_errors=True)
            dir_count = 0
            file_count = 0
            for _, dirs, files in os.walk(name):
                dir_count += len(dirs)
                file_count += len(files)
            if (dir_count + file_count) == 0:
                os.rmdir(name)
            print('Completed')
    except OSError as e:
        logging.error(str_now + " Exception Occured", exc_info=True)
예제 #2
0
파일: core.py 프로젝트: usood/nadine
    def activity_this_month(self, test_date=None):
        if not test_date:
            test_date = date.today()

        membership = self.active_membership()
        if membership:
            if membership.guest_of:
                # Return host's activity
                host = membership.guest_of
                return host.activity_this_month()
            month_start = membership.prev_billing_date(test_date)
        else:
            # Just go back one month from this date since there isn't a membership to work with
            month_start = test_date - MonthDelta(1)
        # print month_start

        activity = []
        for m in [self] + self.guests():
            for l in DailyLog.objects.filter(member=m,
                                             payment='Bill',
                                             visit_date__gte=month_start):
                activity.append(l)
        for l in DailyLog.objects.filter(guest_of=self,
                                         payment='Bill',
                                         visit_date__gte=month_start):
            activity.append(l)
        return activity
예제 #3
0
def get_total_amounts(receiving_lines, invoices, payments, start_date, end_date, vendors, company):
    #上期开始日期
    last_month_start_date = None
    #上期结束日期
    last_month_end_date = None
    
    if start_date:
        delta = datetime.timedelta(days=1)
        if not isinstance(start_date, datetime.date):
            new_start_date = datetime.datetime.strptime(start_date, "%Y-%m-%d").date()
        else:
            new_start_date = start_date
        #前一天
        before_date = new_start_date - delta
        last_month_start_date = new_start_date - MonthDelta(1)
        last_month_end_date = before_date
    
    
    #上期欠款金额  = 上个月欠款
    last_term_owed_amount = getOwedAmountByYear(last_month_start_date.year, last_month_start_date.month, vendors, company)
    
    #累计应付款 = 本期供货金额 - 本期付款金额 + 上期欠款金额
    total_owed_amount = (receiving_lines['unchecked_account_sum'] or 0) - (payments['payments_sum'] or 0) + (last_term_owed_amount or 0)
    
    #上期欠发票  = 上个月欠发票
    last_term_owed_invoices = getOwedInvoiceByYear(last_month_start_date.year, last_month_start_date.month, vendors, company)
    
    #累计应开票 = 本期供货金额 - 本期开票金额 + 上期欠发票
    total_owed_invoices = (receiving_lines['unchecked_account_sum'] or 0) - (invoices['invoices_sum'] or 0) + (last_term_owed_invoices or 0)
    result = {'last_term_owed_amount' : last_term_owed_amount, 
              'total_owed_amount': total_owed_amount,
              'last_term_owed_invoices': last_term_owed_invoices,
              'total_owed_invoices': total_owed_invoices}
    
    return result
예제 #4
0
def create_mort_schedule(session):
    schs = [
        414.40,
        415.91,
        417.43,
        418.95,
        420.48,
        422.01,
        423.55,
        425.09,
        426.64,
        428.2,
        429.76,
        431.33,
        432.90,
        434.48,
        436.06,
        437.65,
    ]

    md = MonthDelta(1)
    d = datetime.date(2012, 1, 1)
    for s in schs:
        session.add( MortgageSchedule(d, s) )
        d = d + md
예제 #5
0
    def obj_create(self, bundle, **kwargs):
        bundle = super(OrderResource, self).obj_create(bundle, **kwargs)

        # get the package
        package = None
        if 'package' in bundle.obj.items:
            package = Package.objects.get(pk=bundle.obj.items['package'])

        # set the actual total
        bundle.obj.calculate()
        bundle.obj.save()

        if 'event' in bundle.data:
            event_resource = EventResource()
            event = event_resource.get_via_uri(bundle.data['event'], request=bundle.request)
            event.is_enabled = True
            event.save()

        # update the account
        if package is not None and package.interval is not None:
            # calculate the valid until date
            expire = None
            now = datetime.now(tz=pytz.UTC)
            trialdays = timedelta(package.trial_period_days)
            if package.interval == Package.INTERVAL_YEAR:
                expire = now + MonthDelta(12 * int(package.interval_count)) + trialdays
            elif package.interval == Package.INTERVAL_MONTH:
                expire = now + MonthDelta(int(package.interval_count)) + trialdays
            elif package.interval == Package.INTERVAL_WEEK:
                expire = now + timedelta(7 * int(package.interval_count)) + trialdays
            else:
                expire = now + trialdays

            # modify the account
            account = bundle.obj.account
            account.package = package
            account.valid_until = expire
            account.save()

        if 'stripeToken' in bundle.data:
            bundle.obj.charge(stripe_token=bundle.data['stripeToken'])

        ## send the receipt ##
        bundle.obj.send_email()

        return bundle
예제 #6
0
def tritransactions(category_id, month):
    yyyy, mm = map(int, month.split('-'))
    date = datetime.date(yyyy, mm, 1) - MonthDelta(1)

    cat_ids = [category_id] + \
        [c.id for c in allChildren(db.session.query(Category).filter(Category.id==category_id).first())]

    tables = []
    for _ in range(3):
        tables.append( db.session.query(Transaction).join(Category)\
                .filter(Category.id.in_(cat_ids),
                    Transaction.bdate>=date,
                    Transaction.bdate<date + MonthDelta(1),
                ).order_by(Transaction.bdate).all()
        )

        date += MonthDelta(1)

    return render_template('transactions.html', form=None, transactions=tables)
예제 #7
0
def membership(request, member_id):
    member = get_object_or_404(Member, pk=member_id)

    start = today = timezone.localtime(timezone.now()).date()
    last_membership = member.last_membership()
    if last_membership and last_membership.end_date and last_membership.end_date > today - timedelta(
            days=10):
        start = (member.last_membership().end_date + timedelta(days=1))
    last = start + MonthDelta(1) - timedelta(days=1)

    if request.method == 'POST':
        membership_form = MembershipForm(request.POST, request.FILES)
        try:
            if membership_form.is_valid():
                membership_form.created_by = request.user
                membership_form.save()
                return HttpResponseRedirect(
                    reverse('staff.views.member.detail',
                            args=[],
                            kwargs={'member_id': member.id}))
        except Exception as e:
            messages.add_message(request, messages.ERROR, e)
    else:
        membership_form = MembershipForm(initial={
            'member': member_id,
            'start_date': start
        })

    # Send them to the update page if we don't have an end date
    if (member.last_membership() and not member.last_membership().end_date):
        return HttpResponseRedirect(
            reverse('staff.views.core.membership',
                    args=[],
                    kwargs={'membership_id': member.last_membership().id}))
    plans = MembershipPlan.objects.filter(enabled=True).order_by('name')
    return render_to_response('staff/membership.html', {
        'member': member,
        'membership_plans': plans,
        'membership_form': membership_form,
        'today': today.isoformat(),
        'last': last.isoformat()
    },
                              context_instance=RequestContext(request))
예제 #8
0
    def to_bucket(self, timestamp, steps=0):
        '''
    Calculate the bucket from a timestamp.
    '''
        dt = datetime.utcfromtimestamp(timestamp)

        if steps != 0:
            if self._step == 'daily':
                dt = dt + timedelta(days=steps)
            elif self._step == 'weekly':
                dt = dt + timedelta(weeks=steps)
            elif self._step == 'monthly':
                dt = dt + MonthDelta(steps)
            elif self._step == 'yearly':
                year = int(dt.strftime(self.FORMATS[self._step]))
                year += steps
                dt = datetime(year=year, month=1, day=1)

        return int(dt.strftime(self.FORMATS[self._step]))
예제 #9
0
def member_membership(request, member_id):
	member = get_object_or_404(Member, pk=member_id)
	
	if request.method == 'POST':
		membership_form = MembershipForm(request.POST, request.FILES)
		if membership_form.is_valid():
			membership_form.save()
			return HttpResponseRedirect(reverse('staff.views.member_detail', args=[], kwargs={'member_id':member.id}))
	
	# Send them to the update page if we don't have an end date
	if (member.last_membership() and not member.last_membership().end_date):
		return HttpResponseRedirect(reverse('staff.views.membership', args=[], kwargs={'membership_id':member.last_membership().id}))
	
	start = today = timezone.localtime(timezone.now()).date()
	if member.last_membership() and member.last_membership().end_date:
		start = (member.last_membership().end_date + timedelta(days=1))
	last = start + MonthDelta(1) - timedelta(days=1)
	return render_to_response('staff/membership.html', {'member':member, 'membership_plans':MembershipPlan.objects.all(), 
		'membership_form':MembershipForm(initial={'member':member_id, 'start_date':start}), 'today':today.isoformat(), 'last':last.isoformat()}, context_instance=RequestContext(request))
예제 #10
0
def add_membership(request, username):
    user = get_object_or_404(User, username=username)

    start = today = timezone.localtime(timezone.now()).date()
    last_membership = user.profile.last_membership()
    if last_membership and last_membership.end_date and last_membership.end_date > today - timedelta(
            days=10):
        start = (last_membership.end_date + timedelta(days=1))
    last = start + MonthDelta(1) - timedelta(days=1)

    if request.method == 'POST':
        membership_form = MembershipForm(request.POST, request.FILES)
        try:
            if membership_form.is_valid():
                membership_form.created_by = request.user
                membership_form.save()
                return HttpResponseRedirect(
                    reverse('staff:user:detail', kwargs={'username':
                                                         username}))
        except Exception as e:
            messages.add_message(request, messages.ERROR, e)
    else:
        membership_form = MembershipForm(initial={
            'username': username,
            'start_date': start
        })

    # Send them to the update page if we don't have an end date
    if (last_membership and not last_membership.end_date):
        return HttpResponseRedirect(
            reverse('staff:user:membership',
                    kwargs={'membership_id': last_membership.id}))

    plans = MembershipPlan.objects.filter(enabled=True).order_by('name')
    context = {
        'user': user,
        'membership_plans': plans,
        'membership_form': membership_form,
        'today': today.isoformat(),
        'last': last.isoformat()
    }
    return render(request, 'staff/user/membership.html', context)
예제 #11
0
    def activity_this_month(self, test_date=None):
        if not test_date:
            test_date = date.today()

        membership = self.active_membership()
        if membership:
            if membership.paid_by:
                # Return host's activity
                host = membership.paid_by
                return host.profile.activity_this_month()
            month_start = membership.prev_billing_date(test_date)
        else:
            # Just go back one month from this date since there isn't a membership to work with
            month_start = test_date - MonthDelta(1)

        activity = []
        for h in [self.user] + self.guests():
            for l in CoworkingDay.objects.filter(user=h, payment='Bill', visit_date__gte=month_start).exclude(paid_by__isnull=False):
                activity.append(l)
        for l in CoworkingDay.objects.filter(paid_by=self.user, payment='Bill', visit_date__gte=month_start):
            activity.append(l)
        return activity
예제 #12
0
def folderSetup(startDate, endDate):
    SR_dates = []
    absolute_file_path = "*scrubbed*"
    if (not os.path.exists('%s/reports/%s-%s/empty' %
                           (absolute_file_path, startDate.strftime("%m.%Y"),
                            endDate.strftime("%m.%Y")))):

        os.makedirs('%s/reports/%s-%s/empty' %
                    (absolute_file_path, startDate.strftime("%m.%Y"),
                     endDate.strftime("%m.%Y")))
    if (not os.path.exists('%s/reports/%s-%s/mm' %
                           (absolute_file_path, startDate.strftime("%m.%Y"),
                            endDate.strftime("%m.%Y")))):

        os.makedirs('%s/reports/%s-%s/mm' %
                    (absolute_file_path, startDate.strftime("%m.%Y"),
                     endDate.strftime("%m.%Y")))

    while (startDate < endDate):
        SR_dates.append(startDate)
        startDate = startDate + MonthDelta(1)
    SR_dates.append(endDate)

    return SR_dates
예제 #13
0
	def next_billing_date(self, test_date=date.today()):
		return self.prev_billing_date(test_date) + MonthDelta(1)
예제 #14
0
 def stale_member_date(self):
     three_months_ago = timezone.now() - MonthDelta(3)
     return three_months_ago
예제 #15
0
def prev_month(date, length):
    """Back one month and preserve day if possible"""
    return date + MonthDelta(-length)
예제 #16
0
def get_receiving_lines(start_date, end_date, vendors, company):
    
    args = build_receiving_parameters(start_date, end_date, vendors, company)
    
    receivingLines = ReceivingLine.objects.filter(*(args,)).order_by('receiving_date', 
                                                                     'orderLine__documentLineItem__projectMaterial__project__name',
                                                                     'orderLine__documentLineItem__projectMaterial__material__category__name',
                                                                     'orderLine__documentLineItem__projectMaterial__material__name',
                                                                     'orderLine__documentLineItem__projectMaterial__material__specification')
     
    
    #查找6个月之前没有对帐的
    args = Q()
    if start_date:
        before_six_month = datetime.datetime.strptime(start_date, '%Y-%m-%d').date() - MonthDelta(6)
        
        #包括所有的子供应商
        args = build_receiving_parameters(before_six_month, start_date, vendors, company)
        
        before_six_month_receivingLines = ReceivingLine.objects.filter(*(args,)).filter(checkAccount__isnull=True).order_by('receiving_date', 
                                                                                                                         'orderLine__documentLineItem__projectMaterial__project__name',
                                                                                                                         'orderLine__documentLineItem__projectMaterial__material__category__name',
                                                                                                                         'orderLine__documentLineItem__projectMaterial__material__name',
                                                                                                                         'orderLine__documentLineItem__projectMaterial__material__specification')
        receivingLines = before_six_month_receivingLines | receivingLines
        
    
    return combine_lines(receivingLines)
예제 #17
0
def add_month(date, length):
    return date + MonthDelta(+ length)
예제 #18
0
def prev_year(date):
    return date + MonthDelta(-12)
예제 #19
0
 def next_billing_date(self, test_date=None):
     if not test_date:
         test_date = date.today()
     return self.prev_billing_date(test_date) + MonthDelta(1)
예제 #20
0
def get_sum_by_project_and_company(start_date, end_date, vendors, company):
    ids = []
    for vendor in vendors:
        ids.append(str(vendor.id))
    
    query = """SELECT company.name AS company_name, 
                      project.name AS project_name, 
                      sum(receiving.total) AS total 
               FROM order_receivingline receiving
               JOIN order_orderline orderline ON
               receiving.orderLine_id = orderline.id
               JOIN order_order orders ON
               orderline.order_id = orders.id
               JOIN project_project project ON
               orders.project_id = project.id
               JOIN company_company company ON
               project.company_id = company.id
               JOIN material_vendor vendor ON
               orders.vendor_id = vendor.id
               WHERE receiving.checkAccount_id IS NULL
               """ 
    
    if bool(vendors):
        query = query + "AND vendor.id in %s  """ % str(ids).replace("[", "(").replace("]", ")")
        
     
    if start_date:
        before_six_month = datetime.datetime.strptime(start_date, '%Y-%m-%d').date() - MonthDelta(6)
        query = query + "AND receiving.receiving_date >= '%s'" %before_six_month
         
    if end_date:
        query = query + "AND receiving.receiving_date <= '%s'" %end_date
     
     
    if company:
        query = query + "AND company.name = '%s'" % company
     
    query = query + " GROUP BY project.id"
    return combin_sum_by_project_and_company(query)
예제 #21
0
def getDateRange(args):
    dateRange = []
    while True:
        if args.start:
            startMonth = args.start[0]
        else:
            startMonth = raw_input("Enter starting month: ")
            startMonth = startMonth.zfill(2)
            #startMonth = "07"
        if not re.search("^\d\d$", startMonth):
            if args.start:
                    del args.start[:]
            if re.search("^00\d$", startMonth):
                print "You seem to have entered a secret agent rather than a month..."
                continue
            print "That's not a valid month, please enter again. (e.g. 01 or 6)"
            continue
        break
    while True:
        if args.start:
            startYear = args.start[1]
        else:
            startYear = raw_input("Enter starting year: ")
            #startYear = "2009"
        if not re.search("^\d\d\d\d$", startYear):
            if args.start:
                    del args.start[:]
            print "That's not a valid year, please enter again. (e.g. 1998)"
            continue
        break
    while True:
        if args.end:
            endMonth = args.end[0]
        else:
            endMonth = raw_input("Enter end month: ")
            endMonth = endMonth.zfill(2)
            #endMonth = "11"
        if not re.search("^\d\d$", endMonth):
            if args.end:
                del args.end[:]
            if re.search("^00\d$", endMonth):
                print "You seem to have entered a secret agent rather than a month..."
                continue
            print "That's not a valid month, please enter again. (e.g. 01 or 6)"
            continue
        break
    while True:
        if args.end:
            endYear = args.end[1]
        else:
            endYear = raw_input("Enter end year: ")
            #endYear = "2012"
        if not re.search("^\d\d\d\d$", endYear):
            if args.end:
                del args.end[:]
            print "That's not a valid year, please enter again. (e.g. 1998)"
            continue
        break
    #Generate Date range
    startDate = date(int(startYear), int(startMonth), 1)
    endDate = date(int(endYear), int(endMonth), 1)
    while(startDate < endDate):
        dateRange.append(startDate)
        startDate = startDate + MonthDelta(1)
    dateRange.append(endDate)
    return dateRange
예제 #22
0
	def checkBuildModel(self):
		""" Checks if any repo is awaiting to build model. 
			We are using a queue because we can't concurrently access R """

		session = Session()

		if self.modelQueue.empty() != True:
			repo_id = self.modelQueue.get()
			repo = (session.query(Repository).filter(Repository.id == repo_id).first())

			# use data only up to X months prior we won't have sufficent data to build models
			# as there may be bugs introduced in those months that haven't been fixed, skewing
			# our model.
			glm_model_time =  int(config['glm_modeling']['months']) 
			data_months_datetime = datetime.utcnow() - MonthDelta(glm_model_time)
			data_months_unixtime = calendar.timegm(data_months_datetime.utctimetuple())
		
			# all commits for repo prior to current time - glm model time
			training_commits = (session.query(Commit)
						.filter( 
							( Commit.repository_id == repo_id ) &
							( Commit.author_date_unix_timestamp < int(data_months_unixtime))
						)
						.order_by( Commit.author_date_unix_timestamp.desc() )
						.all())

			# all commits for repo after or on current time - glm model time
			testing_commits = (session.query(Commit)
						.filter(
							( Commit.repository_id == repo_id ) &
							( Commit.author_date_unix_timestamp >= int(data_months_unixtime)))
						.all())
	
			try: 
				metrics_generator = MetricsGenerator(repo_id, training_commits, testing_commits)
				metrics_generator.buildAllModels()

				# montly data dump - or rather, every 30 days.
				dump_refresh_date = str(datetime.utcnow() - timedelta(days=30))
				if repo.last_data_dump == None or repo.last_data_dump < dump_refresh_date:
					logging.info("Generating a monthly data dump for repository: " + repo_id)

					# Get all commits for the repository
					all_commits = (session.query(Commit)
						.filter( 
							( Commit.repository_id == repo_id )
						)
						.order_by( Commit.author_date_unix_timestamp.desc() )
						.all())

					metrics_generator.dumpData(all_commits)
					repo.last_data_dump = str(datetime.now().replace(microsecond=0))
					
				# Notify user if repo has never been analyzed previously
				if repo.analysis_date is None:
					self.notify(repo)
	
				logging.info("Repo " + repo_id + " finished analyzing.")
				repo.analysis_date = str(datetime.now().replace(microsecond=0))
				repo.status = "Analyzed"
				session.commit() # update status of repo
				session.close()

			# uh-oh
			except Exception as e:
				logging.exception("Got an exception building model for repository " + repo_id)

				repo.status = "Error"
				session.commit() # update repo status
				session.close()
예제 #23
0
 def stale_members(self):
     three_months_ago = timezone.now() - MonthDelta(3)
     recently_used = DailyLog.objects.filter(
         visit_date__gte=three_months_ago).values('member').distinct()
     return self.daily_members().exclude(id__in=recently_used)
예제 #24
0
class TransactionsForm(Form):
    startdate = DateField('startdate', format='%Y-%m-%d', default=datetime.date.today() - MonthDelta(2))
    enddate   = DateField('enddate', format='%Y-%m-%d', default=datetime.date.today())
    category  = SelectField('category', coerce=int)
예제 #25
0
class DateRangeForm(Form):
    startdate = DateField('startdate', format='%Y-%m-%d', default=datetime.date.today() - MonthDelta(12))
    enddate   = DateField('enddate', format='%Y-%m-%d', default=datetime.date.today())