Example #1
0
 def export_data(self, **kwargs):
     data = []
     for delivery in Delivery.objects.filter(**kwargs).all():
         packs = delivery.packs.all()
         data.append({
             'enter':
             JalaliDatetime(delivery.entered_at).strftime("%Y/%m/%d %H:%M")
             if delivery.entered_at else '-',
             'exit':
             JalaliDatetime(delivery.exited_at).strftime("%Y/%m/%d %H:%M")
             if delivery.exited_at else '-',
             'taker':
             delivery.taker.get_full_name() if delivery.taker else '-',
             'giver':
             delivery.giver.get_full_name() if delivery.giver else '-',
             'first_name':
             delivery.pilgrim.first_name,
             'last_name':
             delivery.pilgrim.last_name,
             'phone':
             delivery.pilgrim.phone,
             'country':
             delivery.pilgrim.country,
             'bag':
             sum([item.bag_count for item in packs]),
             'suitcase_count':
             sum([item.suitcase_count for item in packs]),
             'pram_count':
             sum([item.pram_count for item in packs])
         })
     if data:
         header = list(data[0].keys())
         return ExcelUtil().export_file(header, data)
Example #2
0
def date_get_next(start_date, month):
    t = JalaliDatetime(start_date)
    start_first_day = t.replace(month=month, day=1)
    start_end_date = t.replace(month=month, day=t.daysinmonth)
    end_start_day = t.replace(day=1)
    z = start_first_day.todatetime()
    return (make_aware(z), make_aware(start_end_date.todatetime())), \
           (make_aware(end_start_day.todatetime()),)
Example #3
0
def convert_date(in_date, show_time=True):
    try:
        if in_date is None:
            return '-'
        if isinstance(in_date, str):
            in_date = datetime.datetime.strptime(in_date, '%Y-%m-%d %H:%M')
        if is_aware(in_date):
            if in_date.tzname() == 'UTC':
                in_date = make_naive(in_date)
        if not is_aware(in_date):
            in_date = make_aware(in_date)
        if not hasattr(in_date, 'minute'):
            d = JalaliDate(in_date)
        else:
            d = JalaliDatetime(in_date)
        if show_time and hasattr(in_date, 'minute'):
            if JalaliDate.today().todate() == d.date().todate(
            ):  # khayyam BUG fix
                d = d.strftime('%H:%M')
            else:
                d = d.strftime("%y-%m-%d %H:%M")
        else:
            d = d.strftime("%y-%m-%d")
        return d
    except Exception as e:
        print '[EXPECTED]template date parser error : %s : %s' % (e.message,
                                                                  in_date)
        return '-'
Example #4
0
def convert_date_no_day(in_date, show_time=False):
    try:
        if in_date is None:
            return '-'
        if isinstance(in_date, str):
            in_date = datetime.datetime.strptime(in_date, '%Y-%m-%d %H:%M')
        d = JalaliDatetime(in_date)
        if show_time:
            d = d + timedelta(hours=3, minutes=30)
            d = d.strftime("%Y/%m/%d %H:%M")
        else:
            d = d.strftime("%Y/%m/%d")
        return d
    except Exception as e:
        print e.message
        return '-'
Example #5
0
def remove_calendar_event(dash):
    if read_config('dashboard_remove_early', '0') == '1':
        if dash.fk_calendar_dashboard.exists():
            cal = dash.fk_calendar_dashboard.get()
            today_sh = JalaliDatetime.today()
            if cal.cal_day > today_sh.day and cal.cal_month >= today_sh.month and cal.cal_year >= today_sh.year:
                cal.delete()
            elif cal.cal_day == today_sh.day and cal.cal_month == today_sh.month and cal.cal_year == today_sh.year:
                if cal.work_time.start_time >= today_sh.time():
                    cal.delete()
Example #6
0
def generate_username():
    try:
        pat = read_config('login_pattern', '%Y')
        jd = JalaliDatetime.today()
        pat = jd.strftime(pat)
        rand = random.randint(100, 999)
        pat = pat.replace("RAND", str(rand))
        return pat
    except Exception as e:
        print e.message
        return ''
Example #7
0
def get_next_4_days(event_id, only_times_pk=False):
    init_days = int(read_config('calendar_show_days', 5))
    today = JalaliDatetime.today()
    res = []
    days_collected = False
    # for i in range(0, init_days + 1):
    i = 0
    while not days_collected:
        now = today + timedelta(days=i)
        while get_holidays(now.month, now.day):
            i += 1
            now = today + timedelta(days=i)
        qs = Calendar.objects.filter(cal_month=now.month,
                                     cal_year=now.year,
                                     cal_day=now.day,
                                     work_time__event_type=event_id)
        nw = get_match_day(now.weekday())
        if qs.exists():
            rsc = WorkingTime.objects.filter(
                week_day=nw, is_deleted=False, event_type=event_id).filter(
                    fk_calendar_working_time__cal_day=now.day,
                    fk_calendar_working_time__cal_month=now.month,
                    fk_calendar_working_time__cal_year=now.year).annotate(
                        rx=Count('fk_calendar_working_time__work_time')
                    ).filter(resource__lte=F('rx')).values_list('pk',
                                                                flat=True)
            times = WorkingTime.objects.filter(
                week_day=nw, event_type=event_id).exclude(pk__in=rsc)
            if now.day == today.day:
                times = times.filter(start_time__gte=datetime.today().time())
            # for r in rsc:
            #     print r.name,";", r.rx
            # times = rsc
        else:
            if now.day == today.day:
                times = WorkingTime.objects.filter(
                    week_day=nw,
                    is_deleted=False,
                    event_type=event_id,
                    start_time__gte=datetime.today().time())
            else:
                times = WorkingTime.objects.filter(week_day=nw,
                                                   is_deleted=False,
                                                   event_type=event_id)
        if only_times_pk:
            res.append(times.values_list('pk', flat=True))
        else:
            times = times.values('pk', 'name', 'start_time', 'end_time',
                                 'resource')
            res.append(FreeTimes(list(times), now.strftime('%Y-%m-%d %A')))
        i += 1
        if len(res) >= init_days:
            days_collected = True
    return res
Example #8
0
 def _isdst(self, dt):
     dt = dt.replace(tzinfo=None)
     if isinstance(dt, JalaliDatetime):
         jdt = dt
     else:
         jdt = JalaliDatetime.from_datetime(dt)
     start_jdt = jdt.replace(month=1, day=1)
     end_jdt = jdt.replace(month=7, day=1)
     if start_jdt < jdt < end_jdt:
         return True
     else:
         return False
Example #9
0
def get_month_expired_users_count():
    try:
        now = JalaliDatetime.today()
        last_month = JalaliDatetime(now.year, now.month - 1, day=now.day)
        next_month = JalaliDatetime(now.year, now.month, day=now.day)
        last_month = last_month.to_datetime()
        next_month = next_month.to_datetime()
        return UserCurrentService.objects.filter(
            expire_date__lt=next_month, expire_date__gt=last_month).count()
    except Exception as e:
        print e.message
        return 0
Example #10
0
def parse_date_from_str(str_date):
    """
    parsing dates from jalali str to julian date
    @param str_date: date str
    @type str_date:str
    @return:JalaliDateTime
    @rtype:JalaliDateTime
    """
    if not validate_empty_str(str_date):
        return None
    normalized = str_date.replace('-', '/').split(' ')[0]
    try:
        j = JalaliDatetime.strptime(normalized, '%Y/%m/%d')
        res = j.todatetime()
        return make_aware(res)
    except Exception as e:
        print e.message
        return None
Example #11
0
 def get_exited_at(self, obj):
     if obj.exited_at:
         return JalaliDatetime(timezone.localtime(
             obj.exited_at)).strftime("%A %d %B %H:%M")
Example #12
0
 def generate_code():
     jd = JalaliDatetime.today()
     ix = jd.strftime(read_config('indicator_pattern', '%y-%m'))
     return ix
Example #13
0
    def print(self, pack):
        badge_count = pack.bag_count + pack.pram_count + pack.suitcase_count
        pilgrim = pack.delivery.pilgrim
        ch = ConstantHelper(country=pilgrim.country)
        entered_at_jalali = JalaliDatetime(
            timezone.localtime(
                pack.delivery.entered_at)).strftime("%A %d %B %H:%M")
        barcode = self.barcode(pack)
        depository = pack.cell.row.cabinet.depository
        ph = PrintHelper(depository.printer_id)
        pathes = []
        for idx in range(badge_count):
            html = render_to_string(
                'badge.html',
                {
                    'name': pilgrim.get_full_name(),
                    'index': idx + 1,
                    'count': badge_count,
                    'country': pilgrim.country,
                    'phone': pilgrim.get_four_digit_phone(),
                    'entered_at': entered_at_jalali,
                    'code': pack.cell.get_printable_code(),
                    'barcode': barcode,
                    'depository_name': ch.get_depository_name(depository),
                    'taker': pack.delivery.taker.get_full_name(),
                    "BASE_DIR": settings.BASE_DIR
                },
            )
            pathes.append(ph.generate_pdf(html))
        ####################################### Print Receipt #########################################
        data = {
            'name': pilgrim.get_full_name(),
            'depository_name': depository.name,
            'depository_address': ch.get_depository_address(depository),
            'social': ch.get(settings.CONST_KEY_SOCIAL),
            'phone': ch.get(settings.CONST_KEY_PHONE),
            'notice': ch.get_notice(),
            'barcode': barcode,
            "BASE_DIR": settings.BASE_DIR
        }
        if pilgrim.is_iranian():
            data.update({
                'entered_at':
                JalaliDatetime(timezone.localtime(
                    pack.delivery.entered_at)).strftime("%A %d %B %H:%M"),
                'font':
                'IranSans'
            })

        else:
            data.update({
                'entered_at':
                timezone.localtime(
                    pack.delivery.entered_at).strftime("%A %d %B %H:%M:%S"),
                'font':
                "Calibri Light"
            })
        html = render_to_string('reciept.html', data)
        pathes.append(ph.generate_pdf(html, height=120))

        ph.print(pathes)
        os.remove(barcode)