def new_item(category):
if request.method == 'GET':
return render('newitem.html', category=category)
elif request.method == 'POST':
name = request.form['name']
highlight = request.form['highlight']
url = request.form['url']
if valid_item(name, url, highlight):
user_id = login_session['user_id']
item = Item(name=name, highlight=highlight, url=url, user_id=user_id, category_id=category.id)
session.add(item)
session.commit()
flash("Newed item %s!" % item.name)
return redirect(url_for('show_item', category_id=category.id, item_id=item.id))
else:
error = "Complete info please!"
return render('newitem.html', category=category, name=name, highlight=highlight, url=url,
error=error)
python类Item()的实例源码
def fetch(self):
logging.debug("Fetching google calendar data")
self.build_service('calendar', 'v3')
timeMin = self.date_dt.isoformat() + 'Z'
timeMax = self.next_date_dt.isoformat() + 'Z'
results = self.service.events().list(calendarId='primary',
maxResults=self.limit,
timeMin=timeMin,
timeMax=timeMax).execute()
if results:
items = [
Item(
svc=SERVICE.GCAL,
title=r.get('summary'),
details=r.get('description'),
id=r.get('id'),
type=SERVICE.EVENT).json() for r in results.get(
'items',
[])]
return items
return []
def _handle_gmail_message(self, request_id, response, exception):
if exception is not None:
logging.error(str(exception))
else:
if response:
headers = response.get('payload').get('headers')
subject = _from = _to = _date = None
for h in headers:
if h.get('name') == 'Subject':
subject = h.get('value')
if h.get('name') == 'From':
_from = h.get('value')
if h.get('name') == 'To':
_to = h.get('value')
if h.get('name') == 'Date':
_date = h.get('value')
if subject and _from:
self.items.append(
Item(
svc=SERVICE.GMAIL,
title=subject,
subhead=_from,
id=response.get('id'),
type=SERVICE.EMAIL).json())
def item_detail(request, item_code):
from forms import ItemForm
item = get_object_or_404(Item, code=item_code)
if request.method=="POST":
item_form = ItemForm(instance=item, data=request.POST)
if item_form.is_valid():
item = item_form.save()
return redirect(item)
else:
item_form = ItemForm(instance=item)
payload={
'item':item,
'item_form':item_form,
'l_m_storage_active':"active",
}
return _render('warehouse/item/detail.html', payload, request)
def item_json(category, item):
return jsonify(Item=item.serialize)
def fetch(self):
self.build_service('tasks', 'v1')
timeMin = self.date_dt.isoformat() + 'Z'
timeMax = self.next_date_dt.isoformat() + 'Z'
gt_settings = self.user.get_svc_settings('g_tasks')
tasklist = gt_settings.get('taskList', {}).get("value")
if tasklist:
results = self.service.tasks().list(
tasklist=tasklist,
maxResults=self.limit,
completedMin=timeMin,
completedMax=timeMax).execute()
if results:
logging.debug(results)
items = [
Item(
svc=SERVICE.GTASKS,
title=r.get('title'),
id=r.get('id'),
type=SERVICE.TASK).json() for r in results.get(
'items',
[])]
return items
else:
raise ServiceError("No tasklist configured")
return []
def fetch(self):
BATCH_MESSAGES = True
before_gdate = datetime.strftime(self.next_date_dt, "%Y/%m/%d")
after_gdate = datetime.strftime(self.date_dt, "%Y/%m/%d")
self.build_service('gmail', 'v1')
query = 'before:%s after:%s' % (before_gdate, after_gdate)
logging.debug(query)
if BATCH_MESSAGES:
# Fetch message IDs
results = self.service.users().messages().list(
userId='me', maxResults=self.limit, q=query).execute()
if results:
ids = [r.get('id') for r in results.get('messages', [])]
if ids:
batch = self.service.new_batch_http_request(
callback=self._handle_gmail_message)
for id in ids:
batch.add(
self.service.users().messages().get(
id=id, userId="me"), request_id=id)
# Blocks, populates self.items
batch.execute(http=self.http_auth)
else:
# Only threads show snippets in Gmail API?
results = self.service.users().threads().list(
userId='me', maxResults=self.limit, fields='threads', q=query).execute()
if results:
self.items = [
Item(
svc=SERVICE.GMAIL,
title=r.get('snippet'),
id=r.get('id'),
type=SERVICE.EMAIL).json() for r in results.get(
'threads',
[])]
return self.items
def fetch(self):
self.build_service('drive', 'v3')
timeMin = self.date_dt.isoformat()
timeMax = self.next_date_dt.isoformat()
query = "(viewedByMeTime > '%s' and viewedByMeTime < '%s') OR (createdTime > '%s' and createdTime < '%s' and '%s' in owners)" % (
timeMin, timeMax, timeMin, timeMax, self.user.email)
items = []
results = self.service.files().list(
orderBy='modifiedTime',
pageSize=self.limit,
spaces='drive,photos',
fields='files(createdTime,description,id,kind,viewedByMeTime,modifiedTime,name,spaces,webViewLink,thumbnailLink),kind',
q=query).execute()
for f in results.get('files', []):
spaces = f.get('spaces')
is_image = 'photos' in spaces
type = SERVICE.DOCUMENT if not is_image else SERVICE.PHOTO
webViewLink = f.get('webViewLink')
thumbnailLink = f.get('thumbnailLink')
item = Item(svc=SERVICE.GDRIVE,
title=f.get('name'),
id=f.get('id'),
image=thumbnailLink,
details=f.get('description'),
type=type)
if is_image:
logging.debug(f)
items.append(item.json())
return items
def item_list(request):
item_list = Item.objects.all()
return _render('warehouse/item/list.html', {
'item_list':item_list,
'l_m_storage_active':"active",
}, request)