• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python mongodb.get_db函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中moocng.mongodb.get_db函数的典型用法代码示例。如果您正苦于以下问题:Python get_db函数的具体用法?Python get_db怎么用?Python get_db使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了get_db函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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()
开发者ID:GeographicaGS,项目名称:moocng,代码行数:27,代码来源:views.py


示例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)
开发者ID:GeographicaGS,项目名称:moocng,代码行数:26,代码来源:utils.py


示例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)
开发者ID:HengeSense,项目名称:moocng,代码行数:8,代码来源:resources.py


示例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)
开发者ID:GeographicaGS,项目名称:moocng,代码行数:13,代码来源:models.py


示例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})
开发者ID:Macro-Bin,项目名称:moocng,代码行数:35,代码来源:migrate_marks.py


示例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)])
开发者ID:GeographicaGS,项目名称:moocng,代码行数:7,代码来源:0049_mongoGroups.py


示例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)
开发者ID:fid-jose,项目名称:moocng,代码行数:35,代码来源:tasks.py


示例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
开发者ID:GeographicaGS,项目名称:moocng,代码行数:31,代码来源:models.py


示例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)
开发者ID:fid-jose,项目名称:moocng,代码行数:7,代码来源:utils.py


示例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)
开发者ID:jespino,项目名称:moocng,代码行数:25,代码来源:fix_activities_ids.py


示例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))
开发者ID:GeographicaGS,项目名称:moocng,代码行数:27,代码来源:views.py


示例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]))
开发者ID:Yangff,项目名称:moocng,代码行数:28,代码来源:views.py


示例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
开发者ID:MCHacker,项目名称:moocng,代码行数:29,代码来源:validation.py


示例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)
开发者ID:GeographicaGS,项目名称:moocng,代码行数:7,代码来源:models.py


示例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)
开发者ID:GeographicaGS,项目名称:moocng,代码行数:28,代码来源:methods.py


示例16: _process_post_children

	def _process_post_children(self, post, id_user=None):
		postCollection = get_db().get_collection(self.col_post)
		from_zone = tz.tzutc()
		to_zone = tz.tzlocal()
		post['replies'] = []
		for child in post['children']:
			try:
				post_child = postCollection.find({
					'$and': [
						{'_id': child},
						{'$or': [ {'deleted': {'$exists': False} }, {'deleted': False} ] }
					]
				}).limit(1)[0]
				if post_child and len(post_child['children']) > 0:
					self._process_post_children(post_child, id_user)

				post_child["date"] = datetime.strptime(post_child.get("date"), "%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=from_zone).astimezone(to_zone).strftime('%d %b %Y').upper()
				if("original_date" in post_child):
					post_child["original_date"] = datetime.strptime(post_child.get("original_date"), "%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=from_zone).astimezone(to_zone).strftime('%d %b %Y').upper()
				post_child["id"] = post_child.pop("_id")
				post_child["text"] = self._process_urls(post_child["text"])
				post_child["text"] = self._process_hashtags(post_child["text"])
				if id_user:
					user_voted = [x for x in post_child["voters"] if id_user == x["id_user"]]
					post_child["user_vote"] = 0
					for user_vote in user_voted:
						post_child["user_vote"] += user_vote["vote"]

				post['replies'].append(post_child)
			except:
				pass
开发者ID:GeographicaGS,项目名称:moocng,代码行数:31,代码来源:models.py


示例17: clone_activity_user_course

def clone_activity_user_course(user, copy_course, original_course=None, force_email=False):
    if not original_course:
        original_course = copy_course.created_from
        if not original_course:
            raise ValueError("This course needs a original course")
    try:
        course_student_relation = user.coursestudent_set.get(course=copy_course)
    except CourseStudent.DoesNotExist:
        return ([], [], [])

    file_name = get_trace_clone_file_name(original_course, copy_course)
    file_path = get_trace_clone_file_path(file_name)
    f = open(file_path)
    trace_ids = json.loads(f.read())
    f.close()
    if not copy_course.pk == trace_ids['Course'][str(original_course.pk)]:
        raise ValueError

    mongo_db = mongodb.get_db()

    new_act_docs = _clone_activity_user_course(mongo_db, trace_ids, user,
                                               copy_course, original_course)

    insert_answer_docs, update_answer_docs = _clone_answer_user_course(
        mongo_db, trace_ids, user,
        copy_course, original_course)

    if (new_act_docs or insert_answer_docs or update_answer_docs or
       course_student_relation.old_course_status != 'c' or force_email):
        if course_student_relation.old_course_status != 'c':
            course_student_relation.old_course_status = 'c'
            course_student_relation.save()
        if not settings.DEBUG:
            send_cloned_activity_email(original_course, copy_course, user)
    return (new_act_docs, insert_answer_docs, update_answer_docs)
开发者ID:Macro-Bin,项目名称:moocng,代码行数:35,代码来源:utils.py


示例18: dispatch

 def dispatch(self, request_type, request, **kwargs):
     db = get_db()
     self.user_answers = get_user(request, db.get_collection('answers'))
     self.user_activity = get_user(request, db.get_collection('activity'))
     return super(KnowledgeQuantumResource, self).dispatch(request_type,
                                                           request,
                                                           **kwargs)
开发者ID:HengeSense,项目名称:moocng,代码行数:7,代码来源:resources.py


示例19: update_stats

def update_stats(submitted, data_kq=None, data_unit=None, data_course=None):
    db = get_db()
    data_kq = data_kq or {}
    data_unit = data_unit or {}
    data_course = data_course or {}
    # KQ
    # TODO passed
    if data_kq.keys():
        stats_kq = db.get_collection('stats_kq')
        stats_kq.update(
            {'kq_id': submitted['kq_id']},
            {'$inc': data_kq},
            safe=True
        )

    # UNIT
    # TODO passed
    if data_unit.keys():
        stats_unit = db.get_collection('stats_unit')
        stats_unit.update(
            {'unit_id': submitted['unit_id']},
            {'$inc': data_unit},
            safe=True
        )

    # COURSE
    # TODO passed
    if data_course.keys():
        stats_course = db.get_collection('stats_course')
        stats_course.update(
            {'course_id': submitted['course_id']},
            {'$inc': data_course},
            safe=True
        )
开发者ID:Macro-Bin,项目名称:moocng,代码行数:34,代码来源:tasks.py


示例20: on_activity_created_task

def on_activity_created_task(activity_created, unit_activity, course_activity):
    db = get_db()
    kq = KnowledgeQuantum.objects.get(id=activity_created["kq_id"])
    kq_type = kq.kq_type()

    # KQ
    data = {"viewed": 1}
    if kq_type == "Video":
        data["passed"] = 1
    stats_kq = db.get_collection("stats_kq")
    stats_kq.update({"kq_id": activity_created["kq_id"]}, {"$inc": data}, safe=True)

    # UNIT
    data = {}
    if unit_activity == 1:  # First activity of the unit
        data["started"] = 1
    elif kq.unit.knowledgequantum_set.count() == unit_activity:
        data["completed"] = 1
    # TODO passed
    if data.keys():
        stats_unit = db.get_collection("stats_unit")
        stats_unit.update({"unit_id": kq.unit.id}, {"$inc": data}, safe=True)

    # COURSE
    course_kqs = KnowledgeQuantum.objects.filter(unit__course__id=activity_created["course_id"]).count()
    data = {}
    if course_activity == 1:  # First activity of the course
        data["started"] = 1
    elif course_kqs == course_activity:
        data["completed"] = 1
    # TODO passed
    if data.keys():
        stats_course = db.get_collection("stats_course")
        stats_course.update({"course_id": activity_created["course_id"]}, {"$inc": data}, safe=True)
开发者ID:NoriVicJr,项目名称:moocng,代码行数:34,代码来源:tasks.py



注:本文中的moocng.mongodb.get_db函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python moonplayer.get_url函数代码示例发布时间:2022-05-27
下一篇:
Python termcolor.cprint函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap