Commit 0c1c2f20 authored by O'Reilly Media, Inc's avatar O'Reilly Media, Inc
Browse files

Initial commit

parents
total_score = 0
for username in ("jill", "sam", "cathy"):
user_doc = dbh.users.find_one({"username":username})
total_score += user_doc.get("score", 0)
# Email each supplier of this product.
# Default value is the empty list so no special casing
# is needed if the suppliers property is not present.
for supplier in product_doc.get("suppliers", []):
email_supplier(supplier)
# Using upsert=True
def edit_or_add_session(description, session_id):
dbh.sessions.update({"session_id":session_id},
{"$set":{"session_description":description}}, safe=True, upsert=True)
# User X adds $20 to his/her account, so we atomically increment
# account_balance and return the resulting document
ret = dbh.users.find_and_modify({"username":username},
{"$inc":{"account_balance":20}}, safe=True, new=True)
new_account_balance = ret["account_balance"]
# Fetch the score for the current week
import datetime
now = datetime.datetime.utcnow()
current_year = now.year
current_week = now.isocalendar()[1]
# Default missing keys to a score of zero
user_doc["scores_weekly"].get("%d-%d" %(current_year, current_week), 0)
# Update the score for the current week
import datetime
username = "foouser"
now = datetime.datetime.utcnow()
current_year = now.year
current_week = now.isocalendar()[1]
# Use atomic update modifier to increment by 24
dbh.users.update({"username":username},
{"$inc":{"scores_weekly.%s-%s" %(current_year, current_week):24}},
safe=True)
# Update the score for the current week
import datetime
username = "foouser"
now = datetime.datetime.utcnow()
current_year = now.year
current_month = new.month
current_week = now.isocalendar()[1]
current_day = now.timetuple().tm_yday
# Use atomic update modifier to increment by 24
dbh.users.update({"username":username},
{"$inc":{
"scores_weekly.%s-%s" %(current_year, current_week):24,
"scores_daily.%s-%s" %(current_year, current_day):24,
"scores_monthly.%s-%s" %(current_year, current_month):24,
"score_total":24,
}
},
safe=True)
from beaker.cache import CacheManager
from beaker.util import parse_cache_config_options
from pymongo import Connection
from pylons import config
class Globals(object):
"""Globals acts as a container for objects available throughout the
life of the application
"""
def __init__(self, config):
"""One instance of Globals is created during application
initialization and is available during requests via the
'app_globals' variable
"""
self.mongodb_conn = Connection(config['mongodb.url'])
self.mongodb = self.mongodb_conn[config['mongodb.db_name']]
self.cache = CacheManager(**parse_cache_config_options(config))
import logging
from pylons import app_globals as g, request, response, session, tmpl_context as c, url
from pylons.controllers.util import abort, redirect
from pylonsfoo.lib.base import BaseController, render
log = logging.getLogger(__name__)
class MongodbController(BaseController):
def index(self):
new_doc = g.mongodb.counters.find_and_modify({"counter_name":"test_counter"},
{"$inc":{"counter_value":1}}, new=True, upsert=True , safe=True)
return "MongoDB Counter Value: %s" % new_doc["counter_value"]
import datetime
def my_view(request):
new_page_hit = {"timestamp":datetime.datetime.utcnow(), "url":request.url}
request.db.page_hits.insert(new_page_hit, safe=True)
return {"project":"mongofoo"}
import datetime
import string
import random
import mimetypes
import cStringIO as StringIO
from PIL import Image
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from pymongo.connection import Connection
from pymongo import DESCENDING
import gridfs
db = Connection().sms
## Example files for the title:
# MongoDB and Python, by Niall O'Higgins
[![MongoDB and Python, by Niall O'Higgins](http://akamaicovers.oreilly.com/images/9781449310363/cat.gif)](https://www.safaribooksonline.com/library/view/title/9781449312817//)
The following applies to example files from material published by O’Reilly Media, Inc. Content from other publishers may include different rules of usage. Please refer to any additional usage rights explained in the actual example files or refer to the publisher’s website.
O'Reilly books are here to help you get your job done. In general, you may use the code in O'Reilly books in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from our books does not require permission. Answering a question by citing our books and quoting example code does not require permission. On the other hand, selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Incorporating a significant amount of example code from our books into your product's documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN.
If you think your use of code examples falls outside fair use or the permission given here, feel free to contact us at <permissions@oreilly.com>.
Please note that the examples are not production code and have not been carefully testing. They are provided "as-is" and come with no warranty of any kind.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment