PaferaPy Async 0.1
ASGI framework focused on simplicity and efficiency
All Classes Namespaces Files Functions Variables
card.py
Go to the documentation of this file.
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4from pafera.db import *
6
7from pafera.utils import *
8from pafera.validators import *
9
10from apps.learn.schoolclass import *
11
12# Card flags
13CARD_COURSE = 0x1
14CARD_UNIT = 0x2
15CARD_LESSON = 0x4
16CARD_ASIDE = 0x8
17CARD_PROBLEM = 0x10
18CARD_ANSWER = 0x20
19CARD_PRIVATE = 0x40
20
21# Link types
22CARD_CHILD = 0x1
23
24# Problem link types. Each type will be used only for their
25# respective tests.
26CARD_LINK_PROBLEM = 0x1
27CARD_LINK_QUIZ_PROBLEM = 0x2
28CARD_LINK_TEST_PROBLEM = 0x3
29CARD_LINK_EXAM_PROBLEM = 0x4
30
31CARD_TYPES = {
32 1: 'English',
33 2: 'Chinese',
34 3: 'Russian',
35}
36
37# *********************************************************************
39 """Represents a flashcard, which is the basic unit that the Pafera
40 Learning System is based upon.
41
42 A card can contain text, images, sounds, videos, markdown, HTML, or
43 can serve as containers for other cards. Courses and lessons are
44 all individual cards themselves, whereas problems are simply
45 defining a relationship between various cards.
46
47 Each card has one owner which can edit the contents of the card, but
48 you can assign multiple editors to help. In the case that someone
49 changes a card but is not an owner or editor, the system will use
50 a copy-on-change scheme to give them their own copy of the card
51 without changing the original card.
52 """
53
54 _dbfields = {
55 'rid': ('INTEGER PRIMARY KEY', 'NOT NULL',),
56 'cardtype': ('INT', 'NOT NULL',),
57 'image': ('IMAGEFILE', "NOT NULL DEFAULT ''",),
58 'sound': ('SOUNDFILE', "NOT NULL DEFAULT ''",),
59 'video': ('VIDEOFILE', "NOT NULL DEFAULT ''",),
60 'title': ('TEXT', "NOT NULL DEFAULT ''",),
61 'description': ('TEXT', "NOT NULL DEFAULT ''",),
62 'content': ('MULTITEXT', "NOT NULL DEFAULT ''",),
63 'markdown': ('MULTITEXT', "NOT NULL DEFAULT ''",),
64 'html': ('MULTITEXT', "NOT NULL DEFAULT ''",),
65 'ownerid': ('INT', 'NOT NULL',),
66 'editors': ('NEWLINELIST', "NOT NULL DEFAULT ''",),
67 'flags': ('INT', 'NOT NULL DEFAULT 0',),
68 }
69 _dbindexes = ()
70 _dblinks = ['learn_card', 'learn_problem']
71 _dbdisplay = ['title', 'content']
72 _dbflags = DB_TRACK_CHANGES | DB_TRACK_VALUES
73
74 # -------------------------------------------------------------------
75 def __init__(self):
76 super().__init__()
77
78# =====================================================================
80 """In order to facilitate teachers being able to create and sell
81 courses on our website, we check which cards a student is able to
82 see and deny access to anyone who has not joined the proper course.
83
84 This function updates allowed cards for teachers and students.
85 """
86 cardids = []
87
88 for r in g.db.Linked(obj, learn_schoolclass, fields = 'rid'):
89 for course in g.db.Linked(r, learn_card, fields = 'rid'):
90 cardids.append(ToShortCode(course.rid))
91
92 cardids.extend(GetAllCards(g, course))
93
94 # Take out repeated cards to save space
95 cardids = sorted(set(cardids))
96
97 obj.Set(allowedcards = cardids)
98
99 g.db.Update(obj)
100
101# =====================================================================
102def GetAllCards(g, card):
103 """This function returns all child cards given a parent card.
104 """
105 cardids = []
106
107 for r in g.db.Linked(card, learn_card, CARD_CHILD, fields = 'rid, flags'):
108
109 cardids.append(ToShortCode(r.rid))
110
111 if not (r.flags & CARD_LESSON):
112 cardids.extend(GetAllCards(g, r))
113
114 return cardids
115
116# =====================================================================
117def GetCardTitles(g, lessonids):
118 """This is a convenience function to get all titles for a given set
119 of cardids. Useful for selecting child cards.
120 """
121 lessontitles = {}
122
123 for r in g.db.Find(
124 learn_card,
125 'WHERE rid IN (' + ', '.join(['?' for x in lessonids]) + ')',
126 lessonids,
127 fields = 'rid, title'
128 ):
129 lessontitles[ r.rid ] = r.title
130
131 return lessontitles
132
Represents a flashcard, which is the basic unit that the Pafera Learning System is based upon.
Definition: card.py:38
def __init__(self)
Initialize all fields at creation like a good programmer should.
Definition: card.py:75
Base class for all database models.
Definition: modelbase.py:20
def GetAllCards(g, card)
This function returns all child cards given a parent card.
Definition: card.py:102
def GetCardTitles(g, lessonids)
This is a convenience function to get all titles for a given set of cardids.
Definition: card.py:117
def UpdateAllowedCards(g, obj)
In order to facilitate teachers being able to create and sell courses on our website,...
Definition: card.py:79
Definition: db.py:1
def ToShortCode(val, chars='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_')
Turns a 32-bit value into a six character alphanumeric code.
Definition: utils.py:36