Esempio n. 1
0
 def download_photos(cls, photo_url=None):
     from settings.settings import PROJECT_HOME
     import requests
     from libs.datetimes import datetime_now, datetime_to_str
     r = requests.get(photo_url, stream=True)
     image_name = datetime_to_str(datetime_now())
     image_url = "{project_home}/data/image/{image}.jpg".format(project_home=PROJECT_HOME,
                                                          image=image_name)
     print(image_url)
     with open(image_url, 'wb') as f:
         for chunk in r.iter_content(chunk_size=32):
             f.write(chunk)
     return image_url
Esempio n. 2
0
def to_excel(modeladmin, request, queryset):
    response = HttpResponse(mimetype='application/ms-excel')
    fields = modeladmin.list_display
    model = modeladmin.model._meta
    response[
        'Content-Disposition'] = 'attachment;filename=%s.xls' % datetime_to_str(
            datetime_now())
    row_data = []
    header_name = []
    model_data = []

    for header in fields:
        if hasattr(modeladmin, header) and hasattr(getattr(modeladmin, header),
                                                   'short_description'):
            name = getattr(getattr(modeladmin, header), 'short_description')
            header_name.append(name)
        else:
            field = model.get_field_by_name(header)
            if field and field[0].verbose_name:
                name = field[0].verbose_name
            else:
                name = header
            model_data.append(name)
            header_name.append(name)

    row_data.append(header_name)

    for row in queryset:
        line = []
        for content in fields:
            if hasattr(row, 'get_%s_display' % content):
                data = eval('row.get_' + content + '_display()')
            elif hasattr(row, content):
                data = getattr(row, content)
            elif hasattr(modeladmin, content):
                data = getattr(modeladmin, content)(row)

            if callable(data):
                content_data = data()
                line.append(unicode(content_data))
            else:
                line.append(unicode(data))
        row_data.append(line)
    ExcelHelper.write(response, row_data)
    return response
Esempio n. 3
0
def to_excel(modeladmin, request, queryset):
    response = HttpResponse(mimetype='application/ms-excel')
    fields = modeladmin.list_display
    model = modeladmin.model._meta
    response['Content-Disposition'] = 'attachment;filename=%s.xls' % datetime_to_str(datetime_now())
    row_data = []
    header_name = []
    model_data = []

    for header in fields:
        if hasattr(modeladmin, header) and hasattr(getattr(modeladmin, header), 'short_description'):
            name = getattr(getattr(modeladmin, header), 'short_description')
            header_name.append(name)
        else:
            field = model.get_field_by_name(header)
            if field and field[0].verbose_name:
                name = field[0].verbose_name
            else:
                name = header
            model_data.append(name)
            header_name.append(name)

    row_data.append(header_name)

    for row in queryset:
        line = []
        for content in fields:
            if hasattr(row, 'get_%s_display' % content):
                data = eval('row.get_' + content + '_display()')
            elif hasattr(row, content):
                data = getattr(row, content)
            elif hasattr(modeladmin, content):
                data = getattr(modeladmin, content)(row)

            if callable(data):
                content_data = data()
                line.append(unicode(content_data))
            else:
                line.append(unicode(data))
        row_data.append(line)
    ExcelHelper.write(response, row_data)
    return response
Esempio n. 4
0
 def get_date(self):
     return datetime_to_str(self.created_at, "%Y-%m-%d")
Esempio n. 5
0
 def get_created_time(self, data):
     return datetime_to_str(data.operationtime, format=TIME_FORMAT)
Esempio n. 6
0
 def get_created_at(self):
     return datetime_to_str(self.created_at, "%Y-%m-%d %H:%M")
Esempio n. 7
0
 def get_time(self):
     return datetime_to_str(self.created_at, "%H:%M")