def month_cal(year, month, month_iter=0): '''generates a month formatted calender from python standard calender lib based on provided month, year Arguments: year {date object} -- python datetime.datetime.now().year month {date object} -- python datetime.datetime.now().month Keyword Arguments: month_iter {int} -- iterater for creating months in the future from datetime.now() (default: {0}) Returns: htmlstring -- html string calendar formatted by month ''' now = datetime.datetime.now() cal = HTMLCalendar(firstweekday=6) cal = cal.formatmonth(theyear=year, themonth=month + month_iter, withyear=True) cal = cal.replace( '<table ', '<table data-month={0} data-year={1} cellpadding="10" cellspacing="10" class="m-2 text-center month" border="2" ' .format(month + month_iter, year)) if month_iter != 0 or year != now.year: cal = cal.replace( '<table ', '<table hidden data-month={0} data-year={1} cellpadding="10" cellspacing="10" class="m-2 text-center month" border="2" ' .format(month + month_iter, year)) return cal
def changelist_view(self, request, extra_context=None): after_day = request.GET.get('day__gte', None) extra_context = extra_context or {} if not after_day: d = datetime.date.today() else: try: split_after_day = after_day.split('-') d = datetime.date(year=int(split_after_day[0]), month=int(split_after_day[1]), day=1) except: d = datetime.date.today() previous_month = datetime.date(year=d.year, month=d.month, day=1) # find first day of current month previous_month = previous_month - datetime.timedelta(days=1) # backs up a single day previous_month = datetime.date(year=previous_month.year, month=previous_month.month, day=1) # find first day of previous month last_day = calendar.monthrange(d.year, d.month) next_month = datetime.date(year=d.year, month=d.month, day=last_day[1]) # find last day of current month next_month = next_month + datetime.timedelta(days=1) # forward a single day next_month = datetime.date(year=next_month.year, month=next_month.month, day=1) # find first day of next month extra_context['previous_month'] = reverse('admin:events_event_changelist') + '?day__gte=' + str( previous_month) extra_context['next_month'] = reverse('admin:events_event_changelist') + '?day__gte=' + str(next_month) cal = HTMLCalendar() html_calendar = cal.formatmonth(d.year, d.month, withyear=True) html_calendar = html_calendar.replace('<td ', '<td width="150" height="150"') extra_context['calendar'] = mark_safe(html_calendar) return super(EventAdmin, self).changelist_view(request, extra_context)
def time(request): monthCal = HTMLCalendar(calendar.MONDAY) now = datetime.date.today() date_0 = now.year date_1 = now.month date_2 = now.day my_cal = monthCal.formatmonth (date_0 ,date_1 ) my_cal = my_cal.replace ('">%r</td><td class="' %date_2, '"<p style="color:#FF0000">%r</p></td><td class="' %date_2) return render_to_response ('asd/time.html', {'current_date': now, 'cal': my_cal })
def render(self, context): try: my_year = self.year.resolve(context) my_month = self.month.resolve(context) cal = HTMLCalendar() return cal.formatmonth(int(my_year), int(my_month)) except ValueError: return except template.VariableDoesNotExist: return
def calbuilder(request): year = int(request.POST.get('year', None)) month = int(request.POST.get('month', None)) cal = HTMLCalendar(calendar.SUNDAY) cal = cal.formatmonth(year, month) cal = cal.replace('<td ', '<td width="150" height="150"') cal = cal.replace( 'border="0" cellpadding="0" cellspacing="0" class="month">', 'class="table">') events = Event.objects.filter(date__year=year, date__month=month) events_json = serializers.serialize('json', events) context = {'calendar': cal, 'events': events_json} return HttpResponse(json.dumps(context), content_type="application/json")
def leave_calendar(request, month=datetime.date.today().month, year=datetime.date.today().year): year = int(year) month = int(month) if year < 1900 or year > 2099: year = datetime.date.today().year month_name = calendar.month_name[month] cal = HTMLCalendar.formatmonth(year, month) context = {"title": year, "calendar": cal} return render(request, 'leave/leave_calendar.html', context)
from calendar import Calendar, TextCalendar, HTMLCalendar, calendar cal = Calendar() iter = cal.iterweekdays() for i in iter: print(i) #cal.TextCalendar() html_cal = HTMLCalendar() code = html_cal.formatmonth(2017, 12, False) code = html_cal.formatyear(2017, 6) # in HTML Format! code = html_cal.formatyearpage(2017, encoding='UTF-8') code = calendar(2017) # In text ! print(code)
def formatmonth(self, theyear=None, themonth=None, withyear=False): return HTMLCalendar.formatmonth(self, theyear or self.theyear, themonth or self.themonth, withyear)
def calendarpage(request): cal = HTMLCalendar() monthly = cal.formatmonth(2016, 5) return render(request, 'Lfit/calendarproto.html', {'monthly': monthly})
def dashboard(): html_cal = HTMLCalendar() html_code = html_cal.formatmonth(datetime.today().year, datetime.today().month, True) username = session['username'] user_email = User.query.filter_by(username=username).first().email daily_cats = Category.query.filter_by(category_daily=True).all() pie_data = [ pie_chart([cat for cat in CATS['Daily'] + CATS['Monthly']], convert_toPercent([ calculate_expenditure(category_object.id, userid=User.query.filter_by( username=username).first().id, today=False) for category_object in Category.query.all() ]), "My Expenditure Distribution this Month."), pie_chart([cat for cat in CATS['Daily']], convert_toPercent([ calculate_expenditure(category_object.id, userid=User.query.filter_by( username=username).first().id, today=True) for category_object in Category.query.all() ]), "My Expenditure Distribution today!") ] months = [ 'Jan', 'Feb', 'March', 'April', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec' ] l = [ calculate_expenditureBudget_month( userid=User.query.filter_by(username=username).first().id, month=month) for month in range(1, 13) ] exp, budg = zip(*l) gauge_data = gauge_chart( ['{}{}'.format(a, b) for a, b in zip(months, [' Expenses'] * 12)], exp, budg) _budg = budg[datetime.today().month - 1] _exp = exp[datetime.today().month - 1] if _budg > 1: if _exp > _budg: flash( "You have exceeded your budget limit this month by {} Rs.". format(_exp - _budg), "danger") elif _exp == _budg: flash( "Expenses equalled to budget this month, time to stop spending", "warning") else: flash( "Keep spending, you have {} Rs. to spend".format(_budg - _exp), "success") try: if request.method == 'POST': initialize_categories() username = session['username'] if request.form['submit'] == "Set Password": new_password = request.form['NewPassword'] new_password = sha256_crypt.encrypt(str(new_password)) User.query.filter_by( username=username).first().password = new_password db.session.commit() db.session.close() session.clear() gc.collect() flash("Password Changed!", "success") flash("Login Again!") return redirect(url_for('login_page')) if request.form['submit'] == 'Save Email': new_email = request.form['email'] User.query.filter_by( username=username).first().email = new_email db.session.commit() db.session.close() gc.collect() flash("Email changed", "success") return render_template('dashboard.html', CATS=CATS, html_code=html_code, active_tab='Home', isDaily=True, pie_data=pie_data, gauge_data=gauge_data, user_email=user_email) if request.form['submit'] == "Set Budget": _budget_userid = User.query.filter_by( username=username).first().id flag = 0 for obj in Budget.query.filter_by( budget_userid=_budget_userid).all(): if obj.budget_year == datetime.today( ).year and obj.budget_month == datetime.today().month: flash( "Budget successfully changed for this month! from {} to {}" .format( obj.budget_amount, request.form['amount'], ), "success") obj.budget_amount = request.form['amount'] db.session.commit() db.session.close() gc.collect() flag = 1 # now don't need to create object again. if flag == 0: _budget_amount = request.form['amount'] _budget_month = datetime.today().month _budget_year = datetime.today().year budget_object = Budget(budget_userid=_budget_userid, budget_year=_budget_year, budget_month=_budget_month, budget_amount=_budget_amount) db.session.add(budget_object) db.session.commit() session['current_budget_id'] = budget_object.id db.session.close() gc.collect() flash("Budget Set!", "success") l = [ calculate_expenditureBudget_month( userid=User.query.filter_by( username=username).first().id, month=month) for month in range(1, 13) ] exp, budg = zip(*l) gauge_data = gauge_chart([ '{}{}'.format(a, b) for a, b in zip(months, [' Expenses'] * 12) ], exp, budg) return render_template('dashboard.html', CATS=CATS, html_code=html_code, active_tab='Home', isDaily=True, pie_data=pie_data, gauge_data=gauge_data, user_email=user_email) for key in CATS.keys(): for cat in CATS[key]: if request.form['submit'] == "Set {} amount".format(cat): _expenditure_userid = User.query.filter_by( username=username).first().id _spent = request.form['amount'] _where_spent = request.form['location'] _category_id = Category.query.filter_by( category=cat).first().id _date_of_expenditure = datetime.today() _description = request.form['comment'] expenditure_object = Expenditure( expenditure_userid=_expenditure_userid, spent=_spent, where_spent=_where_spent, category_id=_category_id, date_of_expenditure=_date_of_expenditure, description=_description) db.session.add(expenditure_object) db.session.commit() db.session.close() gc.collect() flash("Expenditure recorded of {}!".format(cat), "success") pie_data = [ pie_chart( [ cat for cat in CATS['Daily'] + CATS['Monthly'] ], convert_toPercent([ calculate_expenditure( category_object.id, userid=User.query.filter_by( username=username).first().id, today=False) for category_object in Category.query.all() ]), "My Expenditure Distribution this Month."), pie_chart( [cat for cat in CATS['Daily']], convert_toPercent([ calculate_expenditure( category_object.id, userid=User.query.filter_by( username=username).first().id, today=True) for category_object in Category.query.all() ]), "My Expenditure Distribution today!") ] l = [ calculate_expenditureBudget_month( userid=User.query.filter_by( username=username).first().id, month=month) for month in range(1, 13) ] exp, budg = zip(*l) gauge_data = gauge_chart([ '{}{}'.format(a, b) for a, b in zip(months, [' Expenses'] * 12) ], exp, budg) if Category.query.filter_by( category=cat).first().category_daily == True: return render_template('dashboard.html', CATS=CATS, html_code=html_code, active_tab='expense', isDaily=True, pie_data=pie_data, gauge_data=gauge_data, user_email=user_email) else: return render_template('dashboard.html', CATS=CATS, html_code=html_code, active_tab='expense', isDaily=False, pie_data=pie_data, gauge_data=gauge_data, user_email=user_email) return render_template('dashboard.html', CATS=CATS, html_code=html_code, active_tab='Home') else: flash("Welcome!", "default") return render_template('dashboard.html', CATS=CATS, html_code=html_code, active_tab='Home', pie_data=pie_data, gauge_data=gauge_data, user_email=user_email) except Exception as e: return render_template('error.html', e=e)
print(calendar.monthrange(2021,10)) print(calendar.monthcalendar(2019, 10)) print(calendar.prmonth(2021, 10)) print(calendar.prcal(2021)) print(calendar.day_name[0]) print(calendar.day_abbr[0]) print(calendar.month_name[1]) print(calendar.month_abbr[1]) print('--------------------------------') c = Calendar() print(list(c.itermonthdates(2021, 7))) print(list(c.itermonthdays2(2020, 7))) print(list(c.itermonthdays3(2021, 7))) print(list(c.itermonthdays4(2021, 7))) print('--------------------------------') tx = TextCalendar() print(tx.formatmonth(2021, 9)) print(tx.prmonth(2021, 9)) print(tx.formatyear(2021)) print(tx.pryear(2021)) print('---------------------------------') hc = HTMLCalendar() print(hc.formatmonth(2021, 10)) print(hc.formatyear(2021)) print(hc.formatyearpage(2021))