本文整理汇总了Python中openedx.core.djangoapps.models.course_details.CourseDetails.fetch_about_attribute方法的典型用法代码示例。如果您正苦于以下问题:Python CourseDetails.fetch_about_attribute方法的具体用法?Python CourseDetails.fetch_about_attribute怎么用?Python CourseDetails.fetch_about_attribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类openedx.core.djangoapps.models.course_details.CourseDetails
的用法示例。
在下文中一共展示了CourseDetails.fetch_about_attribute方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_overview
# 需要导入模块: from openedx.core.djangoapps.models.course_details import CourseDetails [as 别名]
# 或者: from openedx.core.djangoapps.models.course_details.CourseDetails import fetch_about_attribute [as 别名]
def get_overview(self, course_overview):
"""
Get the representation for SerializerMethodField `overview`
"""
# Note: This makes a call to the modulestore, unlike the other
# fields from CourseSerializer, which get their data
# from the CourseOverview object in SQL.
return CourseDetails.fetch_about_attribute(course_overview.id, 'overview')
示例2: get_course_description
# 需要导入模块: from openedx.core.djangoapps.models.course_details import CourseDetails [as 别名]
# 或者: from openedx.core.djangoapps.models.course_details.CourseDetails import fetch_about_attribute [as 别名]
def get_course_description(
self, bot, chat_id, course_name=None, course_key=None, enroll=None, enroll_keyboard=False
):
"""
Get description of particular courses by Title or by course_id
"""
bot.sendChatAction(chat_id=chat_id, action=ChatAction.TYPING)
if enroll:
result = CourseEnrollment.objects.get(id=enroll)
results = [modulestore().get_course(result.course_id)]
course_name = results[0].display_name_with_default
elif course_key:
results = [modulestore().get_course(course_key)]
course_name = modulestore().get_course(course_key).display_name_with_default
else:
results = modulestore().get_courses()
results = [course for course in results if course.scope_ids.block_type == "course"]
for each in results:
if each.display_name_with_default == course_name:
message = truncate_course_info(CourseDetails.fetch_about_attribute(each.id, "overview"))
if message == "null":
bot.sendMessage(chat_id=chat_id, text="I'm sorry, but this course has no description")
else:
bot.sendMessage(
chat_id=chat_id, text="*Short course description*", parse_mode=telegram.ParseMode.MARKDOWN
)
course_key = each.id
current_site = Site.objects.get_current()
course_title = modulestore().get_course(course_key).display_name_with_default
course_url = "[%s](%scourses/%s/)" % (course_title, current_site, each.id)
bot.sendMessage(chat_id=chat_id, text=course_url, parse_mode=telegram.ParseMode.MARKDOWN)
if enroll_keyboard:
reply_markup = InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
text="I like it and I want to enroll",
callback_data=json.dumps({"key": str(course_key)}),
)
]
]
)
bot.sendMessage(chat_id=chat_id, text=message, reply_markup=reply_markup)
else:
bot.sendMessage(chat_id=chat_id, text=message)
break
bot.sendMessage(chat_id=chat_id, reply_markup=ReplyKeyboardHide())
示例3: get_coursed_and_create_matrix
# 需要导入模块: from openedx.core.djangoapps.models.course_details import CourseDetails [as 别名]
# 或者: from openedx.core.djangoapps.models.course_details.CourseDetails import fetch_about_attribute [as 别名]
def get_coursed_and_create_matrix():
results = [course for course in modulestore().get_courses() if course.scope_ids.block_type == "course"]
new_matrix = TfidMatrixAllCourses.objects.all().first() or TfidMatrixAllCourses()
print new_matrix.matrix.shape[0] != len(results)
if new_matrix.matrix.shape[0] != len(results):
all_courses = [re.sub("<[^>]*>", "", CourseDetails.fetch_about_attribute(x.id, "overview")) for x in results]
MatrixEdxCoursesId.objects.all().delete()
map(lambda x: MatrixEdxCoursesId.objects.create(course_key=x.id, course_index=results.index(x)), results)
stemmer = snowballstemmer.stemmer("english")
courses_stem = [" ".join(stemmer.stemWords(x.split())) for x in all_courses]
vect = TfidfVectorizer(stop_words=get_stop_words(), lowercase=True, dtype=np.float32)
matrix = vect.fit_transform(courses_stem)
new_matrix.matrix = matrix
new_matrix.save()
示例4: update_course
# 需要导入模块: from openedx.core.djangoapps.models.course_details import CourseDetails [as 别名]
# 或者: from openedx.core.djangoapps.models.course_details.CourseDetails import fetch_about_attribute [as 别名]
def update_course(self, mongo_course, assign_universities=False):
'''
For the given course, we create or update the corresponding
course in SQL Course table.
'''
course_handler = CourseHandler(mongo_course)
key = course_handler.key
self.stdout.write('Updating data for course {}\n'.format(key))
course, was_created = Course.objects.get_or_create(key=key)
if was_created or assign_universities:
university = course_handler.assign_university(course)
if university:
self.stdout.write('\t University assigned to "{}"\n'.format(key))
else:
self.stdout.write('\t No university assigned to "{}"\n'.format(key))
course.is_active = True
course.university_display_name = course_handler.university_name
course.title = course_handler.title
course.image_url = course_handler.image_url
thumbnails_info = {}
for thumbnail_alias, thumbnails_options in \
courses_settings.FUN_THUMBNAIL_OPTIONS.items():
thumbnail = course_handler.make_thumbnail(thumbnails_options)
if thumbnail:
thumbnails_info[thumbnail_alias] = thumbnail.url
course.thumbnails_info = thumbnails_info
course.start_date = mongo_course.start
course.enrollment_start_date = mongo_course.enrollment_start
course.enrollment_end_date = mongo_course.enrollment_end
course.end_date = mongo_course.end
course_key = CourseKey.from_string(key)
course.short_description = CourseDetails.fetch_about_attribute(
course_key,
'short_description',
)
course.save()
del course
self.stdout.write('Updated course {}\n'.format(key))
return None
示例5: check_course_overview_against_course
# 需要导入模块: from openedx.core.djangoapps.models.course_details import CourseDetails [as 别名]
# 或者: from openedx.core.djangoapps.models.course_details.CourseDetails import fetch_about_attribute [as 别名]
def check_course_overview_against_course(self, course):
"""
Compares a CourseOverview object against its corresponding
CourseDescriptor object.
Specifically, given a course, test that data within the following three
objects match each other:
- the CourseDescriptor itself
- a CourseOverview that was newly constructed from _create_or_update
- a CourseOverview that was loaded from the MySQL database
Arguments:
course (CourseDescriptor): the course to be checked.
"""
def get_seconds_since_epoch(date_time):
"""
Returns the number of seconds between the Unix Epoch and the given
datetime. If the given datetime is None, return None.
Arguments:
date_time (datetime): the datetime in question.
"""
if date_time is None:
return None
epoch = datetime.datetime.utcfromtimestamp(0).replace(tzinfo=pytz.utc)
return math.floor((date_time - epoch).total_seconds())
# Load the CourseOverview from the cache twice. The first load will be a cache miss (because the cache
# is empty) so the course will be newly created with CourseOverview._create_or_update. The second
# load will be a cache hit, so the course will be loaded from the cache.
course_overview_cache_miss = CourseOverview.get_from_id(course.id)
course_overview_cache_hit = CourseOverview.get_from_id(course.id)
# Test if value of these attributes match between the three objects
fields_to_test = [
'id',
'display_name',
'display_number_with_default',
'display_org_with_default',
'advertised_start',
'social_sharing_url',
'certificates_display_behavior',
'certificates_show_before_end',
'cert_name_short',
'cert_name_long',
'lowest_passing_grade',
'end_of_course_survey_url',
'mobile_available',
'visible_to_staff_only',
'location',
'number',
'url_name',
'display_name_with_default',
'display_name_with_default_escaped',
'start_date_is_still_default',
'pre_requisite_courses',
'enrollment_domain',
'invitation_only',
'max_student_enrollments_allowed',
'catalog_visibility',
]
for attribute_name in fields_to_test:
course_value = getattr(course, attribute_name)
cache_miss_value = getattr(course_overview_cache_miss, attribute_name)
cache_hit_value = getattr(course_overview_cache_hit, attribute_name)
self.assertEqual(course_value, cache_miss_value)
self.assertEqual(cache_miss_value, cache_hit_value)
# Test if return values for all methods are equal between the three objects
methods_to_test = [
('clean_id', ()),
('clean_id', ('#',)),
('has_ended', ()),
('has_started', ()),
('may_certify', ()),
]
for method_name, method_args in methods_to_test:
course_value = getattr(course, method_name)(*method_args)
cache_miss_value = getattr(course_overview_cache_miss, method_name)(*method_args)
cache_hit_value = getattr(course_overview_cache_hit, method_name)(*method_args)
self.assertEqual(course_value, cache_miss_value)
self.assertEqual(cache_miss_value, cache_hit_value)
# Other values to test
# Note: we test the time-related attributes here instead of in
# fields_to_test, because we run into trouble while testing datetimes
# for equality. When writing and reading dates from databases, the
# resulting values are often off by fractions of a second. So, as a
# workaround, we simply test if the start and end times are the same
# number of seconds from the Unix epoch.
time_field_accessor = lambda object, field_name: get_seconds_since_epoch(getattr(object, field_name))
# The course about fields are accessed through the CourseDetail
# class for the course module, and stored as attributes on the
# CourseOverview objects.
course_about_accessor = lambda object, field_name: CourseDetails.fetch_about_attribute(object.id, field_name)
others_to_test = [
#.........这里部分代码省略.........
示例6: test_fetch_about_attribute_error
# 需要导入模块: from openedx.core.djangoapps.models.course_details import CourseDetails [as 别名]
# 或者: from openedx.core.djangoapps.models.course_details.CourseDetails import fetch_about_attribute [as 别名]
def test_fetch_about_attribute_error(self):
attribute_name = 'not_an_about_attribute'
with self.store.branch_setting(ModuleStoreEnum.Branch.draft_preferred, self.course.id):
CourseDetails.update_about_item(self.course, attribute_name, 'test_value', self.user.id)
with self.assertRaises(ValueError):
CourseDetails.fetch_about_attribute(self.course.id, attribute_name)
示例7: test_fetch_about_attribute
# 需要导入模块: from openedx.core.djangoapps.models.course_details import CourseDetails [as 别名]
# 或者: from openedx.core.djangoapps.models.course_details.CourseDetails import fetch_about_attribute [as 别名]
def test_fetch_about_attribute(self, attribute_name):
attribute_value = 'test_value'
with self.store.branch_setting(ModuleStoreEnum.Branch.draft_preferred, self.course.id):
CourseDetails.update_about_item(self.course, attribute_name, attribute_value, self.user.id)
self.assertEqual(CourseDetails.fetch_about_attribute(self.course.id, attribute_name), attribute_value)
示例8: _create_from_course
# 需要导入模块: from openedx.core.djangoapps.models.course_details import CourseDetails [as 别名]
# 或者: from openedx.core.djangoapps.models.course_details.CourseDetails import fetch_about_attribute [as 别名]
def _create_from_course(cls, course):
"""
Creates a CourseOverview object from a CourseDescriptor.
Does not touch the database, simply constructs and returns an overview
from the given course.
Arguments:
course (CourseDescriptor): any course descriptor object
Returns:
CourseOverview: overview extracted from the given course
"""
from lms.djangoapps.certificates.api import get_active_web_certificate
from openedx.core.lib.courses import course_image_url
log.info('Creating course overview for %s.', unicode(course.id))
# Workaround for a problem discovered in https://openedx.atlassian.net/browse/TNL-2806.
# If the course has a malformed grading policy such that
# course._grading_policy['GRADE_CUTOFFS'] = {}, then
# course.lowest_passing_grade will raise a ValueError.
# Work around this for now by defaulting to None.
try:
lowest_passing_grade = course.lowest_passing_grade
except ValueError:
lowest_passing_grade = None
display_name = course.display_name
start = course.start
end = course.end
max_student_enrollments_allowed = course.max_student_enrollments_allowed
if isinstance(course.id, CCXLocator):
from lms.djangoapps.ccx.utils import get_ccx_from_ccx_locator
ccx = get_ccx_from_ccx_locator(course.id)
display_name = ccx.display_name
start = ccx.start
end = ccx.due
max_student_enrollments_allowed = ccx.max_student_enrollments_allowed
return cls(
version=cls.VERSION,
id=course.id,
_location=course.location,
org=course.location.org,
display_name=display_name,
display_number_with_default=course.display_number_with_default,
display_org_with_default=course.display_org_with_default,
start=start,
end=end,
advertised_start=course.advertised_start,
announcement=course.announcement,
course_image_url=course_image_url(course),
social_sharing_url=course.social_sharing_url,
certificates_display_behavior=course.certificates_display_behavior,
certificates_show_before_end=course.certificates_show_before_end,
cert_html_view_enabled=course.cert_html_view_enabled,
has_any_active_web_certificate=(get_active_web_certificate(course) is not None),
cert_name_short=course.cert_name_short,
cert_name_long=course.cert_name_long,
lowest_passing_grade=lowest_passing_grade,
end_of_course_survey_url=course.end_of_course_survey_url,
days_early_for_beta=course.days_early_for_beta,
mobile_available=course.mobile_available,
visible_to_staff_only=course.visible_to_staff_only,
_pre_requisite_courses_json=json.dumps(course.pre_requisite_courses),
enrollment_start=course.enrollment_start,
enrollment_end=course.enrollment_end,
enrollment_domain=course.enrollment_domain,
invitation_only=course.invitation_only,
max_student_enrollments_allowed=max_student_enrollments_allowed,
catalog_visibility=course.catalog_visibility,
short_description=CourseDetails.fetch_about_attribute(course.id, 'short_description'),
effort=CourseDetails.fetch_about_attribute(course.id, 'effort'),
course_video_url=CourseDetails.fetch_video_url(course.id),
self_paced=course.self_paced,
)
示例9: recommend
# 需要导入模块: from openedx.core.djangoapps.models.course_details import CourseDetails [as 别名]
# 或者: from openedx.core.djangoapps.models.course_details.CourseDetails import fetch_about_attribute [as 别名]
def recommend(self, bot, chat_id, telegram_user):
telegram_id = telegram_user.telegram_id
if not LearningPredictionForUser.objects.filter(telegram_user=telegram_user):
bot.sendMessage(
chat_id=chat_id,
text="It seems like I see you for the first time,"
" please answer a few questions, so I'll be know more about you",
)
prediction.get_test_courses(telegram_id)
test_courses = LearningPredictionForUser.objects.get(telegram_user=telegram_user).get_list()
if len(test_courses) > 0:
course_id = MatrixEdxCoursesId.objects.filter(course_index=test_courses[0]).first().course_key
course_key = CourseKey.from_string(course_id)
reply_markup = InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
text="I like it!", callback_data=json.dumps({"method": "learning", "kwargs": {}})
)
],
[
InlineKeyboardButton(
text="Hmmm. I don't like at all!",
callback_data=json.dumps({"method": "learning", "kwargs": {"is_positive": False}}),
)
],
]
)
else:
predicted_course_id = prediction.prediction(telegram_id)
if predicted_course_id == -1:
bot.sendMessage(chat_id=chat_id, text="It seems like you have enrolled to all courses we have for now")
return
predicted_course_key = (
MatrixEdxCoursesId.objects.filter(course_index=predicted_course_id).first().course_key
)
bot.sendMessage(chat_id=chat_id, text="Now I'm going to recommend you some great courses")
course_key = CourseKey.from_string(predicted_course_key)
course_for_user = PredictionForUser.objects.get_or_create(telegram_user=telegram_user)[0]
course_for_user.prediction_course = predicted_course_key
course_for_user.save()
reply_markup = InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
text="I like it and I want to enroll",
callback_data=json.dumps({"method": "enroll", "kwargs": {}}),
)
],
[
InlineKeyboardButton(
text="I like it but will eroll another time",
callback_data=json.dumps({"method": "not_enroll", "kwargs": {}}),
)
],
[
InlineKeyboardButton(
text="What the shit is this (I don't like it)",
callback_data=json.dumps({"method": "wrong_predict", "kwargs": {}}),
)
],
]
)
course_description = CourseDetails.fetch_about_attribute(course_key, "overview")
course_title = modulestore().get_course(course_key).display_name_with_default
bot.sendMessage(chat_id=chat_id, text="*%s*" % course_title, parse_mode=telegram.ParseMode.MARKDOWN)
bot.sendMessage(chat_id=chat_id, text=truncate_course_info(course_description), reply_markup=reply_markup)