本文整理汇总了Python中moocng.mongodb.get_db函数的典型用法代码示例。如果您正苦于以下问题:Python get_db函数的具体用法?Python get_db怎么用?Python get_db使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_db函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: profile_badge
def profile_badge(request, badgeid, id=None):
if (badgeid):
if(not id):
if(not request.user.id):
return HttpResponseRedirect("/auth/login")
user = request.user
id = request.user.id
else:
user = User.objects.get(username=id)
id = user.id
badge = get_db().get_collection('badge').find({"id_user": id, "id_badge": int(badgeid)})[0]
hashed_email = hashlib.sha256(user.email + settings.BADGES_HASH_SALT).hexdigest()
badge['id'] = badge['_id']
return render_to_response('profile/badge.html', {
"id":id,
'request': request,
"user_view_profile": user,
"badges_count": get_db().get_collection('badge').find({"id_user": id}).count(),
"hashed_email": hashed_email,
"badge": badge,
"openbadges_service_url": settings.BADGES_SERVICE_URL,
}, context_instance=RequestContext(request))
else:
return HttpResponseBadRequest()
示例2: change_user_group
def change_user_group(id_user, id_group, new_id_group, pos_lat=0.0, pos_lon=0.0):
groupCollection = mongodb.get_db().get_collection('groups')
group = groupCollection.find_one({'_id': ObjectId(id_group)})
for m in group["members"]:
if m["id_user"] == id_user:
member = m
group["members"].remove(m)
if "size" in group:
group["size"] -= 1
else:
group["size"] = len(group["members"])
groupCollection.update({'_id': ObjectId(id_group)}, {"$set": {"members": group["members"], "size": group["size"]}})
group = groupCollection.find_one({'_id': ObjectId(new_id_group)})
group["members"].append(member)
if "size" in group:
group["size"] += 1
else:
group["size"] = len(group["members"])
groupCollection.update({'_id': ObjectId(new_id_group)}, {"$set": {"members": group["members"], "size": group["size"]}})
groupsActivityCollection = mongodb.get_db().get_collection('groups_activity')
timestamp = int(round(time.time() * 1000))
activity_entry = {"id_course": group["id_course"], "id_user": id_user, "former_id_group": ObjectId(id_group), "new_id_group": ObjectId(new_id_group), "timestamp": timestamp, "lat": pos_lat, "lon": pos_lon}
groupsActivityCollection.insert(activity_entry)
示例3: obj_delete
def obj_delete(self, request, **kwargs):
obj = self.obj_get(request, **kwargs)
#submissions and reviews must be deleted too
submissions = get_db().get_collection('peer_review_submissions')
submissions.remove({'kq': obj.kq_id})
reviews = get_db().get_collection('peer_review_reviews')
reviews.remove({'kq': obj.kq_id})
super(PrivatePeerReviewAssignmentResource, self).obj_delete(request, **kwargs)
示例4: update_following_blog_user
def update_following_blog_user(self, id, following, extra=None):
get_db().get_collection(self.col_user).update({"id_user": id}, {"$set": {"following": following}})
# xAPI
if extra:
user = User.objects.get(pk=id)
course = None
resource = {
'type': 'microblogfollow',
'user_id': extra['user_id']
}
geolocation = extra.get('geolocation')
x_api.learnerInteracts(user, resource, course, geolocation)
示例5: handle
def handle(self, *args, **options):
users = User.objects.all()
if options["user"]:
users = users.filter(pk=options["user"])
if not users:
raise CommandError(u"User %s does not exist" % options["user"])
self.message("Migrating the user: %s" % users[0].username)
elif settings.NUM_MIGRATE_MARK_DAILY is not None:
email_list = options["email_list"]
if not email_list:
raise CommandError(u"Please you have to pass the email list")
first_day = datetime.strptime(settings.FIRST_DAY_MIGRATE_MARK, '%Y-%m-%d')
today = datetime.today()
num_days = (today - first_day).days
start_pk = num_days * settings.NUM_MIGRATE_MARK_DAILY + 1
end_pk = (num_days + 1) * settings.NUM_MIGRATE_MARK_DAILY
self.message("Migrating the users from pk=%s to pk=%s " % (start_pk, end_pk))
users = users.filter(pk__gte=start_pk, pk__lte=end_pk)
max_pk = User.objects.aggregate(Max('pk'))['pk__max']
if not users and end_pk >= max_pk:
send_mail('The mark migration is finished',
'The mark migration is finished',
settings.DEFAULT_FROM_EMAIL,
email_list.split(','))
db = get_db()
for user in users:
for course in user.courses_as_student.all():
for unit in course.unit_set.scorables():
for kq in unit.knowledgequantum_set.all():
updated_kq, passed_kq_now = update_kq_mark(db, kq, user, course.threshold)
self.update_passed(db, 'stats_kq', passed_kq_now, {'kq_id': kq.pk})
updated_unit, passed_unit_now = update_unit_mark(db, unit, user, course.threshold)
self.update_passed(db, 'stats_unit', passed_unit_now, {'unit_id': unit.pk})
updated_course, passed_course_now = update_course_mark(db, course, user)
self.update_passed(db, 'stats_course', passed_course_now, {'course_id': course.pk})
示例6: forwards
def forwards(self, orm):
from moocng.mongodb import get_db
db = get_db()
activity = db.get_collection("groups")
activity.create_index([("id_course", pymongo.ASCENDING)])
示例7: update_mark
def update_mark(submitted):
from moocng.courses.marks import calculate_kq_mark, calculate_unit_mark, calculate_course_mark
updated_kq_mark = updated_unit_mark = updated_course_mark = False
passed_kq = passed_unit = passed_course = False
kq = KnowledgeQuantum.objects.get(pk=submitted['kq_id'])
unit = kq.unit
course = kq.unit.course
user = User.objects.get(pk=submitted['user_id'])
mark_kq, mark_normalized_kq = calculate_kq_mark(kq, user)
db = get_db()
# KQ
updated_kq_mark, passed_kq = update_kq_mark(db, kq, user, course.threshold,
new_mark_kq=mark_kq,
new_mark_normalized_kq=mark_normalized_kq)
# UNIT
if not updated_kq_mark:
return (updated_kq_mark, updated_unit_mark, updated_course_mark,
passed_kq, passed_unit, passed_course)
mark_unit, mark_normalized_unit = calculate_unit_mark(kq.unit, user)
updated_unit_mark, passed_unit = update_unit_mark(db, unit, user, course.threshold,
new_mark_unit=mark_unit,
new_mark_normalized_unit=mark_normalized_unit)
# COURSE
if not updated_unit_mark:
return (updated_kq_mark, updated_unit_mark, updated_course_mark,
passed_kq, passed_unit, passed_course)
mark_course, units_info = calculate_course_mark(unit.course, user)
updated_course_mark, passed_course = update_course_mark(db, course, user, mark_course)
return (updated_kq_mark, updated_unit_mark, updated_course_mark,
passed_kq, passed_unit, passed_course)
示例8: save_retweet
def save_retweet(self, post_id, user_id, username, extra=None):
postCollection = get_db().get_collection(self.col_post)
post = postCollection.find_one({"$and": [{"id_user":user_id},{"id_original_post":ObjectId(post_id)}]})
if(not post):
postCollection.update({"$or": [{"_id": ObjectId(post_id)}, {"id_original_post": ObjectId(post_id)}]}, {"$inc": {"shared": 1}}, multi=True)
post = postCollection.find_one({"_id": ObjectId(post_id)})
post["id_author"] = post["id_user"]
post["id_user"] = user_id
post["id_original_post"] = post["_id"]
post["original_date"] = post["date"]
post["date"] = datetime.utcnow().isoformat()
post["shared_by"] = "@%s" % (username)
del post["_id"]
rtpost_id = super(Microblog,self).insert_post(post)
# xAPI
if extra:
user = User.objects.get(pk=user_id)
course = None
resource = {
'type': 'microblogshare',
'url': 'https://%s%s#%s' % (settings.API_URI, reverse('profile_posts_byid', kwargs={'id': user_id}), rtpost_id),
'name': 'Microblog post',
'description': 'This is a blog post'
}
geolocation = extra.get('geolocation')
x_api.learnerSubmitsAResource(user, resource, course, geolocation)
return True
else:
return False
示例9: insert_p2p_if_does_not_exists_or_raise
def insert_p2p_if_does_not_exists_or_raise(p2p_submission, submissions=None):
if submissions is None:
db = get_db()
submissions = db.get_collection("peer_review_submissions")
if submissions.find({'kq': p2p_submission['kq'], 'author': p2p_submission['author']}).count() > 0:
raise BadRequest(_('You have already sent a submission. Please reload the page'))
return submissions.insert(p2p_submission)
示例10: handle
def handle(self, *args, **options):
activity = get_db().get_collection('activity')
to_fix = activity.find({'$where': 'function() { return this.unit_id == null; }'})
fixed_counter = 0
removed_counter = 0
total = to_fix.count()
if total > 0:
for act in to_fix:
try:
kq = KnowledgeQuantum.objects.get(id=int(act['kq_id']))
activity.update(
{'kq_id': act['kq_id']},
{'$set': {'unit_id': kq.unit.id}},
)
fixed_counter += 1
except KnowledgeQuantum.DoesNotExist:
activity.remove({'kq_id': act['kq_id']})
removed_counter += 1
if ((total / 100) % (fixed_counter + removed_counter)) == 0:
percent = int(total / (fixed_counter + removed_counter))
print 'Progress: %d%% of %d' % (percent, total)
print 'Fixed %d activities, removed %d' % (fixed_counter, removed_counter)
示例11: profile_user
def profile_user(request, id, byid=False):
if(not id):
case = None
if(not request.user.id):
return HttpResponseRedirect('/auth/login')
id = request.user.id
user = request.user
else:
case = _getCase(request,id)
if byid:
user = User.objects.get(pk=id)
else:
user = User.objects.get(username=id)
id = user.id
courses = get_courses_user_is_enrolled(user)
return render_to_response('profile/user.html', {
'id': id,
'case': case,
'badges_count': get_db().get_collection('badge').find({'id_user': id}).count(),
'request': request,
'courses': courses,
'is_user': True,
'user_view_profile': user,
}, context_instance=RequestContext(request))
示例12: teacheradmin_stats
def teacheradmin_stats(request, course_slug):
course = get_object_or_404(Course, slug=course_slug)
is_enrolled = course.students.filter(id=request.user.id).exists()
stats_course = get_db().get_collection('stats_course')
stats = stats_course.find_one({'course_id': course.id})
if stats is not None:
data = {
'enrolled': course.students.count(),
'started': stats.get('started', -1),
'completed': stats.get('completed', -1),
}
if course.threshold is not None:
#if the course doesn't support certification, then don't return the
#'passed' stat since it doesn't apply
data['passed'] = stats.get('passed', -1)
return render_to_response('teacheradmin/stats.html', {
'course': course,
'is_enrolled': is_enrolled,
'initial_data': simplejson.dumps(data),
}, context_instance=RequestContext(request))
else:
messages.error(request, _(u"There are no statistics for this course."))
return HttpResponseRedirect(reverse('teacheradmin_info',
args=[course_slug]))
示例13: is_valid
def is_valid(self, bundle, request):
if not bundle.data or not ("kq" in bundle.data):
return {'__all__': 'Expected kq id'}
errors = {}
db = get_db()
collection = db.get_collection("peer_review_submissions")
exists = collection.find({
"kq": bundle.data["kq"],
"author": unicode(request.user.id)
})
if exists.count() > 0:
msg = "Already exists a submission for kq=%s and user=%s" % (
bundle.data["kq"],
request.user.id)
logger.error(msg)
errors["kq"] = [msg]
errors["author"] = [msg]
text = bundle.data.get("text", "")
max_text_leng = getattr(settings, "PEER_REVIEW_TEXT_MAX_SIZE", 5000)
if len(text) > max_text_leng:
errors["text"] = "Text is longer than %s chars" % max_text_leng
return errors
示例14: search_posts
def search_posts(self, query, page):
postCollection = get_db().get_collection(self.col_post)
mongoQuery = {'$regex': '.*%s.*' % (query)}
posts = postCollection.find({'text': mongoQuery})[page:page+10].sort("date",pymongo.DESCENDING)
return self._process_post_list(posts)
示例15: unenroll_student
def unenroll_student(course, student):
from moocng import mongodb
from bson.objectid import ObjectId
from moocng.x_api import utils as x_api
if course.has_groups:
groupCollection = mongodb.get_db().get_collection('groups')
group = groupCollection.find_one( { 'id_course': course.id, 'members.id_user':student.id } )
if(group):
for m in group["members"]:
if(m["id_user"] == student.id):
group["members"].remove(m)
if "size" in group:
group["size"] -= 1
else:
group["size"] = len(group["members"])
groupCollection.update({'_id': ObjectId(group["_id"])}, {"$set": {"members": group["members"], "size": group["size"]}})
course.students.through.objects.get(student=student,
course=course).delete()
geolocation = {
'lat': 0.0,
'lon': 0.0
}
x_api.learnerUnenrollsInMooc(student, course, geolocation)